Jump to content
  • Advertisement
Sign in to follow this  

Pathing by using math concepts?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.

Edited by StubbornDuck

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!