# Curves for enemy jump path

This topic is 3841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to make an enemy chase after the player in a 2d platformer, so at various points they need to jump from one point on a platform to another point on a different platform. At first I've tried this by calculating the required jump velocity and just letting the basic physics carry them from start to end. However the results are too inaccurate (from a combination of floating point inaccuracy and euler integration inaccuracy) and it often misses the destination point. Instead I'm thinking of constructing a curve from start to end and just forcing the enemy to follow it so it will always arrive at the exact end point. Currently I create a quadratic bezier curve from the start and end points, and with the middle two control points set at a constant offset above the start and end points. This works great but the movement isn't terribly convincing. Has anyone got any suggestions for a curve method which looks more convincing? I'm wondering if a cubic bezier with a better chosen middle control point would be better, but I haven't had a chance to try it yet. Cheers

##### Share on other sites
I doubt floating-point inaccuracies can would be large enough to be noticeable.

When you compute the initial velocity you can try to not use the "correct" parabola and instead use the discrete approximation provided by Euler's integration.

If you can post how you are computing the initial velocity now, I can try to modify it so Euler would do the right thing. I am assuming your time steps are constant; if that's not the case, what I suggest is not possible.

##### Share on other sites
Here's the code to calculate the launch velocity, I hope it makes sense:

			ImmutableVector2f startPos = enemy.getPosition();			ImmutableVector2f endPos = nextWaypoint.getPosition();			// First, find jump apex			final float highestY = Math.max(startPos.y(), endPos.y());			final float apexY = highestY + 100;						// Vertically, to apex			// t = sqrt( 2s / a )						final float s1 = apexY - startPos.y();			final float t1 = (float)Math.sqrt( (2 * s1) / -FallBehaviour.GRAVITY);						// What's our launch speed for this section?			final float vy = -FallBehaviour.GRAVITY * t1;						// Vertically from apex						final float s2 = apexY - endPos.y();			final float t2 = (float)Math.sqrt( (2 * s2) / -FallBehaviour.GRAVITY);						// Total time for entire jump			final float tTotal = t1 + t2;						// Horizontally			final float s3 = (endPos.x() - startPos.x());			final float vx = tTotal / s3;						brain.jump( new Vector2f(vx, vy) );

All values are in pixels for distance or ticks for time. Basically it calculates how long the up and down portions of the jump take on the y axis, which in total should be the total time for the jump. Then we calculate the correct x velocity to take use the required horizontal distance in this amount of time.

Any help would be appreciated, thanks.

##### Share on other sites
Quote:
 final float vx = tTotal / s3;

Shouldn't that be
final float vx = s3 / tTotal;
?

##### Share on other sites
Ahahaha, you're right - I have no idea how I missed that, thanks. [grin]

It only misses the target by about half a pixel now, so I think it's close enough to wait until the estimated time has elapsed and snap to the destination point.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015658
×