I have been doing some code on projectile motion. I initiated a timer t and used the constructor 10 for it, every action event I add 0.01 to a time variable initialised as 0.00, to simulate time going past. Using this time I calculate the height/distance etc. I cannot figure out 2 problems:
1) When printing the current time into the console it keeps going to many decimal places, run code. 2) Height will keep increasing even though it shouldn't, I feel as if it is due to the positive and negative of acceleration.
(My panel is 1920x1080 and the panel animating takes up 80pixels - that's why it is 1000-50) (1000-radius)
Images for use: https://i.stack.imgur.com/dho5L.jpg
From responses: 1) Forgot to multiply vy by t. 2) Use DecimalFormat to truncate the value
Thanks to @G_H ; @Brick ; @Tom ; @Diginoise Code:
package projectV1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Animate extends JPanel implements ActionListener {
double x = 0;
double time = 0.00;
String timeText2 = "0";
double y = 1000;
double velY = 0.0;
double velX = 0.0;
public static Timer t;
public Animate() { // Constructor
super();
}
public void start(String acceleration, String initialVelocity, String angle){
new Ball(acceleration, angle, initialVelocity);
t = new Timer(10, this);
t.start();
}
@Override
public void actionPerformed(ActionEvent e) {
updateTime();
move();
}
public void move(){ // HOlds the repaint method
x = x + Ball.getVelX();
Simulation.distanceText.setText(Double.toString(Ball.getDistance(time))); // Some of these are just checks for me in the gui
Simulation.velocityText.setText(Double.toString(time));
y = y- Ball.getVelY(time);
Simulation.heightText.setText(Double.toString(Ball.getHeight(time)));
repaint();
}
public void updateTime(){
time= time + 0.01;
System.out.println(Double.toString(time)); // Using this as a test to check the time output.
timeText2 = Double.toString(time);
Simulation.timeText.setText(timeText2);
}
public void paintComponent(Graphics gg){
super.paintComponent(gg);
gg.drawRect(0, 0, 1920, 1000);
Graphics2D g = (Graphics2D) gg;
Ellipse2D.Double shape = new Ellipse2D.Double(x, y-50.0, 50, 50);
g.fill(shape);
// g.fillOval(x, y-50, 50, 50);
}
}
Code from '' Ball Class ''
public Ball(String acceleration, String angle, String initialVelocity ) {
Ball.acceleration = acceleration;
Ball.initialVelocity = initialVelocity;
Ball.angle = angle;
}
public static double getVelX(){ // This stays constant throughout the program
double velX = 0.0;
int u = Integer.parseInt(initialVelocity);
double ang = Double.parseDouble(angle);
velX = u*(Math.cos(ang));
return velX;
}
public static double getVelY(double time ){
double velY = 0.0 ;
double ang, acc = 0;
int u = 0;
ang = Double.parseDouble(angle);
acc = Double.parseDouble(acceleration);
u = Integer.parseInt(initialVelocity);
velY = u*(Math.sin(ang))- (acc*time);
return velY;
}
public static double getDistance(double time){
double distance = 0;
double ang = 0;
int u = 0;
ang = Double.parseDouble(angle);
u = Integer.parseInt(initialVelocity);
distance = (u*time)*(Math.cos(ang));
return distance;
}
public static double predictedTime(){
double ptime = 0;
double ang = 0;
ang = Double.parseDouble(angle);
int u = Integer.parseInt(initialVelocity);
ptime = ((u*u)*(Math.sin(ang)*Math.sin(ang))/(2*9.81));
return ptime;
}
public static double getHeight(double time ){
double height;
double ang, acc;
int u = 0;
ang = Double.parseDouble(angle);
acc = Double.parseDouble(acceleration);
u = Integer.parseInt(initialVelocity);
height = u*(Math.sin(ang))-(0.5*acc*(time*time));
return height;
}