**1**

# Jumping for AI in my platform game

###
#1
Members - Reputation: **100**

Posted 05 March 2012 - 07:37 AM

This is my first post here ^^

Now to le business.

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.

My problem here is that i want to create a code for her jumping. i will have set of waypoints which will have 1) coordinates for the next place she wants to go and 2) if a jump is needed or not.

But i cant seem to find anywhere a similair code for that.

I've looked at parabola equations (http://www.wolframalpha.com/input/?i=y+%3D+%28x%5E2%29+%2F+%284+*+a%29+where+a+is+-1 and http://www.wolframalpha.com/input/?i=parabola+formula)

And also the throwing equation you learned in physics courses at high school (http://www.wolframalpha.com/input/?i=y+%3D+%28V0+*+sin%28a%29+*+t%29+-+%28%28g+*+t%5E2%29+%2F+2%29)

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!

Cheers!

/TheCalm

###
#4
Members - Reputation: **100**

Posted 06 March 2012 - 10:04 AM

Though i'm afraid that's not my problem. I already have a class for the waypoints who contains a position-vector and a bool that keeps track if i need to jump.

The problem is the get a formula to get Sarah to jump and land exactly on the position :/

Cheers!

###
#5
Crossbones+ - Reputation: **484**

Posted 06 March 2012 - 01:37 PM

1) Keep in mind that there may be many jump angles/velocities that land you in the same spot. You can simplify by locking your angle to 45 degrees. (or whatever angle works best for you)

2) If you know the maximum height of your arc, and your acceleration due to gravity, then you also know the time it takes to rise to that point.

3) You posted a lot of formulas you're looking at, but i find diagrams and descriptions a little easier to convert into code:

http://www.physicsclassroom.com/Class/vectors/u3l2c2.cfm

I'll post more when I have time if this was too cryptic :-)

###
#7
Members - Reputation: **100**

Posted 07 March 2012 - 01:32 PM

I don't have time to write code for you, but here are some ideas for getting more information into your equation and simplifying the problem.

1) Keep in mind that there may be many jump angles/velocities that land you in the same spot. You can simplify by locking your angle to 45 degrees. (or whatever angle works best for you)

2) If you know the maximum height of your arc, and your acceleration due to gravity, then you also know the time it takes to rise to that point.

3) You posted a lot of formulas you're looking at, but i find diagrams and descriptions a little easier to convert into code:

http://www.physicscl...tors/u3l2c2.cfm

I'll post more when I have time if this was too cryptic :-)

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) * t

Where 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?

###
#8
Crossbones+ - Reputation: **2577**

Posted 09 March 2012 - 12:42 AM

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.

###
#9
Members - Reputation: **100**

Posted 09 March 2012 - 06:35 AM

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 :/

###
#11
Members - Reputation: **92**

Posted 13 March 2012 - 08:33 PM