PDA

View Full Version : Sine wave at an angle?



Joe123
10-02-2008, 12:12 PM
At the moment, I have a movement routine for an enemy worked out to follow a straight line between two points, using y = mx +c.

I want to make it also move in a sine wave sort of pattern, but along that line...

I can only think of how to make things move in a sine wave along a horizontal or vertical line though.

Anyone have any ideas as to how I'd go about doing this?

Saffith
10-02-2008, 04:09 PM
Just think of the wave movement as an offset along a line perpendicular to the direction you're moving. Something like this:


while(true)
{
// Undo previous frame's offset
this->X-=offset*Cos(angle+90);
this->Y-=offset*Sin(angle+90);

// Move along the overall angle
this->X+=speed*Cos(angle);
this->Y+=speed*Sin(angle);

// Find the offset from the straight line
offset=amplitude*Sin(i);

// Offset movement
this->X+=offset*Cos(angle+90);
this->Y+=offset*Sin(angle+90);

i=(i+1)%360;
Waitframe();
}

Joe123
10-03-2008, 01:10 PM
I shall...

...have a look and try to decipher that.

Thanks ^_^

Joe123
10-04-2008, 10:28 AM
Oh, I had it moving using y=mx+c to move it along the line, but this uses sine and cosine to do that, doesn't it?

Hrm ok.



EDIT: With a little fiddling, and a complete conversion to radians it now works ^_^
Thanks.