DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: game-making tool questions

1. Senior Member
Join Date
Dec 2003
Posts
3,366
Sin and cos are opposites, and this is true:
sin(direction) * speed = vertical speed and
cos(direction) * speed = the horiz speed

and since this is right triangle stuff, and since 3 angles in a triangle add up to 180,
dir2 must be 90 - direction (but your quadrants are wrong, use atan2).

atan2 is a nice arc(inverse) tangent that gives you the correct quadrant! read the help on that one.

NOTE THAT ALL BUILT IN TRIG IS IN RADIANS!!! 180 degrees = 3.1415926535897932384626433832795 radians.

2. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
ahh, so I don't need to figure the angle dir2, I just use cos() with direction? cool. okay, so how do I convert radians to degrees? I don't get what you mean with the whole quadrant atan2 thing either. sorry I keep asking questions and everything.
Last edited by Algorien; 04-06-2005 at 07:40 PM.

3. Senior Member
Join Date
Dec 2003
Posts
3,366
its ok lol... its refreshing to have a younger person who wants to learn.

Its really simple to convert:
degrees * PI/180 = radians,
and
radians * 180/PI = degrees. I supplied an industrial strength pi in my last post.

atan2 would be a good start for your learning to use the built in help/man pages / whatever you have available.

atan2 calculates the arctangent of y/x. atan2 is well defined for every point other than the origin, even if x equals 0 and y does not equal 0. So atan(opposite/adjacent) = atan(vert/horiz) = your angle, in radians, so long as its not (0,0) for x,y. Give it a try...

4. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
Okay, I think i get it. to find an angle's measurement, you use atan2(opposite/adjacent)? That sounds like it'll be helpful for my next question. I used my wierd sin() and cos() method of turning a direction and speed into a vspeed and hspeed, but now I want to rotate the images. You see, the way my images work is that they're pretty much vector graphics, except filled in. so I figured out to rotate a shape, I would have to do something to find the initial angle of ech point from 0*, add that to the argument and then use sines and cosines to find the new point, i'm pretty sure I can do the new point with either:

//where newX is the x after rotating, dist is the point's initial distance from the origin which must be maintained during rotation and xdist x of the initial point
newX = sin(degrees) * dist;
newY = cos(degrees) * dist;

or maybe

newX = sin(degrees) * xdist;
newY = cos(degrees) * ydist;

So if you could tell me which of these, if any, is right, that'd be great. And also(sorry, I never seem to run out of questions, but you have been REALLY helpful to me), one of the problems with my code earlier was that I needed to find the points initial angle from 0*, could I use

atan2(pointx/pointy);

?

5. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
PS. should I start a new thread with this question, like because there's too mush in this one or something? I'm not completely sure about the exact rules here. sorry again.

6. Senior Member
Join Date
Dec 2003
Posts
3,366
Keep it in one thread, in my opinion, so readers can follow it. However, if you continue along the math instead of C questions line, you may want to private message me instead and we can work it out.

TRY to read this: (its a little advanced for your age, but its what you NEED)

http://www.makegames.com/3drotation/

Sin and cos break vectors (basically, a line segment that starts at 0,0 and goes to x,y) into pieces, the x piece and the y piece. (Its one of many ways to think of the trig functions, and one of the simplest). This relates to the triangles too, the shorter legs, when added, produce the hypotenuse. You add vectors by putting the tail of one onto the head of the other, and draw the line between them. There are illustrations on the web, but I cant draw one here easily. Wrap your head around that relationship!

dist = ( vector sum of the xpart and the ypart)
= sqrt(xpart*xpart + ypart*ypart)
= ypart/ sin(a)
= xpart/cos(a)
and so on...

also:
sin(angle) = ypart/dist
cos(angle) = xpart/dist
tan(angle) = ypart/xpart (not straight up, in 3-d when this happens you use the quaternions)

so...
sin(a) * xdist = ypart/dist * xdist = nothing terribly useful!!
sin(a) * dist = ypart/dist * dist = ypart !!!!

so I don't think either of those equations is what you wanted, since the top set is not too useful and the bottom set is redundant.

To rotate a single point, then, you know its angle
atan2(ycoord/xcoord)
and you know the amount of rotation,
and you know how far it is from the origin sqrt(x*x + y*y)

new x = cos(rotate + atan2) * dist
new y = sin(rotate + atan2) * dist
new dist = old dist

7. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
I see. I'm gonna try that now! Thanks. Oh, but does atan2 return in radians?
Last edited by Algorien; 04-08-2005 at 09:51 PM.

8. Senior Member
Join Date
Dec 2003
Posts
3,366
ALL the trig functions WORK ON and RETURN radians. They will accept any double, but will not work correctly for degrees.

9. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
so what's wrong with

void rotate_points( int shapeNum, float degrees )
{
degrees = (PI/180) * degrees;
//convert the degrees in the argument to radians
int pointsRotated;
//I don't like for() loops, so I use this setup(initializer, while(){increment})
while(pointsRotated < shape_numOfSides[shapeNum])
{
float dist = sqrt((itemVertex[shapeNum][pointsRotated][0] * itemVertex[shapeNum][pointsRotated][0]) + (itemVertex[shapeNum][pointsRotated][1] * itemVertex[shapeNum][pointsRotated][1]));
//that calculated the distance from (0,0). itemVertex[shape number][vertex number][x/y] is the array within cItem that stores the loaded shape.
float point_atan = atan2(itemVertex[shapeNum][pointsRotated][1] , itemVertex[shapeNum][pointsRotated][0]);
//that calculates the initial angle of the point from (0,0)
itemVertex[shapeNum][pointsRotated][0] = cos(degrees + point_atan) * dist;
//that rotates the x-coordinate by the argument angle and the initial angle and the next line does the same for the y
itemVertex[shapeNum][pointsRotated][1] = sin(degrees + point_atan) * dist;

//increment
pointsRotated += 1;
}
}

called with

//tester is an instance of cItem that I use to test the engine
tester.rotate_points( 0, 1);

?? I must have missed something
Last edited by Algorien; 04-09-2005 at 12:40 PM.

10. Senior Member
Join Date
Dec 2003
Posts
3,366
180 is an int. 180.0 must be used or it rounds off to 0.0
see if that is the problem. if not I will look again.

11. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
That didn't work. I'm not sure where the problem is.
Should I start using private messages now?

12. Senior Member
Join Date
Dec 2003
Posts
3,366
Print the angle in degrees, the x and y of the point, dist, and the new points, all for a single point. I don't see anything wrong, but its midnight and stuff.

13. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
lol. oh, wow, it is. So late I didn't understand what you requested. sorry.

edit: when you said to print all those values, did you mean draw them on the screen so I can keep track of them or show them to you?
Last edited by Algorien; 04-09-2005 at 12:47 AM.

14. Senior Member
Join Date
Dec 2003
Posts
3,366
Well, I could use a hint as to what its doing...

Oh, and you are not trying to do a rotation about some point that is not the origin, are you? You have to move it to the orig, rotate, and move it back for that. I think you can do it all with one matrix multiply, but you have to do a lot of trig to set it up for speed. What I was showing was just a rotate about the orig, so it may be doing what its supposed to.
Last edited by jonnin; 04-09-2005 at 12:34 PM.

15. Registered User
Join Date
Apr 2004
Location
Pennsylvania
Posts
72
I put most of the values in comments above, and the code is trying to rotate about (0,0). Maybe I should e-mail you all my source so you can look at it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source