Advertisement Jump to content
Sign in to follow this  
Waylander

Need help with 2D oscillation

This topic is 1756 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

I'm working in XNA but my problem is the math so I posted here.

 

FYI I am very new to this kind of math and I've been studying hard.  I am to the point where I "think" I can solve this myself but once I try to work it out, I draw a blank.

 

Simply put, I am wanting sprites to oscillate (sine/cosine?) in a wave-like pattern along their vector.  I've found several options that work with going along in a straight line along the X axis and I believe I understand how those work.  However, my sprites aren't moving straight along the X axis and unfortunately I haven't found anything that is specific to what I'm after.  Their vector can vary in basically any direction.  What I don't know how to do is take the information from their vector and use it to create the oscillation I am after.

 

Any ideas?

Share this post


Link to post
Share on other sites
Advertisement

You don't provide much detail, but it can be as simple as choosing a 2D point and a direction.

 

Position2D = sin(angle) * magnitude * vector2DDirection + point2D

 

Works in any number of dimensions. Pick a magnitude for the maximum displacement from the point. Because sin(angle) will vary from -1 to +1, the position will vary by -magnitude to +magnitude (provided that vector2D is a unit vector) along the direction either "side" of the point.

 

E.g., a direction vector = ( 0.707, 0.707 ) will result in an oscillation along the 45 degree line through the point.

 

You could use cos(angle) instead. That will produce the same oscillation, but just be out of phase with a sine based oscillation.

 

In time-based terms,

 

P(t) = P0 + L*sin(w*t)*Vdir

 

w is an angular frequency constant (or variable if you want) in units of radians/sec. t is time. L is the maximum distance from P0. Vdir is a unit direction vector.

Edited by Buckeye

Share this post


Link to post
Share on other sites
For horizontal movement you are probably doing
x = x0 + speed * t
y = y0 + oscillation_amplitude * sin(t * oscillation_frequency)

You can think of this in vector notation as this single equation:
(x,y) = (x0,y0) + (speed,0) * t + (0,1) * oscillation_amplitude * sin(t * oscillation_frequency)

Here (x0,y0) is the initial position, (speed,0) is the velocity and (0,1) is the direction of oscillation, which is perpendicular to the velocity. So to do this in general, you can replace (speed,0) with the arbitrary velocity (v.x,v.y), and (0,1) with a vector that is perpendicular to (v.x,v.y) and has length 1. You can compute a vector perpendicular to (v.x,v.y) as (-v.y,v.x), and you can divide it by its length to make it have length 1; let's call the result (perp.x,perp.y). Then the formula is just
(x,y) = (x0,y0) + (v.x,v.y) * t + (perp.x,perp.y) * oscillation_amplitude * sin(t * oscillation_frequency)

Share this post


Link to post
Share on other sites

This is where it gets fuzzy for me (and also where I am getting used to the terminology).  I'm going to play around with this for a bit and see what I come up with.  Thanks guys for getting me started :).

Share this post


Link to post
Share on other sites

Ok, I've been tinkering with this and unfortunately haven't gotten anywhere.  There are a few things I don't understand.  But I'll start with what I'm trying to do and hopefully it will help.

 

Essentially what I am doing is having sprites randomly spawn off screen (any side) and having them emerge onscreen, race across the screen, and then leave the screen.

 

Buckeye, what would the magnitude be?  An integer?  Is the vector2DDirection my sprite's speed?  And what is the point2D?  Is that the "pick a point" you talked about?

 

Alvaro, I "think" I am close to implementing your final formula but I am stuck on your oscillation_amplitude and oscillation_frequency.  What are those?  Other vectors?

 

And to both of you, how are you using t (time) in here?  I don't know how to implement that.

 

Again, thanks for your posts.  I get the feeling that I am on the cusp of understanding this stuff since I am moving from textbook stuff to actual implementation...which makes this all the more frustrating! :)

Share this post


Link to post
Share on other sites
I am using "amplitude" and "frequency" in a very standard manner. These are common words in describing waves and you should have learned about them in high school or middle school. If you don't know what they are, try Wikipedia.

Perhaps you can tell us in detail how you implement oscillation for objects moving along the x axis, so we can see where you are and try to get you from there to where you need to go.

Share this post


Link to post
Share on other sites

What I am trying to understand is how they (amplitude and frequency) are used/implemented in code (specifically the code you posted).

Share this post


Link to post
Share on other sites
What I posted is basically the code: The amplitude multiplies the sine, and the frequency times the time variable goes inside the sine.

Share this post


Link to post
Share on other sites


I am wanting sprites to oscillate (sine/cosine?) in a wave-like pattern along their vector.


But I'll start with what I'm trying to do and hopefully it will help.
Essentially what I am doing is having sprites randomly spawn off screen (any side) and having them emerge onscreen, race across the screen, and then leave the screen.

It's no longer clear what you're asking about, I'm afraid. Oscillating and racing across the screen are two different things.

 

Maybe the first thing to ask is: do you understand vectors?

 

Second: Can you describe in clearer terms, or even provide a sketch or picture of the motion you're trying to achieve?

Share this post


Link to post
Share on other sites


What I am trying to understand is how they (amplitude and frequency) are used/implemented in code (specifically the code you posted).

 

When you're not sure what equation you should be using to describe some motion or are unsure what the variables represent, it's a good habit to do a summary dimensional analysis on the equation, using the simple rules below:

- dimensions multiply and divide as usual

- you cannot add or subtract different dimensions

- transcendental functions are dimensionless

 

Using this on the equation Alvaro posted, you can see that the frequency (as inverse time, i.e. in Hz) is multiplied with the time variable to give a dimensionless value, the sine of that is dimensionless and is then multiplied with amplitude as a (peak) displacement, giving a result as a measure of displacement, which checks out and is what you wanted. Using the same reasoning you can deduce that the frequency cannot be a vector (unless time is a vector too, which would imply that your x and y coordinates are subject to different times, which is probably not what you want), and so on. As you can see this gives a quick way to check what units a variable should be, whether a physics equation "makes sense", and is also handy to verify that you didn't make an implementation/logic error somewhere.

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!