Pathing by using math concepts?

Started by
2 comments, last by JackDigbug 10 years, 5 months ago

So I'm a bit confused on something.

I'm a bit new to game programming, and I've never written a game that involved a level of AI.

I'm trying to figure out how I could make an object move across a sine wave, specifically

Wave.png Reference! :P

Maybe I'm over complicating it, but any idea of how I would go about this?

Jack @ That Naughty Panda

"Spooky Scary Mansion" For Wii U Coming Soon!

Advertisement

Simply by doing: y = amplitude * sin(x / (pi / 2 * wavelength))

Set amplitude to what you want, and wavelength to what you want. Set the x position, and then just calculate y based on that equation. The pi / 2 part assumes you're using radians; if you're using degrees it's just 180 / 2.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Disclaimer: I am not a "maths guy" and someone else might produce a better explanation.

Step 1: Figure out how the sine wave maps to your physical dimensions.

For instance, let's say we have a 2D sidescroller and you want the object to move like in that picture with +X = right and +Y = up. Then the direct equations are:

positionX = x + cX

positionY = amplitude * sin(x * 2 * pi / wavelength + phase) + cY

(cX, cY) is an arbitary start offset.

Now, as you can see, this expresses the positionY of the object as a function of x which can be either the x coordinate on the screen (in case you want to render it), or something else.

If you want to use time as the independent variable, you need to substitute:

x = speedX * time

positionX = speedX * time + cX

positionY = amplitude * sin(speedX * time * 2 * pi / wavelength + phase) + cY

The equations above are great if you can exactly position the object (rather than move it relative to some undefined position, as is more common in games). In that case, you to take the derivative of the above equations with respect to time to produce equations for velocity. Let's do that:

velocityX = speedX

velocityY = amplitude * cos(speedX * time * 2 * pi / wavelength + phase) * speedX * 2 * pi / wavelength

Then you can do integration with delta time in each frame, a basic (crude) way is:

positionX += velocityX * deltaTime

positionY += velocityY * deltaTime

In reality, you might not want to move the object exactly along the X-axis. In that case, you transform the velocity by a rotation matrix to make the object move in another direction.

Might also add that the complicated looking equation for velocityY can be simplified if you're satisfied with just tweaking values without needing to understand the units, i.e:

velocityY = A * cos(F * time + phase)

In this case, just find good looking values for A, F and phase.

Thanks guys, worked out pretty well!

Jack @ That Naughty Panda

"Spooky Scary Mansion" For Wii U Coming Soon!

This topic is closed to new replies.

Advertisement