Search code examples
javalibgdx

How projectile motion should be simulated?


I am making a simulator using libgdx about projectile motion, something like this. Based on my understanding velocity x and velocity y are seperate and don't affect each other, so the object being trown an an angle will fall due to gravity. So this means if the object initial speed is 40 m/s then it means it only refers horizontally? or x = 40m/s, So what are the formulas to follow when computing for the velocity of x and y with initial speed and angle?


Solution

  • @Alnitak has the initial velocity part nailed on - you need to know the initial speed, and the initial angle (measured up from the horizontal), then you can use his formulae to work out the initial x and y components of the velocity (call them x0 and y0 - these are fixed numbers, or constants).

    All that answer was missing is how to calculate how those x and y components of velocity change over time.

    Assuming no air resistance (probably fair for a typical game) there is no horizontal acceleration, meaning the x component stays fixed at x0 for all time.

    Vertically you have acceleration due to gravity, g, which is typically approximated by 9.8ms-2 but you can tweak it to be whatever feels right.

    To get the vertical velocity, y, after time t seconds, given that it started with initial vertical velocity y0 (which you already calculated), you can do:

    y = y0 - 9.8*t

    (subtracting since gravity goes down)

    So at time t your velocity is (x0, y0 - 9.8*t).

    Google 'kinematics equations' for any more info