Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Sine Waves

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

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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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);

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!