### Author Topic: Maths help for my game  (Read 2181 times)

#### FutureCow

• HelpEditor
• Prof. Inline
• Posts: 680
##### Maths help for my game
« on: 2012-Jul-04 »
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

• code monkey
• Prof. Inline
• Posts: 10815
• here on my island the sea says 'hello'
##### Re: Maths help for my game
« Reply #1 on: 2012-Jul-04 »
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

• To boldy go where no pixel has gone before!
• Global Moderator
• Prof. Inline
• Posts: 1621
• Particle Hawk
##### Re: Maths help for my game
« Reply #2 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.
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

#### FutureCow

• HelpEditor
• Prof. Inline
• Posts: 680
##### Re: Maths help for my game
« Reply #3 on: 2012-Jul-05 »
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!