Rotation

Started by
8 comments, last by medevilenemy 18 years, 1 month ago
Hello again everyone, I've recently decided to add the ability for the player to rotate their ship. the problem is, i've been having terrible trouble actually rotating the ship properly. the method i tried was a combination of glRotated() and glTranslated(). Using glTranslated() to set the position of the axis of rotation and glRotated() to do the acutal rotation. When i do this, it works perfectly until i move the ship... the axis of rotation does not move along with the ship and so it begins to rotate about it's old position rather than it's own centerpoint. I havent been able to figure out how to solve this problem. Any suggestions? I would be glad to post relevent code if needed.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Advertisement
It sounds like you're glTranslate()-ing to a fixed spot every time. If you translate to the center point of the ship instead, you should get what you want.

Edit: and if that doesn't work, post the code where you do your rotating so we can see what's goin' on.
the following is a section of the function that draws the ship/enemy ships/objects/etc. The HandleEnemies and HandleObjects functions are self explanitory -- their code is not in question and is not posted.

if(scrnum == 0)
{
glTranslated(0,0,-5);
DrawBitmap(bgimgnum, -3, 3, 0, 3, -3);

glPushMatrix();

glRotated(shipangle, shipx, 0, shipy) // i know it doesnt make sense -- but it was visually as close as i could get
glTranslated(shipx, shipy, 1);

HandleEnemies(Enemies);
HandleObjects(Objects, PShip.currenthp, PShip.currentshields, PShip.shieldson, shipx-0.25, shipy-1.25, shipx+0.25, shipy-1.75, sdead);

if(PShip.shieldson == true && PShip.currenthp > 50 && PShip.currentshields > 0) // If Shields Are On, and there is some shields strength left
{
//DrawBitmap(1, -0.25+shipx, -1.25+shipy, 1, 0.25+shipx, -1.75+shipy);
DrawObj(models[0], 1, shipx, -1.375+shipy, 1); // Draw the Player's Ship
}
if(PShip.shieldson == false && PShip.currenthp > 50) // If Shields Are Off
{
//DrawBitmap(3, -0.25+shipx, -1.25+shipy, 1, 0.25+shipx, -1.75+shipy);
DrawObj(models[0], 3, shipx, -1.375+shipy, 1); // Draw the Player's Ship
}
if(PShip.currenthp <= 50 && PShip.currenthp > 0)
{
//DrawBitmap(4, -0.25+shipx, -1.25+shipy, 1, 0.25+shipx, -1.75+shipy);
DrawObj(models[0], 4, shipx, -1.375+shipy, 1); // Draw the Player's Ship
}
if(PShip.currenthp > 50 && PShip.currentshields <= 0)
{
//DrawBitmap(3, -0.25+shipx, -1.25+shipy, 1, 0.25+shipx, -1.75+shipy);
DrawObj(models[0], 3, shipx, -1.375+shipy, 1); // Draw the Player's Ship
}

if(PShip.currenthp <= 0)
{
//DrawBitmap(9, -0.25+shipx, -1.25+shipy, 1, 0.25+shipx, -1.75+shipy);
DrawObj(models[2], 9, shipx, -1.375+shipy, 1); // Draw Explosion at Player's Ship's Location
sdead = true;
}
glPopMatrix();
if(sdead != true && PShip.currenthp > 0)
{
HandlePfire(Enemies, Objects);
}
}

sorry about the poor formatting... i dont know how to properly format the code in a post.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Quote:Original post by medevilenemy
sorry about the poor formatting... i dont know how to properly format the code in a post.
To format your code in a post, there are two tags you can use. Either encase everything in {code} {/code} tags, or {source} {/source}, using the square brackets instead of the curly brackets.

To fix your problem, simply translate to the ship's position first, and then rotate.
I actually had the code compiled that way for a while... didnt solve the problem

in fact, it didnt appear to do anything at all.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Okay... next problem with your above code... hehe

Umm, when you translate to the ship's current position, that becomes your new origin. So, when you're later calling your DrawObj(models[0], 1, shipx, -1.375+shipy, 1) function, you're actually drawing at a point (shipx, -1.375+shipy) away from where the ship is positioned. So instead, you need to do DrawObj(models[0], 1, 0, -1.375, 1) to get what you want (if I understand how that function works, that is...

Try that, see if that helps. You'll also have to adjust your code to properly draw the other objects in the scene, too, as they will all begin from the ship's position as well as their origin.
again, sorry, i forgot to mention that the code i posted was the pre-rotation code (i reverted back) with the couple of lines of new code jammed in there for review purposes. In the actual version i was using, i was drawing my objects at (shipx, shipy, 0)
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Alright, it is about 7:50PM now and i've managed to fix the problem.

The problem wasnt really with my rotation or translation calls, but rather a conflict between the new coordinate system imposed by glTranslated() and my old coordinate system. I re-wrote the old system to work with the new one and it now works perfectly.

The updated game is downloadable from www.medevilenemy.f2o.org/pp.html

The only problem now is a positioning oddity with the torpedos when the ship is angled. I can tell you now that the torpedo initial positions are set to be precisely where they are when the ship is facing up, but i have yet to figure out how to move them depending on the ship's angle.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
You need to perform the translate and rotate on the starting position of the torps. I don't know if you can ask OpenGL to transform arbitrary points through the current transformation matrix; if not it's an easy enough piece of matrix multiplication to find the correct location. (You need to change their orientation too, but if they fire straight forwards that is trivial.)
alrighty... i've managed to fix the whole torpedo mis-placement issue thing.

It took a couple of hours and a diagram to figure out but it came down to a simple matter of basic vector physics... i simply used the sine and cosine of the ship's angle to adjust the torpedo's position and it works perfectly...

Here is the code:
torposx = shipx + (-0.25*cos(shipangle * (3.141592653/180)));
torposy = shipy + (-0.25*sin(shipangle * (3.141592653/180)));
// This is the code for torpedo #1, the code for torpedo #2 is basically the same


Consequently, i've uploaded the updated version of the game to my website. The download page is www.medvilenemy.f2o.org/pp.html
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.

This topic is closed to new replies.

Advertisement