Jagware

# Sinewaves

## Recommended Posts

Just curious if someone can give me the break down on using sinewaves for object position.

Mainly what im curious about is how to go about generating the sinewave table that code can be written to pull information from. (x and y axis).

Is there such a tool that makes it easy to create a pattern or wave and then output the coordinates easily?

##### Share on other sites

If you just want a basic table then this will do it :-

if you want high level language then in "C" its something like this :-

```#define SIN_NO_OF_POINTS 60
#define SIN_SCALE 100
#define SIN_OFFSET 20

for (int c=0; c<SIN_NO_OF_POINTS; c++)
{
signed long int val=SIN_OFFSET+(signed long int)(sin(angleInRadians)*SIN_SCALE);
//... Process val here.
}```

You'll need to include math.h to get the sine function. The value of PI might be defined in that header file too, if not you'll have to #define it yourself.

##### Share on other sites

Or there's a sine table already hardcoded into the DSP. It's not the most precise in the world, but you can interpolate between samples if you need higher precision.

##### Share on other sites
Or there's a sine table already hardcoded into the DSP. It's not the most precise in the world, but you can interpolate between samples if you need higher precision.

ahh thats right...... i totally forgot about that...

Thanks for the replies. ill have to mess around with it a bit and see what i come up with.

##### Share on other sites

as a lazy guy I always used this nice little tool for generating my sine table on demand

##### Share on other sites
ggn    0
Just curious if someone can give me the break down on using sinewaves for object position.

Usually it's not very practical to store the object's position using polar coordinates AKA rho,theta (ρ,θ) or at a stretch of the definition, sine waves. What is rather useful though is to use polar coordinates for expressing the movement vector or speed for X and Y axes in layman's terms.

Let's assume that you have an object at (100,200) and you want it to move with a speed of 5 pixels/frame 30 degrees north-east. The pair (5 pix/frame,30°) is exactly a representation of the movement vector in polar coordinates. If we were to add this every frame to the coordinates of the object (which are expressed in the Cartesian system), we'd get the desired motion. Since our object's position is expressed in Cartesian coordinates and we can't change the way the hardware stores coordinates, it follows that we have to convert the movement vector to the Cartesian system as well. We can do this relatively easy using the 2 following formulas:

Δx = r * cos(θ) = 5 * cos(30°)

Δy = r * sin(θ) = 5 * sin(30°)

If we were to add this pair of equations to our coordinates we'd get the coordinates of the object for the new frame, thus:

new_x= Δx + 100 = 5 * cos(30°) + 100

new_y= Δy + 100 = 5 * sin(30°) + 200

One final thing to notice is that actual Cartesian coordinates have the x axis pointing right and the y pointing up, but screen coordinates have the y axis pointing down (the pair of coordinates (0,0) is at the top left corner instead of the bottom left because of this), so if we were to add the above, the object would head to the bottom right instead of top right. So we have to adjust the 2nd equation like so:

new_y= -5 * sin(30°) + 200

And all will be well.

Apologies if that wasn't what you asked for and it's common knowledge for you, but this is what I understood you were asking in the first sentence of your question. Of course, if this is what you asked for and something's not clear in my answer, feel free to ask further .

##### Share on other sites
Just curious if someone can give me the break down on using sinewaves for object position.

Usually it's not very practical to store the object's position using polar coordinates AKA rho,theta (ρ,θ) or at a stretch of the definition, sine waves. What is rather useful though is to use polar coordinates for expressing the movement vector or speed for X and Y axes in layman's terms.

Let's assume that you have an object at (100,200) and you want it to move with a speed of 5 pixels/frame 30 degrees north-east. The pair (5 pix/frame,30°) is exactly a representation of the movement vector in polar coordinates. If we were to add this every frame to the coordinates of the object (which are expressed in the Cartesian system), we'd get the desired motion. Since our object's position is expressed in Cartesian coordinates and we can't change the way the hardware stores coordinates, it follows that we have to convert the movement vector to the Cartesian system as well. We can do this relatively easy using the 2 following formulas:

Δx = r * cos(θ) = 5 * cos(30°)

Δy = r * sin(θ) = 5 * sin(30°)

If we were to add this pair of equations to our coordinates we'd get the coordinates of the object for the new frame, thus:

new_x= Δx + 100 = 5 * cos(30°) + 100

new_y= Δy + 100 = 5 * sin(30°) + 200

One final thing to notice is that actual Cartesian coordinates have the x axis pointing right and the y pointing up, but screen coordinates have the y axis pointing down (the pair of coordinates (0,0) is at the top left corner instead of the bottom left because of this), so if we were to add the above, the object would head to the bottom right instead of top right. So we have to adjust the 2nd equation like so:

new_y= -5 * sin(30°) + 200

And all will be well.

Apologies if that wasn't what you asked for and it's common knowledge for you, but this is what I understood you were asking in the first sentence of your question. Of course, if this is what you asked for and something's not clear in my answer, feel free to ask further .

No that sums it up. i somewhat knew this already but was more inquiring about the process of creating the tables themselves so that i could write a routine to call to read the tables for x/y positions and have an object follow a pattern per say.

##### Share on other sites

It's probably a good idea to start with sub-pixel accuracy as well. saves things looking crap further down the line when its a pain to change things.

##### Share on other sites

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoticons maximum are allowed.