JackDigbug 182 Report post Posted October 28, 2013 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 Reference! :P Maybe I'm over complicating it, but any idea of how I would go about this? 0 Share this post Link to post Share on other sites
Cornstalks 7031 Report post Posted October 28, 2013 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. 1 Share this post Link to post Share on other sites
StubbornDuck 602 Report post Posted October 28, 2013 (edited) 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 October 29, 2013 by StubbornDuck 2 Share this post Link to post Share on other sites
JackDigbug 182 Report post Posted October 29, 2013 Thanks guys, worked out pretty well! 0 Share this post Link to post Share on other sites