The problem:I have a side-scrolling-platform-game where you have 2 characters. The first one is the one you are in controll of. He is ready and done with all the controlls.Then we have the enemy, lets call her Sarah.Sarah will be at the left on the screen throwing stuff at you, and when you get close enough she will either walk or jump to the next waypoint in her waypointlist.After that she will continue to throw stuff at you. That is, untill you get close enough, then she will either walk or jump to the next waypoint and so on and so on.

But I really can't seem to get a code where she jumps EXACTLY to the place i have in her waypoint-vector..

So the variables i have from the beggining och the jump are:The distance in x.The distance in y at the top (i will have the difference in Y in the the platforms + 128 pixels as a top for the parabola).

Asides from those two, i have tried to experiment things like starting veclocity in x and y, gravity and at which time (in frames) the jump would take.

Can somebody please give me either a code for this, a good site about this or something else useful i would be forever grateful!

Thanks!I've been trying to get a good formula from what i know. But i cant get it to be exact. The problem is that it seem to be dependent on to many variables.

For instance we have one formula that says that the speed in the x-axis is the same, it never changes. So if we have that in mind we know that:X (a distance) is equal to V0 * cos(a) * tWhere V0 is the starting velocity, the cos(a) i the cos for the angle at the start and t is the time it takes to travel the distance in x.

Okey, so we can rewrite cos(a) to be V0x / V0 which leads to "x = V0 * (V0x / V0) * t". We see that we can take away the "V0" part leading us to get:"x = V0x * t". This is good, since I know my velocity in x to be the same all the time which means that the v0x is the same.

So we know x, since we have a waypoint that keeps track of the points we want Sarah to go. Perfect you might think! But it is here that it gets tricky. Why?

I'll try to explain.

So say that x is 200. That means Sarah is supposed to jump 200 pixels from her place here. Her speed in the x-axis must be atleast 11 (since my character im steering has 10 in speed, and i want Sarah to be faster then my character).So this will give us the time for the jump.In this case this means that the distance of 200 pixels with a speed of 11 pixels per update (i will refer the time as how many updates something will take. And my FPS is locked at 60 FPS, giving us 60 updates per seond. If you would rather count it as pixels per second or something).Anyhow! This means that the jump will take 200/11 = 18.18181818... ~18.2 updates.

We also know that "y = V0 * sin(a) * t - ((g * t^2) / 2)Where y represents the point at any time in the jump in the y-axis, the g is the gravity and the sin(a) is sin of the angle.We can rewrite sin(a) as V0y / V0.And as above this will lead to "y = V0y * t - ((g* t^2) /2).Now we have three loose variables. The starting velocity V0y, the gravity g and the y.But if we say that we will take the point where y is the highest, then we know that would be exactly the half of the time (18.2 updates).

And say that we take y to be "the difference in y + 128". Meaning if sarah will jump in the same y height, her maximum height will be 128 pixels above her position now. If she were to jump to a platform 64 pixels above her, her maximum height would be 128+64 = 192 pixels and so on. But for now, lets just focus on a jump 200 pixels away in the same y-height. This will give us y = 128 at the time of (18.2 / 2) = 9.1.

But the problem now is that we still have two loose variables, g and V0y, right?

Your system should have a constant g, should it not? Your entire world would have some form of gravity, and your character that is finished will probably be using the same constant. At mid jump, the apex, your V0y would be 0. At the start and the finish, your V0y would be 0. Mid-jump, your V0y would be your previous Vy. I may be wrong on that very last statement, but I don't think so. If V0y was to consider your initial jump's starting velocity, the character is not moving, so that would fully eliminate V0y * t.

EDIT: As an afterthought, and due to it being late, V0y would be a function included in your equation for y, and each point during the jump would recalculate the equation with a different t: y = (g * t + V0y) * t - ((g * t^2) / 2) where V0y is the very first velocity you want to give the character to jump.

-- To clarify: (g * t + V0y) is the velocity the character is at at the end of the last update.

Your system should have a constant g, should it not? Your entire world would have some form of gravity, and your character that is finished will probably be using the same constant. At mid jump, the apex, your V0y would be 0. At the start and the finish, your V0y would be 0. Mid-jump, your V0y would be your previous Vy. I may be wrong on that very last statement, but I don't think so. If V0y was to consider your initial jump's starting velocity, the character is not moving, so that would fully eliminate V0y * t.

EDIT: As an afterthought, and due to it being late, V0y would be a function included in your equation for y, and each point during the jump would recalculate the equation with a different t: y = (g * t + V0y) * t - ((g * t^2) / 2) where V0y is the very first velocity you want to give the character to jump.

-- To clarify: (g * t + V0y) is the velocity the character is at at the end of the last update.

Thanks

The problem is that even though i set a constant "g" and a constant "V0y" the jump doesnt seem to land on the exact position i want to. Sarah seem to land to far :/

Like what one poster said you could simplify things by making some variables constant. But you could make it even easier by making almost every variable constant. Restrict yourself to a limited set of jump vectors and have the AI choose from among set one vector that best fits the situation.