Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sine Waves

Started by sakky Dec 4, 2002 at 5:46 PM 4 replies 1.1k views
Original Post
sakky
sakky
Can some one give a code snipplet of making a sine wave in C. I have done this in VB but I am running into big problems trying to get ito work in C. Many thanx to the kind person that leaves me there C code.
Take back the internet with the most awsome browser around, FireFox
Hairybudda
Hairybudda
Making? Like, filling a data structure? Or actually drawing it?
st0ned
Thunder_Hawk
Thunder_Hawk
Don''t forget that the sin() function uses radians and not degrees
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
sakky
sakky
LOOK, I know that C needs sertain conversions right? I think I need to convert something to radians. I''m lossing track here for somee reason. Different data types effect things. I was using this formula.

Frequency = ( 2 * Pi * Herz(440) ) / Sample Rate( 22,000 )
Y = Amplitude * ( sin( Frequency * Ticks))

The amplitude is low because the sine waves produced are used for a particle engine. So I need the actual coords from the sine wave signature. I make the particle move in the shape of a sine wave. I then run checks to see if any other particles are around. If there are close enought then both the particles waves are combined (additive synthesis). If they are really, really close then some phasing or modulation of the particles waves are added to them selfs along with the additive synthesis of the to neighboring particles.

So I need to know how to make sine waves in C. Because I can''t get my VB formula to work right.
Take back the internet with the most awsome browser around, FireFox
sakky
sakky
quote:
Original post by Thunder_Hawk
Don''t forget that the sin() function uses radians and not degrees


You must have posted when I was typing my reply. I knew this but I''m confused and I lost track, please help! Is this right:

// 2 pi''s
for (time = 0; time < 360; i++)
{
radian = y * pi / 180.0f;
x = sin(radian) * 5; // amplitude
}
Take back the internet with the most awsome browser around, FireFox
freshya
freshya
If you think about the code you just posted, you will see that it isn''t really doing anything useful. The loop iterates 360 times, and at each iteration, it sets a variable x to 5 times the value of a trigonometric expression. However, this all happens inside the loop, so by the time the loop is over, x is set to the last value, in this case 0 (because sin (359) ~= sin(2 pi) = 0).

What I''m guessing you want to do, is each *frame*, set x to a different value, something like this:


// our definition of 2pi ... there''s more but I don''t know it...
#define TWO_PI 6.2831f

// the current time, in milliseconds
unsigned int tick = timeGetTime();

// get ourselves a value within the range (0..2pi]
// alter the divide by 1000 to alter the period (speed) of the wave
float theta = tick / 1000.0f;
while (theta > TWO_PI) theta -= TWO_PI;

// calculate ''x'' for this frame
x = 5 * sinf(theta);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.