## News:

*NEW* Current Version on STEAM16.793

Webchat:
Visit the chat

## Maths help for my game

#### FutureCow

Any takers on how to apply a sin wave on a vector?

Ie. Normally if your object was travelling down the screen you'd just add the sin value to your X coordinate, but what if you're not travelling horizontally or vertically?
I know I need to add the sin wave to the perpendicular of the vector, but I'm having trouble finding the formula written down without being in complicated maths language or expressed as a matrix.

So far I have
1) To work out coordinates based on an angle : x=cos (angle) and y=sin(angle)
2) Angle of a vector : angle=atan(ydifference,x difference)

Any takers?

Bonus points if you can explain to me why in code that uses trigonometry you see the code "normalize" the vectors all the time. What does the normalizing do? I think it just maps it to the unit circle (ie. range -1 to +1) - is that right?

#### Kitty Hello

let me think.
You have a direction vector x,y and want to move in that direction, but with a sin wave added?
first you need the current start angle for your sin wave and you must know that for the vector. Say... phi.

Now, starting with phi = 0 and vx=1, vy=-1, the thing moves up,right and will soon move right, then back right, down:

Now we need the flight-direction angle, I guess. Which would be flight direction - phi of sin wave for this vector.
something like:
psi = fmod(atan(vy, vx) - phi + 360), 360)

Now we need to increase phi for the next update:
inc phi, delta_time

and finally calculate a new flight direction, which is:
newpsi = psi + phi

now fly in that direction
vx = speed * cos(newpsi)
vy = speed * sin(newpsi)

Untested, but I think that should work.

#### Hemlos

Quote from: FutureCow on 2012-Jul-04
Ie. Normally if your object was travelling down the screen you'd just add the sin value to your X coordinate, but what if you're not travelling horizontally or vertically?

you multiply actually

Quote
I know I need to add the sin wave to the perpendicular of the vector, but I'm having trouble finding the formula written down without being in complicated maths language or expressed as a matrix.

I wrote a math tutorial in the math section:
http://www.glbasic.com/forum/index.php?topic=2241.0

Quote
So far I have
1) To work out coordinates based on an angle : x=cos (angle) and y=sin(angle)
2) Angle of a vector : angle=atan(ydifference,x difference)

Multiply the sin/cos by your movement size(distance)

Quote

Bonus points if you can explain to me why in code that uses trigonometry you see the code "normalize" the vectors all the time. What does the normalizing do? I think it just maps it to the unit circle (ie. range -1 to +1) - is that right?

You normalize, because anything times 1 is itself a distance.
Bing ChatGpt is pretty smart

#### FutureCow

Thanks guys, both for the help and the corrections!
I'll give it a go tonight and see if I can get it to work.

Cheers!