# Calculating the maxima of an object's trajectory given it's y and y velocity

I need to know a few things for this equation
I’ve already tried using (y val)+(yvel^2 / 2g), g being gravity
This equation may work, I just don’t know the exact value gravity is. I’ve put in the gravity my game uses (105) and it’s not working, I’ve even tried dividing that gravity by 29.2 (bc 1 velocity = 29.2 pixels/s)

I need to know this so my ai can figure out if the ball was high enough to hit.

(ignore lines pointing offscreen, those are for testing the previously mentioned equation)
I’ve already made it so the game can calculate the maxima by just finding the highest point the ball was at every time it hits something, but the issue with this is that the bot needs more time to figure out whether to hit, and if I had an equation to do this it would double the bot’s time to figure out whether the maxima of the ball was high enough for a hit.

Only person I can think of that could help me is @JR01

well if you turned gravity off, you could use velocity, which is in blocks per seconds im pretty sure

gravity works differently than a set velocity, it accelerates at a certain speed

plus i’m not redoing 99% of my game’s physics just for an equation

it will not just throw off the ai, but also my muscle memory and skills

Have you learned derivatives yet? If so you could take the equation for the ball’s motion and derive it, then set the derivative equal to 0 and then solve. That will give you the x value in the original function where the ball reaches its peak (the derivative equals 0), and then you can put that x value back into the original equation and solve it for y to get the height at that point.
Or just look for when the ball’s y velocity becomes 0 or super close to 0, and that point is when it’s at its maximum height

already doing that but i need to know it before it hits the max height so my ai has more time to think

the issue is that i have no idea what the equation for the ball’s motion is

1 Like

Ah ok, well from the derivative you get something similar to the expression you have above, except it should be a - sign instead of a + sign (the expression you have above means the ball will just move downwards all the time, regardless of its initial velocity, which isn’t true). Also regarding the velocity / acceleration, you can measure the velocity (in pixels / frame) and acceleration (in pixels / frame^2) using this:

Then take those numbers (or an average for the acceleration since flowlab is inconsistent about it) and put them into your above expression with the sign correction.

[There is no way to perfectly calculate the height, by the way. By nature of how code works on discrete time intervals, there will always be some amount of error since the object must move a certain number of pixels every frame or timestep. The error is most evident in the acceleration, which alternates between values since flowlab is literally adding a different amount of pixels each frame.]

1 Like