# moving in a sin wave

This topic is 2933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm working on a simple racing game in which I am trying to get my car to follow a sin curve while moving in its current heading. The only way I can think of achieving this is by adding a sine wave multiplied by some scalar to the steering rotation. This works sometimes but sometimes overturns and causes the jeep to get stuck in a circle(sometimes infinite). Also, it gets stuck in a circle when it hits a wall too. Here is how I'm doing it, does anyone know how I can fix/improve it ? static float scalar = 10,delay = 0,input= 0; if((delay += dt) > .01f){ delay = 0; input += dt; SetFinalHeading(rotation.y + sin(input) * scalar); } thank you!

##### Share on other sites
How do you know this is the code that's causing the problem?

Your posted code appears to be okay, assuming:

1. rotation.y never changes ( or is "under control" )

2. the function is called with a valid dt. If dt==0 the heading will never change. How do you calculate dt? Do you always call this routine, even if the vehicle hits an obstacle, etc.?

3. your sin() function returns valid values for high values of angle.

It likely the problem is one of the above 3.
Taking care of 3 is easy:
input += dt;if( input > pi/2 ) input -= pi/2;

I'm guessing under certain circumstances this routine is not called, or is called with an invalid dt value.

##### Share on other sites
Aah, I think the problem would be number 1 then. Basically the jeep is following a pathfinder which sometimes causes it to turn. It falls into a circle when the sine wave is applies and it is already turning on its own.

Perhaps, I need make it follow the sinwave only when its moving in a straight line ? any suggestions ?

##### Share on other sites
Quote:
 Perhaps, I need make it follow the sinwave only when its moving in a straight line ?

Without seeing any code, I couldn't say. But it sounds like the pathfinder and the routine you posted may be fighting one another. If you have 2 independent functions that both change the direction, that should probably be changed, particularly if the pathfinder is trying to find a proper direction based on the current direction which is constantly changing due to the sinewave function.