Jump to content
  • Advertisement
yaboiryan

Gun Keeps Moving Away from Camera in OpenGL program

Recommended Posts

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)

 

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Posted (edited)

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.
Edited by VoxycDev

Share this post


Link to post
Share on other sites
Posted (edited)
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! 

Edited by yaboiryan

Share this post


Link to post
Share on other sites
Posted (edited)
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_

Edited by yaboiryan

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!