PDA

View Full Version : Need a little help understanding rotation



Latheesan
05-04-2008, 12:26 AM
Hello,

I am working on a very simple game in C++ OpenGL GLUT and i came across this odd problem with rotating.

When my game begins, it looks like this :

http://img258.imageshack.us/img258/3212/95325995mc5.png

When i press the up key, it moves the cannon. I wrote a bit of code to copy the movement off my cannon onto my cannon ball, so it would move along with the cannon - staying inside the cannon mouth.

But what actually happens is that the ball moves in a faster motion than the actual speed my cannon is moving and as a result, the cannon ball gets out of line like this :

http://img380.imageshack.us/img380/9159/98738468ww1.png

This is the code i use to manipulate cannon and cannon ball movement using keyboard input :


/* Process Keyboard Input */
void Key(unsigned char key, int x, int y)
{
switch (key)
{
// Escape game
case 27:
exit(0);
break;

// Move Cannon Up :: Keyboard Key W
case 119:
if (ceil(cannon.GetAngleDegrees()) < 140) {
cannon.SetAngleDegrees(cannon.GetAngleDegrees() + 1);
cannonBall.SetPos(cannonBall.GetXPos() + cos(cannon.GetAngleRadians()), cannonBall.GetYPos() + sin(cannon.GetAngleRadians()));
}
break;

// Move Cannon Down :: Keyboard Key S
case 115:
if (ceil(cannon.GetAngleDegrees()) > 27) {
cannon.SetAngleDegrees(cannon.GetAngleDegrees() - 1);
cannonBall.SetPos(cannonBall.GetXPos() - cos(cannon.GetAngleRadians()), cannonBall.GetYPos() - sin(cannon.GetAngleRadians()));
}
break;

// Pause Game
case ' ':
glutIdleFunc(Idle);
break;

default:
break;
}
}

Where am i going wrong? Can someone please advice?

-NiCo-
05-04-2008, 12:32 AM
I think it should be:

cannonBall.SetPos(<u>cannon</u>.GetXPos() + cos(cannon.GetAngleRadians()), <u>cannon</u>.GetYPos() + sin(cannon.GetAngleRadians()));

Latheesan
05-04-2008, 12:33 AM
here's the demo of what i have so far, so you can see the problem your self : http://www.mediafire.com/?iz0ml0g3jvk

-- Edit --

Okay, thanks NiCo, i will try that out and see what happens

Latheesan
05-04-2008, 12:34 AM
after change it to like this :

cannonBall.SetPos(cannon.GetXPos() + cos(cannon.GetAngleRadians()), cannon.GetYPos() + sin(cannon.GetAngleRadians()));


It jumped to some random place and it stopped moving

http://pastebin.ca/1006744 (cannon.cpp)
http://pastebin.ca/1006745 (cannon.h)

http://pastebin.ca/1006747 (ball.cpp)
http://pastebin.ca/1006748 (ball.h)

Latheesan
05-04-2008, 02:12 AM
anyone?

-NiCo-
05-04-2008, 03:15 AM
I'm using linux, so I can't test your code...

cannon.GetXPos() and cannon.GetYPos() return the point of rotation of the cannon, right?

Latheesan
05-04-2008, 03:06 PM
hmm, does this give you the info u need?

http://pastebin.ca/1007414 (main.cpp)