View Full Version : Simple(Or so I thought..) 2D math

DecoyLuke

04-25-2004, 06:45 PM

Ok, basically I'm making a small game that communicates over the network, now the network code works fine, but this is my first time programming in OpenGL.

I've got it setup where 4 rotates left, 6 rotates right, and 8 is up and 5 & 2 are down.

I can't seem to get the coordinate updates to work I can rotate just fine but when I press 8 to go forward it goes some random(or so it seems direction)

what is an easy way to solve this?

malancha

04-25-2004, 07:28 PM

When you rotate, you're also rotating your axes. Pressing 8 after a rotation will cause a forward motion with respect to your new axes, which is why you're not seeing the kind of motion you're expecting.

Try using quaternions for your rotations.

chowe6685

04-25-2004, 07:32 PM

there is absolutely no need for quaternions in this situation. Use sin and cos to decompose the angle and magnitude into x and y coordinates. So

X+=v*cos(angle)

Y+=v*sin(angle)

something like that

DecoyLuke

04-26-2004, 08:40 AM

Ok, So the axes rotate at with the same incrementation we use to rotate our object?

Right now we have it rotating + or - 10.0f.

The way we had it set up is basically like this to change x & y:

float nXPos, nYPos = 0;

//nMovement is 3 pixels

//this is for hitting '8'

if(fHeading >= 0.0f && fHeading =< 90.0f)

{

nXPos = nXPos + (nMovement * cos(fHeading));

nYPos = nYpos + (nMovement * sin(fHeading));

}

then from there we followed the rules of trig for different angles.

So if my axes also rotate how would I adjust for that in my math?

chowe6685

04-27-2004, 12:46 PM

you have it set up perfectly, your mistake is that sin and cos take arguments in radians and you are giving them in degrees. Just multiply by pi/180

Krisztian

05-18-2004, 11:12 PM

Also make sure the camera and projection are plugged correctly, sometimes it's easy to mix up the rotation with the controls, because of one misplaced ",". Just a precaution if it's not the math.

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.