Reputation: 1858
I am trying to make a simulator like this in Libgdx and I'm already done with the calculation of the displacement x using this formula:
Java format:
theta = Parameter.angle * 2;
range = ((float) Math.pow(Parameter.speed, 2) / Constant.GRAVITY) * (float) Math.sin(Math.toRadians(theta));
But the problem is that my projectile always start at 0.0m in height, and I would like to be able to set the initial height of the projectile, so what would me the formula needed? also how do you calculate the displacement y?
Upvotes: 1
Views: 567
Reputation: 162
I made this program aswell. You have four main components that you need to consider:
If you want your projectile to start at some given height you need to change Y position formula to something like:
The time projectile will spend in motion ( ie. flying) is actually the time you get when you set Velocity Y equal to zero ( because the projectile is motionless when it reaches it's peak height)
Peak height is VelocityY multiplied by time required to reach that peak
Here's a code snippet from a while ago when i made attempt to do the same job. I've used javafx polyline to draw it since it has sub-pixel precision ( it takes doubles as parameters) Edit:
public void getYPoints(){
int counter=0;
double time=0.0;
double yCoord=y;
while(yCoord>=0){
yCoord=yCoord+(ySpeed*time+0.5*g*time*time);
yPoints.add(yCoord);
counter++;
time+=0.01;
//System.out.printf("Y coord for time %f is %f and in arraylist is %d\n",time,yCoord,yPoints.get(counter-1));
this.time=time;
this.counter=counter;
}
//return yPoints;
}
public void getXPoints(){
double xCoord=x;
double newTime=0.0;
while(newTime<this.time){
xCoord=xCoord+xSpeed*this.time;
xPoints.add(scale*xCoord);
newTime+=0.01;
}
Upvotes: 3