Kevin Bryan
Kevin Bryan

Reputation: 1858

How do you calculate the displacement of a projectile with an initial height?

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:

enter image description here

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

Answers (1)

Pavisa
Pavisa

Reputation: 162

I made this program aswell. You have four main components that you need to consider:

  • VelocityX- remains constant unless you take air friction into account, it's calculated as Velocity*cos(theta)
  • VelocityY- which changes over time following formula Vy=Velocitysin(theta)-gtime
  • X position- VelocityX*time;
  • Y position- VelocityY*time-0.5*g*time^2

If you want your projectile to start at some given height you need to change Y position formula to something like:

  • Y position- StartingHeight+VelocityY*time-0.5*g*time^2

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)

  • Velocity Y=0
  • (Velocity*sin(theta))/g The time required for the projectile to reach it's peak is the same time needed for it to fall down on the ground ( if it was launched from the ground, that is starting height was zero)

Peak height is VelocityY multiplied by time required to reach that peak

  • PeakHeight=StartingHeight+(Velocitysin(theta)-gtime)(Velocitysin(theta))/g

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

Related Questions