🔒 Locked
Rotating weapon problem
Advertisement
Original Post
Hi, i am working on a VERY basic first person shooter. I used some of the code from www.gametutorials.com (great site) to use mouse movement. And i have draw a square (temporary weapon) at the same coords as the center x,y,z values. So effectivly the "gun" is in front of me, but the problem is, when u turn, the gun does not rotate with the camera, all it does, is just stay facing the same direction , in fact, it looks a lot like a 3rd person camera. I know i have to use vectors, but im not quite sure of my self with that kind of maths, and i could do with some help.
thx
----------------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
What, like the old Delta Force Land Warrior? lol, those were the days. But i think i will stick with 3D beacuse, i want to move on to models later, also, could someone tell me how to make a polygon reflective, ive seen all these tutorials, but i coudnt get them to work..
-------------
-------------
Okay I hoped you picked3d too...
Well if you know your rotation in your world you can you this to figure out where your next move will be if you step forward, which you can use to draw the weapon...
PosX := Position.X - Sin(DegToRad(Rotation.Y));
PosZ := Position.Z + Cos(DegToRad(Rotation.Y));
Then you can time each one something like 0.1-0.5 to get different distances. I write in Object Pascal but I hope that you can understand it, it''s not that hard, but anyway just ask if it''s confusing. I''m not that much into classes in C++, so that''s why I haven''t rewrote it.
Well if you know your rotation in your world you can you this to figure out where your next move will be if you step forward, which you can use to draw the weapon...
PosX := Position.X - Sin(DegToRad(Rotation.Y));
PosZ := Position.Z + Cos(DegToRad(Rotation.Y));
Then you can time each one something like 0.1-0.5 to get different distances. I write in Object Pascal but I hope that you can understand it, it''s not that hard, but anyway just ask if it''s confusing. I''m not that much into classes in C++, so that''s why I haven''t rewrote it.
i probably didnt make it quite clear.
What i CAN & CANT do:
CAN: get weapon to have its X,Y,Z tobe equal to the cameras, ie, it always follows you.
CANT: get weapon to always face forward when u turn, instead, it jus rotates around its on axis.
hope that clears it up a bit
--------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
What i CAN & CANT do:
CAN: get weapon to have its X,Y,Z tobe equal to the cameras, ie, it always follows you.
CANT: get weapon to always face forward when u turn, instead, it jus rotates around its on axis.
hope that clears it up a bit
--------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
ok, as there are so many "how do i get my stuff to be in front of the camera" questions: by doing NOTHING. sure, you can figure out camera position and orientation, use the same for your object, then have the inverse used as view matrix and watch how they cancel each other out.
-loadidentity
-transform relative to camera (most likely a little down and forward)
-draw
its not different from drawing a gui and you never see anyone doing complex trigonometry for every button and health bar, do you?
-loadidentity
-transform relative to camera (most likely a little down and forward)
-draw
its not different from drawing a gui and you never see anyone doing complex trigonometry for every button and health bar, do you?
Just to make sure Kris gets the concept... Since you never want the gun to move and always want it to be in the same position, you don't have to do any rotation or anything for the gun. What you want to do is set your camera and draw your scene and then call glLoadIdentity() so that you are back at the origin. Then translate to the camera position (and, like Trienco said, you will want to move a little in front of the camera so the user can see the gun), and then draw the gun. So, something like this:
Hope this, along with the other posts, clears things up. As for vector math, you can find tons of stuff on the web if you're interested.
~Urayami
[edited by - urayami on May 7, 2004 4:03:42 AM]
glLookAt (...) ; // Or whatever...setup your camera
// Draw the stuff in the scene
glLoadIdentity () ; // Reset orientation
glTranslate(...) ; // Translate a little bit in front of the camera
// Draw the gun
Hope this, along with the other posts, clears things up. As for vector math, you can find tons of stuff on the web if you're interested.
~Urayami
[edited by - urayami on May 7, 2004 4:03:42 AM]
thx for that last one, i used it.
but the problem still is, that when u look around (using mouse) the gun just stays where it is.
IE: you are looking around, but the gun still faces the same way it started.
what i want, is the gun to face forward, whereever you are looking...
---------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
but the problem still is, that when u look around (using mouse) the gun just stays where it is.
IE: you are looking around, but the gun still faces the same way it started.
what i want, is the gun to face forward, whereever you are looking...
---------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
quote:
Original post by Kris2456
IE: you are looking around, but the gun still faces the same way it started.
what i want, is the gun to face forward, whereever you are looking...
and you did
a) load the identity WITHOUT touching glulookat or whatever after that
b) only translate it (slightly right, a bit down, somewhat forward)
because then its impossible for it to get any rotation unless you explicitly add it yourself.
LOL, now this is a funny one. I usd that code that u sent me. It works, but only partially. When you look around the TieFighter (previously weapon - got bored-) stays in your view, and osnt o reandom rotations, etc...
BUT, when you move, the tie fighter does all sorts of crazy things, and flys miles forards when you press UP, etc...
just to show you what i have done, here is my GLDrawSene code
the code for the camera is of GameTutorials.com Camera lesson 3.
cheers in advance
----------------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
BUT, when you move, the tie fighter does all sorts of crazy things, and flys miles forards when you press UP, etc...
just to show you what i have done, here is my GLDrawSene code
nt DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer // Done Drawing The Triangle
//THE LOAD IDENTITY
glLoadIdentity();
//THE TRANSLATION
glTranslatef(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z-2);
//THE TIEFIGHTER
TieFighter();
//THE GluLookAt
// // //
gluLookAt(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z,
g_Camera.m_vView.x, g_Camera.m_vView.y, g_Camera.m_vView.z,
g_Camera.m_vUpVector.x, g_Camera.m_vUpVector.y, g_Camera.m_vUpVector.z);
//THIS BIT IS TERRAIN
glPushMatrix();
glTranslatef(0.0f,-30.0f,-20.0f);
glBindTexture(GL_TEXTURE_2D, TexID[6]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);glVertex3f(-50.0f,0.0f,-50.0f);
glTexCoord2f(1.0f, 1.0f);glVertex3f(50.0f,0.0f,-50.0f);
glTexCoord2f(1.0f, 0.0f);glVertex3f(50.0f,0.0f,-50.0f);
glTexCoord2f(0.0f, 0.0f);glVertex3f(-50.0f,0.0f,50.0f);
glEnd();
glPopMatrix();
return TRUE; // Everything Went OK
}
the code for the camera is of GameTutorials.com Camera lesson 3.
cheers in advance
----------------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
That sounds like a problem with your keyboard functionality. I don't understand what you meant with the rotational problems. As for the depth translational problems, it sounds like you're either increasing the depth too much with each key press or there's a problem because your movement isn't time-dependent. Personally, I'd just make the movement time-dependent to fix the problem because it would also add more functionality to your program at the same time and you'd get to learn about that as well. I know there's a time-dependent movement tutorial at gametutorials.com and I'm sure you can find many via a search.
~Urayami
[edited by - urayami on May 7, 2004 5:40:16 PM]
~Urayami
[edited by - urayami on May 7, 2004 5:40:16 PM]
Why are you translating your weapon by the camera coordinates? That will definitely make it move around when you move the camera. All you need is to move the thing forward by some constant amount so it''s in front of the near clip plane.
...
glLoadIdentity();
glTranslatef(0, 0, -weapondistance);
TieFighter();
...
*sigh*
>>glTranslatef(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z-2);<<
i hoped it was obvious to not do _anything_ more than described (i even added words like "slightly" and "a bit" to make it more clear). this cant work, because youre supposed to only translate it into position RELATIVE to the camera. it just absolutely and in no way matters where youre camera is or how it is rotated, because after loading the identity it is ALWAYS at 0,0,0 and facing along 0,0,-1.
thats btw. why its not a good idea to try writing 3d games without understanding at least the basic math behind it.
>>glTranslatef(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z-2);<<
i hoped it was obvious to not do _anything_ more than described (i even added words like "slightly" and "a bit" to make it more clear). this cant work, because youre supposed to only translate it into position RELATIVE to the camera. it just absolutely and in no way matters where youre camera is or how it is rotated, because after loading the identity it is ALWAYS at 0,0,0 and facing along 0,0,-1.
thats btw. why its not a good idea to try writing 3d games without understanding at least the basic math behind it.
WOW, thx man, now it works a treat. Basicly, when you said a position relative to the camera, it hought i hjad to type the coords of the camera, and take away 2. but i see what you mean now, and ill keep that in mind. Thx to everyone else aswell....
--------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
--------
"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement