I'm having some trouble finding the right formula for this, so i need someone whos good at math to help me out (Ashley..).
An NPC is running forward with a constant speed and finds a platform somewhere ahead of him. Now, using the speed value, the gravity and the position he needs to land on, i need to calculate the strength with wich he has to jump to land on the right spot.
In other words, i got the X velocity and the downwards acceleration, and by inserting the correct Y velocity i need to draw a line from the position of the NPC that passes through a certain xy position.
If anyone understands and can help i would be very happy!
Develop games in your browser. Powerful, performant & highly capable.
you have to draw a straight line or you need to calculate the jump?
those are two different things, unless you're using linear speed.
If you're using platform behavior, only the platform behavior dev may help, as we don't know the exact math behind it and a real jump calculation may differ from the behavior's jump.
for a straight line, just use the starting position x1,y1 and the ending position x2,y2:
arctan(y2-y1 / x2-x1) = angle to get from start to end.
for a normal jump (no idea if this matches platform behavior, I'm sure it doesn't if jump hold is there) where Vy is the vertical speed you need to start with, Vx is the horizontal speed you had (and you weren't jumping or falling), a is the downwards acceleration and ?x,?y are the horizontal and vertical distance to the target:
?y=Vy?t + (a/2)?t?
so since you know your horizontal speed, you can figure out how long will it take you to get there using the first equation:
t = ?x/Vx
and now using that time, you figure out how high you need to jump. NOTE: THIS WILL NOT AVOID OBSTACLES
?y - (a/2)?t? = Vy?t
(?y - (a/2)?t?)/t = Vy
plug the value of t obtained earlier....
(?y - (a/2)?(?x/Vx)?)/(?x/Vx) = Vy <-- and that's your initial jump speed. Do take care of the signs, I assumed gravity acceleration as positive and positive velocity pointing up. In construct I think positive velocity points down, so just reverse the speed sign
Just this morning i figured it out myself (after trying for hours), and i think my solution is the same as what you wrote. Thank you very much for the clear explanation and sorting out the formulas for me! Just what i needed!