Game Development Community

Sine wave weirdness

by Simon Love · in Torque Game Builder · 06/05/2005 (6:50 pm) · 4 replies

Greetings everyone,

I have tried, and succeeded at assigning a simple sine wave movement to a particle Emitter's position.
The effect is pretty cool by itself and works like a charm if I keep it at ONE specific frequency.

In other words, everything works fine, until I dynamically change the frequency.

Whenever the Frequency (%B) equals 4,8,19,21,23,26,30,33,39,40 or 67 (Probably larger values but I was screaming obscenities at this point, which makes my brain forget how to count properly) , the loop just goes on.

I have traced the code, and %x eventually equals the %period (1.57 if the frequency is 4),
yet the if (%x == %period) never gets called. I have console readouts where %x = 1.57 and %period=1.57 ... but the code simply refuses to acknowledge this fact!

I reset %x every time it equals the period, but I have tried a function where I don't reset %x...Same result.

Maybe I'm overlooking something simple, maybe I just spent too much time staring...Point is, I need help. :)

Here is my function where:

%x is the x coordinate on the 'graph'
%y is the y coordinate on the 'graph'
%A is the amplitude of the wave
%B is the frequency of the wave

$Pi was defined as 3.1415

function SineUpdate(%x, %A, %B)
{

%y= %A * msin(%B*%x); //sine wave formula
%y=mfloatlength(%y,2); 

%x=%x+0.01;

%periodA = (2*$Pi)/%B; 
%period = mfloatlength(%periodA,2);

if (%x == %period)
{
%x=0;
%B=%B+1; 
}

%Myliege=Liege.getobject(0);
%Myliege.setpositionY(%y);

schedule (10,0,"SineUpdate",%x, %A, %B);
}

The X value of the graph is modified, but as you can see, the X position of the emitter doesn't, the bugger simply goes up and down as the particles drift to the right.


Yes...my particle emitter is called %MyLiege. I'm strange like that.

Could someone please help me out, as this is pushing me slowly towards insanity.
Hope I have included everything that could help my future savior, if not, just let me know!!!

About the author

I am here to help. I've worked at every imaginable position in game development, having entered the field originally as an audio guy.


#1
06/05/2005 (7:14 pm)
Try throwing echo statements in like this (were your previous tests the same ?)

function SineUpdate(%x, %A, %B)
{

%y= %A * msin(%B*%x); //sine wave formula
%y=mfloatlength(%y,2); 

%x=%x+0.01;

%periodA = (2*$Pi)/%B; 
%period = mfloatlength(%periodA,2);

echo(x = " @ %x SPC "period = " @ %period);

if (%x == %period)
{
echo("--- x and period are equal ---");
%x=0;
%B=%B+1; 
}

%Myliege=Liege.getobject(0);
%Myliege.setpositionY(%y);

schedule (10,0,"SineUpdate",%x, %A, %B);
}

if it still shows them as being equal and not completing the if statement try setting the if statement up like this

if (%x $= %period)
#2
06/05/2005 (7:27 pm)
It works!!!!

Thank you, King Tut. I have seen you help people a few times over the past few months...But having the privilege to be helped in such a quick and precise way...unbelievable!!! Thanx a bunch!!

I have a question, though. isn't the $= used to compare strings? How does it apply to floating point numbers? Why does it see the equality when == doesn't? I'm curious.

A small note just to close this thread up nice and tight, I had to force %x to have two decimals after the comma ( %x=mfloatlength(%x,2); ), or else when period would equal 0.90, %x would simply be 0.9 and would not recognize them as equals. It really works great now!
#3
06/06/2005 (1:34 am)
I don't know if this is the cause of your problem but comparisons involving floating point numbers are often problematical due to rounding errors (or limits in internal accuracy). A common way around this is to compare to a (very small) range e.g. using your code variables
if( mAbs( %x - %period ) ) < 0.00001 )
{
     // etc....
}
or the long version (I think mAbs() returns a string and I'm not sure if that will cause problems)
%diff = %x - %period;
if( %diff > -0.00001 && %diff < 0.00001 )
{
     // etc....
}

Obviously by varying the value of 0.00001 the accuracy of the comparison can be controlled.

I suspect that there is some rounding going on when using the string comparison that doesn't happen when comparing numerics.

e.g. When I try this in the console
$x = 0.7999999;
echo( $x );
echo( $x - 0.0000005);
I get values of 0.8 and 0.799999 respectively. So, while the value of 0.7999999 is stored accurately in $x, it is rounded to 6 d.p. for display
#4
06/06/2005 (5:40 pm)
@Brian : I actually had that feature 'implemented' before the weirdness occurred. I just ended up rounding up everything to 2 decimals, and it works without a hitch. It's still a good thing to have on the forums for future reference! Thanx!