Help calculate the range of a projectile?

0 favourites
  • 5 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi,

    I want to be able to determine where a projectile (a ball) will hit the ground.

    So I'm trying to implement a formula I found on WikiPedia:

    en.wikipedia.org/wiki/Range_of_a_projectile

    It's the formula under "Solving for the range once again"

    But I can't get it to work. I've made this CAPX to help as follows:

    1. A ball is moved to a fixed position then its x & y velocities are set to random values that cause the ball to be launched upwards and to the right

    2. after 0.2 seconds, I snapshot the ball's X & Y positions and velocities

    3. I feed these into what I think the formula on Wikipedia dictates and display the result

    4. When the ball lands, I show the actual distance travelled.

    Problem is that 3 =/= 4 and I can't figure out why.

    Pretty sure I could have figured all this out from first principles whilst at school - but that was 40 years ago!

    Thanks!

    drive.google.com/file/d/1PWaQTllN5PN0pDyYR67oDmwGKxzySIfi/view

  • Messed with your example briefly but no luck. Do note, that the gravity value you should use in the equations needs to be 50x the gravity value that the physics behavior uses.

    Anyways you could try it with the bullet behavior instead in case the physics behavior is throwing it off.

    I mean your logic seems sound. There’s probably something I didn’t notice that’s throwing it off. If it helps this is what I come up with when working out the math. I haven’t tested it though.

    Var g=500
    Var x0=0
    Var rangeCalc=0
    Var rangeActual=0
    
    Start of layout 
    — ball: set position to (0, 400)
    — ball: set velocity to (100,-100)
    — set x0 to ball.x
    — set rangeCalc to (-vy + sqrt(vy*vy + 2*g*(480-ball.y)))*vx/g
    
    On ball collides with ground
    — set rangeActual to ball.x-x0
  • Thanks for taking a look - I need to spend some more time investigating - will see if I can implement your code to see if I get a different result.

  • I was able to test mine as working so I now see what you had amiss.

    You had:

    (Vx / 10)* (Vy + sqrt(((Vy * Vy) + (2 * 10 * Y0Height)))) - X0

    But it needs to be this. 500 instead of 10, and you don't subtract x0.

    (Vx / 500)* (Vy + sqrt(((Vy * Vy) + (2 * 500 * Y0Height))))

    That will the calculated be approximately the actual. It will be off since the floor is hit a bit early.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you so much - it works! I found this website that allows you to enter values and will calculate the result:

    omnicalculator.com/physics/projectile-motion

    Which I used to confirm your gravity x 50 tip so realised the error must be in my code. Was just about to debug when I noticed you'd posted above.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)