I've got a side scrolling game going. If the player is on a platform thats higher or lower than the AI, the AI will throw a grenade at the player. The code for the movement of my grenades look like this:
Set Grenade Alterable Value X to Alterable Value X + Speed X
Set Grenade Alterable Value Y to Alterable Value Y - Speed Y
Set Grenade X position to Alterable Value X
Set Grenade Y position to Alterable Value Y
Set Alterable Value Y to Alterable Value Y - 1 <- gravity
Simple enough. So when the AI throws the grenade, I need to set it's Speed X and Speed Y to the appropriate values such that the grenade will actually hit the player. This technique is done in many games so I was wondering what technique one would use.
To be honest, they don't fit into the way the rest of the engine is structured. If I understand what you mean by quadratics, I'd have to either make x a function of y or vice versa, and that would run into problems with some other checks in the code.
Regardless, please elaborate on 'quadratics'. A solution in some shape or form would be nice.
I think any inaccuracies might just be coming from not casting the integers as floats, so the divisions truncate any decimals, compounding the error. So multiply top and bottom of each fraction by 1.0 if you try that code. I don't have MMF1 working on this computer to test it out again, though.
Edited by the Author.
"Omg. Where did they get the idea to not use army guys? Are they taking drugs?" --Tim Schafer on originality in videogames
quadratic equation is of the form y = ax^2 + bx + c
if you know a b and c you can find the x intercepts. quadratic formula is of the form x = (-b(+or-)sqrt(b^2 - 4ac))/2a
the equation is a parabolic function so if you know 3 points you can derive quadratic equation that goes through them. so you have your enemy which is point number 1 and your player which is point number 2. your third point is in between these two and has a greater y value than both of them. the greater the y value the higher the arc is going to be. id have to crack open an old math book to explain how to get the equation from the three points so i suggest you look it up. but youre right. it might not fit in with your engine.
theres another way to do an arc thats based on angle of projection using sin and cos, velocity, and gravity. that may be of more use to you. any way you look at it, an equation would better suit the engine than using an xspeed and yspeed and changing them as the grenade moves. vectors may be another way to do this as well.
any way you slice it, its gonna be a bit of math to determine how the grenade should move to strike its target.
V is the projection velocity
P is the angle of projection
X and Y are distances.
X speed at projection = V*cos(P)
Y speed = V*sin(P)
using various equations (calculate vertical and horizontal values independantly, time is usually the same in both)
v = u + at
v² = u² + 2as
s = ut + 0.5at²
s = 0.5(u+v)t
v is final velocity
u is initial velocity (or velocity at the point where your equation is calculated from)
s is distance
a is acceleration DOWNWARDS (i.e if your object is travelling upwards, and you are taking upwards velocity to be positive, acceleration would be negative)
horizontal acceleration is 0
velocities can be in pixels/second or pixels/frame, time must be in the corresponding value (i.e if v in pixels/second, time is in seconds)
distances are in pixels
vertical velocity at the midpoint (highest point acheived from the ground, usually half the time taken to complete path) is 0
Something like this may be simpler than using quadratic equations, as you can find variables quite easily.
Sorry Nyob, must have missed it some how. Its a great example and I found it to be amazingly accurate. I'll examine it in detail later, but from a quick look, it seems the arc is a tad high. Some tweaking will hopefully remedy that