# Python sine/cosine problem.

## Recommended Posts

On a calculator in degree mode: cos(0)/cos(360) returns 1 cos(90) and cos(270) returns 0 cos(180) returns -1. sin() does pretty close to the opposite. For some reason im not getting these numbers when using the equivolent in python. The docs say cos(x) gives an answer in radians...that should be right as far as I can tell but the numbers im getting are all over the place. Am I doing something wrong or just not using it properly? Any help would be appreciated.

##### Share on other sites
WOOT! NM I figured it out. Have to convert x to radians first then find the cos()/sin().

Thx anyways.

##### Share on other sites
NM...that wasnt it...hmmm....

##### Share on other sites
If the numbers you get are:

cos(0)/cos(360) returns 1/-0.283691
cos(90) and cos(270) returns -0.448074/0.984382
cos(180) returns -0.59846.

Then it's working fine. Translated to radians:

cos(0)/cos(2*pi) returns 1
cos(.5*pi) and cos(1.5*pi) returns 0
cos(pi) returns -1.

FYI, cos doesn't return a value in radians, its argument is in radians. To convert degrees to radians, multiply by pi/180 (0.0174532925...)

[Edited by - smart_idiot on May 28, 2005 1:30:57 PM]

##### Share on other sites
Thats great and all...let my try to explain what im looking for and maybe that will help...

I have an object that I want to move on an x,y cordinate system based on the direction its facing. The direction its facing is based on north which is 0/360. So if the object is facing 180 degrees it will be moving south.

If cos(x) and sin(x) (x being the direction being faced) returned the numbers im looking for it would be really easy to move the object in the cord system. If it returned what my calculator does it would work. but its not.

Thats what stumping me. I dont know why I cant seem to wrap my brain around this...maybe im tired...it is midnight here.

So any help with this would help.

##### Share on other sites
Make sure your calculator is set to use radians, or try your math with Google. Also, I'm not sure what you're doing for graphics, but when dealing with pixels and bitmaps the origin is at the top left of the screen and increasing y moves you down. That might screw you up if you were expecting positive y to go up.

##### Share on other sites
I do the same thing in one of my python programs... Here you go:

distance = elapsed_time * velocitydirection = degrees * math.pi / 180x = math.cos(direction) * distance y = math.sin(direction) * distancenewx = oldx + xnewy = oldy + y

It's just a simplified 2d rotation matrix.

##### Share on other sites
accually this has nothing to do with graphics. just a theoretical grid with 0,0 at the center.

Ok trying what you just put...gimme a sec.

##### Share on other sites
Thx SMR...thats exactly what im looking for. You too Smart_Idiot.

##### Share on other sites
MUAHAHAH!!! I love it when code finaly works.

Thx again to the both of you.

##### Share on other sites
Ok so ive got it to work perfectly with just X,Y but am having trouble with X, Y, Z.

right now I have:

PI = pi/180

x1 += sin(angle1 * PI) * 1
y1 += cos(angle1 * PI) * 1
z1 += cos(angle2 * PI) * 1

basicly angle1 and angle2 are the horizontal and verticle angles the object has to face to move in the right direction. The way I have it now works some of the time but not always. What am I doing wrong here?

##### Share on other sites
I think the bits you're adding to x and y need to be multiplied by sin(angle2).

##### Share on other sites
Quote:
 Original post by Cannibal_CoderOk so ive got it to work perfectly with just X,Y but am having trouble with X, Y, Z.right now I have:PI = pi/180x1 += sin(angle1 * PI) * 1y1 += cos(angle1 * PI) * 1z1 += cos(angle2 * PI) * 1basicly angle1 and angle2 are the horizontal and verticle angles the object has to face to move in the right direction. The way I have it now works some of the time but not always. What am I doing wrong here?

The matrix changes completely when you do this in the third dimension:

(I took this from a gamdev.net article)

x'=z*sin(yaw)+x*cos(yaw)
y'=y
z'=z*cos(yaw)-x*sin(yaw)

x"=x'
y"=y'*cos(pitch)-z'*sin(pitch)
z"=y'*sin(pitch)+z'*cos(pitch)

// I only used this last part.
x"'=y"*sin(roll)+x"*cos(roll)
y"'=y"*cos(roll)-x"*sin(roll)
z"'=z"

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628312
• Total Posts
2981998

• 9
• 9
• 13
• 11
• 13