🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Gun Keeps Moving Away from Camera in OpenGL program

Started by
11 comments, last by yaboiryan 5 years, 2 months ago

I made a game in OpenGL 3.3 (I know, it is strange that I am working on something so new!). I ran into no errors, and was even adding some new screens when I came across an issue with my matrices.

I uploaded a video about it showing the exact issue here

(Ignore the Goldeneye models and music, it was just a funny test I could show to my friends that played Goldeneye)

 

Here, it is demonstrated that the gun moves faster than the camera in every direction.

How might I fix this?

 

I can give out any code that you want me to.

However, most of the code that positions the models is done with matrices from the GLM library.

 

I have checked the other gamedev.net post about something similar, and it was of no help... (you can find it here)

 

Advertisement

First you should write how do you draw a weapon, anyway imo it should be done like this, given some close near plane

You set the camera pos at the center of eyes. You draw weapon model, that is rotated with camera.....

So you create a rotation matrix out of camera view direction, you position your gun at the character hands and apply transformation (remembering to translate it to 0,0,0 point (but not for the weapon itself but the offset where you place gun related to the model center of rotation - thus this is more complicated cause torso moves on some fixed point) then you translate weapon back by player position

Order can be like this

-translate_character_cog*cam_otation*+translate_character_cog. More or less...

10 hours ago, yaboiryan said:

I made a game in OpenGL 3.3 (I know, it is strange that I am working on something so new!)

OpenGL 3.3 was released 9 years ago. Am I missing some joke here?

10 hours ago, yaboiryan said:

I can give out any code that you want me to.

That would be useful. Specifically, show the part of the code that sets up the camera matrix and the part of the code that sets up the gun matrix. And the shader that draws the gun. If it's not too big, best would be to show all of the code.

14 hours ago, _WeirdCat_ said:

First you should write how do you draw a weapon, anyway imo it should be done like this, given some close near plane

 

_WeirdCat_ is right.

Post how you are drawing your weapon.

But, you can find a good tutorial here: http://www.morrowland.com/apron/tut_gl.php (more old opengl, but can help you)

 

There are two ways to do this:

  1. Draw weapon with a separate renderer / draw call
    Pros: easy, no math required
    Cons: requires separate renderer, hard to integrate lighting from scene, won't translate to VR or third-person perspective
  2. Place the weapon directly in front of player on each frame. I do the following in my Lua/C++/OpenGL engine:

  local px, py, pz = getendoftickpos("player") -- Where the player will end up at the end of current tick    
  setpos("weapon", px, py - 0.25, pz) -- Place weapon directly on player and lower it a bit
  local ppitch, pyaw, proll = getplayerorient() -- Get euler angles to where the player is looking

  -- Weapon model position fix (this is specific to my model)
  setorient("weapon", 0, pyaw - 90, 0) -- The weapon model is to the side in the source .obj; turn it so we can fix this
  moveforward("weapon", 0.03) -- Move model to the side a little; fix this
  setorient("weapon", 0, pyaw, 0) -- Turn the weapon model back around so it's facing forward again

  moveforward("weapon", 0.5) -- Move weapon forward a bit

moveforward uses trigonometry to move the object forward to where it's facing (you can google this easily)

Disclaimers:

  • The player movement here is smoothed in C++ layer, but the weapon movement is not. Therefore, Lua is not in charge of calculating final player position of the tick, so it gets it from C++ via getendoftickpos().
  • I know the use of Euler angles is frowned upon. Works for me, though.
  • Yes, I know that to fix weapon position in the model, I should have moveleft() and moveright() functions instead of turning it around twice unnecessarily. Will probably add this in the future.
  • In my particular case, the above example does not take account for looking up and down (my engine does not do this yet), but in a general case, I think, it should.
On 4/16/2019 at 8:00 AM, 1024 said:

OpenGL 3.3 was released 9 years ago. Am I missing some joke here?

Yeah. A few years back I wrote an entire Real Time Strategy game in DirectX 9, which is almost my age.

On 4/16/2019 at 4:46 PM, WhiskyAndCode said:

_WeirdCat_ is right.

Post how you are drawing your weapon.

But, you can find a good tutorial here: http://www.morrowland.com/apron/tut_gl.php (more old opengl, but can help you)

 

Thank you! 

On 4/16/2019 at 5:46 PM, WhiskyAndCode said:

_WeirdCat_ is right.

Post how you are drawing your weapon.

But, you can find a good tutorial here: http://www.morrowland.com/apron/tut_gl.php (more old opengl, but can help you)

 

1. Nehe

2. Morrowland

 

are there any other old legacy OpenGL tutorials that you know of that have some cool techniques?

 

P.S. thank you and everyone in this forum for helping me!

they include:

VoxycDev

WhiskyAndCode

1024

and _WeirdCat_

8 hours ago, yaboiryan said:

are there any other old legacy OpenGL tutorials that you know of that have some cool techniques?

  

Why note a modern opengl ? https://learnopengl.com/

On 4/23/2019 at 1:48 PM, yaboiryan said:

 

are there any other old legacy OpenGL tutorials that you know of that have some cool techniques?

 

U will find here: http://www.humus.name/index.php?page=3D

and http://www.3dcpptutorials.sk/index.php?id=14

The code is finally here!

 

 

 


glm::mat4 model;
glm::mat4 gun;
glm::mat4 map;
glm::mat4 windowMat;

glm::mat4 view = camera.GetViewMatrix( );

...
              
gun = glm::translate(gun, glm::vec3(camera.GetPosition().x, camera.GetPosition().y, camera.GetPosition().z));

//my attempt to make the gun work is with this line of code. I made the gun use the camera's view matrix because I thought it would work...
gun = (gun * glm::inverse(view));



//I also have this as another way (I thought) I could get it to work

gun = glm::lookAt(glm::vec3(camera.GetPosition().x, camera.GetPosition().y, camera.GetPosition().z), glm::vec3
			(camera.GetPosition().x, camera.GetPosition().y, camera.GetPosition().z) + camera.GetFront(), camera.getUp());


//this is how I draw the gun
gunModel.Draw(shader);

I kept trying, but it seemed that almost none of the code would work...

If needed, I can give you my model view shader.

This topic is closed to new replies.

Advertisement