Sinus and Cosinus in games - help a guy?

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Sinus and Cosinus in games - help a guy?

Post by dandi8 »

Hi!
Could someone please explain to me or give me a link explaining how sinus and cosinus are used in games? Definitions, pictures etc. I'm mainly talking about this:

Code: Select all

px = sin(double (proty * irr::core::DEGTORAD)) * playerspeed; 
pz = cos(double (proty * irr::core::DEGTORAD)) * playerspeed; 
PlayerMdl->setPosition(PlayerMdl->getPosition() + vector3df(px,0,pz));
(code for moving player forward)

Best thing would be if someone could make a paint picture showing the XY grid and px,py,prot(player rotation) as that would help me better understand the concept.

I'm writing this plead for help because, having read a few articles about sinus and cosinus and asked people in my surroundings to explain it to me, I failed miserably and I don't want to wait until university for this to be explained to me. Will anyone help me?

P.S.
I'm also not sure what a DEGTORAD is doing there.
If it exists, it can be broken.
wITTus
Posts: 167
Joined: Tue Jun 24, 2008 7:41 pm
Location: Germany

Post by wITTus »

deg2rad is like celsius to fahrenheit :lol:

sine and cosine are used for lots of cool things. in your player movement example it is probably used for faking the walk movement. (the players cam is moved up and down and up etc. with each step). like so:

Image

well. they are important mathematical functions. just play around with sin() and cos() and you'll see what it gives.
Generated Documentation for BlindSide's irrNetLite.
"When I heard birds chirping, I knew I didn't have much time left before my mind would go." - clinko
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

No, the code I posted is for literally moving the player forward. Change proty to (proty+90) and you would have strafing. The X and Y are 2D, not 3D (perhaps that's why you thought that it's for faking the cam movement). Notice that it's the PlayerMdl (player model) that gets moved, not the camera (which would be cam).

Thanks for the explanation of DEGTORAD, it really makes it all clearer :). Could you also tell me the difference between deg and rad? Why is there a need for conversion?

And ofc I still don't understand sin and cos from my example.
If it exists, it can be broken.
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

It makes the player move forward in a certain direction. So if proty was 45, it would move diagonally.

You might want to get a Geometry or Trig book and find the sections on Sines + Consines and triangles. The code in the most basic sense is making little right triangles with the the angle "proty" at your player. (px and pz represent the x and y coords of the point at the other end of the hypotnuse = where the player needs to go.) You can scale the triangle using playerspeed to make him travel further.

DEGTORAD is used because sin and cos typically use radians, it just converts degrees to radians.

Also note that px and pz are added to the players position. This is because the points you get are basically realitive to the player and not the world/its parent.

~DtD
PS> I don't understand this stuff that well, so I hope this was correct and not confusing!
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Thanks!

Although... Could maybe make a paint picture? XD It would be that much easier for me to understand :)

The "why" in why I can't understand it is because I imagine that SIN and COS must be able to be represented as lines or points somewhere on the XY grid and I would like to know where. So a picture would REALLY help.
If it exists, it can be broken.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

sin() and cos() aren't just lines on a 2d graph. They are trigonometric functions. They are tools that help solve problems involving triangles. A typical problem of this type would be to find the x and y components of a line that has been rotated some number of degrees around an axis. This is exactly what the above code is doing.

Travis
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Could you explain this to me in more detail (step by step) or with a picture?
If it exists, it can be broken.
mk.1
Posts: 76
Joined: Wed Oct 10, 2007 7:37 pm

Post by mk.1 »

wikipedia ftw

Really, you should at least try to find something.
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

The problem with wikipedia, my offensive friend, is that it has way too much information for me and I get lost the second I try to read something about sinuses there. It's just that "The cosine of an angle is the ratio of the length of the adjacent side to the length of the hypotenuse" doesn't really answer my question about moving and strafing.

I come here asking for knowledge and you send me away saying "go read the books".
If it exists, it can be broken.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Actually, it does. The problem is that you don't understand some other concepts on which sin and cos are built. Hence, you need to dig further. You will not understand how to use an integral properly until you learn to use derivation. The same way, sin, cos, tan, and their inverse functions can hardly be understood if you don't understand the basis of trigonometry, as in, a right triangle and it's hypothenuse. Once you understand that bit, just place a triangle in a circle, the hypothenuse being also the radius and the right angle away from the center. From there, draw the angle of the corner of the triangle touching the center of the circle. That angle is special and is linked to the three sides of the triangle through sin, cos and tan.
dandi8
Posts: 45
Joined: Thu Feb 14, 2008 2:01 pm

Post by dandi8 »

Thx, man. I'm sure I'll understand that tomorrow... Which is today as it's 04:22 (...just noticed) XD I'll have a look at what you wrote tomorrow/today, seems like you explained it in a pretty clear way (at least the sin/cos/tan stuff utter basics) :)
If it exists, it can be broken.
Kaeles
Posts: 37
Joined: Thu Jun 03, 2004 12:43 am
Location: oklahoma
Contact:

Post by Kaeles »

Basically, whats its doing is changing the amount that the player moves on the X and Z axis due to its rotation.

So, if you were standing on a clock, looking at the 12 oclock mark (90 degrees or pi/2 radians) , then sin is going to give you 1 and cos 0, so in that example, so, on the x axis your going to move 1 unit, and going to move 0 on the Z axis.

What this does is allow movement to be based on which "direction" the model is "facing".

So when you are looking at the 3 oclock mark, you are at 0 degrees, and it increases going counter clockwise, (thats how most "unit circles" are shown).

So if you understand what the trig function is giving you back, then you multiply that by the player speed so you move so many units on each axis.

so, at
0 degrees cos = 1,sin = 0
45 degrees cos = root2/2, sin = root2/2
90 degrees cos = 0, sin = 1
135 degrees cos = -root2/2, sin = root2/2


so, assuming player speed = 1
when "facing" 0 degrees you go forward 1 unit, and 0 units to either side
at 45 degrees you go root2/2 units forward and root2/2 units left.
and so on.

Hopefully this helps a bit.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Personally I think the easiest way to understand all that stuff is by learning it in the unit circle. Because in the unit circle it's defined all geometrically so it's possible to visualize all values. I recommend to google for an explanation using the unit circle and once you understood that one you will always know which leading signs to use without having to remember it, why each angle has a sinus value and why you also have a radiant value for each angle. And if you follow the values of sin for each x in there you will see that those form these nice waves and why they repeat every 2*PI. And as a unit circle has a radius of 1 it is easy to see that it's never possible for sin to be larger than that. And as you are already in a circle you might even notice why having a sinus and cosinus are really nice things to have if you want to rotate persons in a game.

Only thing you really have to remember is which one is sinus and which is cosinus. My learning aid for that was to know that sinus is the latin word for the cord in a bow. So you know which way people hold bows you know which line is used for sinus.

Later you can read up on other ways to define and calculate them (that opposite leg/hypotenuse stuff in perpendicular triangles, but don't worry - you'll get the same values out of that than in a unit circle). Maybe even look up some formulas which allow you to put these things to some work. But for understanding it - all you need is that unit circle - no calculations needed at all.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply