Jump to content
  • Advertisement
Sign in to follow this  
mynameisnafe

OpenGL GLM First Person Camera is broke as, C++

This topic is 2078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, it's broken. Theres a lot of code here! Please help

 

It's basically the one from opengl-tutorial.com. Using glm but not glfw.

 

The camera kind of works but doesn't.. it's jerky and when I go left with the mouse it goes and looks up etc.. when I go up it goes down..

all sorts of crazy

 

If we can fix this maybe we can fix the keyboard input :/

 

void UpdateFromMouse( int w, int h, int mousex, int mousey )
		{	
			/*
				Handle the mouse input
			*/
			_mx = mousex;		_my = mousey;

			m_horizontalAngle += m_mouseSpeed * m_frameTime * float( w / 2 - _mx );
			m_verticalAngle   += m_mouseSpeed * m_frameTime * float( h / 2 - _my );

			m_direction = glm::vec3
			(
				cos( m_verticalAngle ) * sin( m_horizontalAngle ),
				sin( m_verticalAngle ),
				cos( m_verticalAngle ) * cos( m_horizontalAngle )
			);

			m_right = glm::vec3
			(
				sin( m_horizontalAngle - 3.14f / 2.0f ),
				0,
				cos( m_horizontalAngle - 3.14f / 2.0f )
			);

			m_up = glm::cross( m_right, m_direction );

			/*
				Update the view matrix
			*/
			m_viewMatrix = glm::lookAt( position, position + m_direction, m_up );
		}

 

 

Edited by mynameisnafe

Share this post


Link to post
Share on other sites
Advertisement

Without looking at the original code, I am pretty sure that you should go back and have a very close look at what they are doing with the mouse coordinates. Limited to the code above it is completely pointless to store mx and my and you really need to make up your mind about whether you want to use mouse position or mouse movement.

Share this post


Link to post
Share on other sites

Without looking at the original code, I am pretty sure that you should go back and have a very close look at what they are doing with the mouse coordinates. Limited to the code above it is completely pointless to store mx and my and you really need to make up your mind about whether you want to use mouse position or mouse movement.

 

Movement please!

 

Which code would you like to see? I'm happy to share :)

Share this post


Link to post
Share on other sites

Correct me if I'm wrong, but this looks like w and h are your window width and height, with mousex and mousey being the mouse position.

 

So you are changing your angles not by how far the mouse has moved, but by how far it is from the center. This would make little to no sense, unless the intention is to not rotate "with" your mouse, but always rotate towards it (and if that was the case, there should be a dead zone in the center).

 

It also makes the mx and my variables completely pointless, because you could just use mousex and mousey directly. So ditch w and h, and use (mousex - mx) and (mousey-my) to adjust the angle, _then_ set mx and my to mousex and mousey.

Share this post


Link to post
Share on other sites

Trienco:
You´r right, but there is a missing code there (at least on my code)

At the end of code there should be:

glfwSetMousePos(w/2, h/2); 

So the mouse is always on center.

Share this post


Link to post
Share on other sites

I guess that works, as long as it won't create any mouse move events. Sounds like a really annoying thing during debugging, though. Especially when glfw lets you disable GLFW_MOUSE_CURSOR.

 

In that case it's time to debug, check what happens to your angles, make sure the cross product is in the correct order, etc.

Edited by Trienco

Share this post


Link to post
Share on other sites

Not using GLFW, using SetCursorPosition and GetCursorPositition from Win32 API.. Soul, I reset the cursor pos outside this call, after I call cam->UpdateFromMouse( x, y, w, h,)

 

I think I bodged it to work anyways.. trigonometry abuse.

 

void UpdateFromMouse( int w, int h, int mousex, int mousey )
        {    
            _mx = (float) mousex;        _my = (float) mousey;

            m_frameTime = g_clock->GetFrameTime() ;   ///always returns like 0.000000000001 so hence bodge below..
            
            m_frameTime = 0.01;

            m_horizontalAngle -= m_mouseSpeed * m_frameTime * float( _mx );
            m_verticalAngle     -= m_mouseSpeed * m_frameTime * float( _my );

            m_direction = glm::vec3
            (
                cos( m_verticalAngle ) * sin( m_horizontalAngle ),
                sin( m_verticalAngle ),
                cos( m_verticalAngle ) * cos( m_horizontalAngle )
            );

            m_right = glm::vec3
            (
                sin( m_horizontalAngle - 3.14f / 2.0f ),
                0,
                cos( m_horizontalAngle - 3.14f / 2.0f )
            );

            m_up = glm::cross( m_right, m_direction );

            /*
                Update the view matrix
            */
            m_viewMatrix = glm::lookAt( position, position + m_direction, m_up );
        }

 

then in 'main'..

 

 
//att initialisation
mainCam->Create( theClock );
 
//at frame()/ update / message handler::
 
case WM_LBUTTONDOWN: {
            mDown = true;
            ShowCursor(false);

            SetCapture(hWnd);                                    // capture the mouse.      
            GetCursorPos(&cPos);                                // store cursor position
            SetCursorPos(screenWidth / 2, screenHeight / 2);    // reset to some default position
                
            return 0;
        }
            
        case WM_LBUTTONUP: {
            if ( mDown ) {
                mDown = false;    
        
                SetCursorPos( cPos.x, cPos.y );                    // set the cursor to its position
                ReleaseCapture();                                // release the capture                        
            }
            ShowCursor( true );                                    // show it
            
            return 0;
        }
            
        case WM_MOUSEMOVE: {
            if( mDown ) {
                GetCursorPos( &currentPos ); // screen coords

                disp.x = currentPos.x -  screenWidth / 2;
                disp.y = currentPos.y -  screenHeight / 2;

                /*    Update the Camera     */
                m_GFX->mainCam->UpdateFromMouse( screenWidth, screenHeight, disp.x, disp.y );

                SetCursorPos( screenWidth / 2, screenHeight / 2 );
                return 0;
            }
        }
 
 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!