Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


GLM First Person Camera is broke as, C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 mynameisnafe   Members   -  Reputation: 252

Like
0Likes
Like

Posted 03 February 2013 - 08:46 AM

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, 03 February 2013 - 08:49 AM.


Sponsor:

#2 Sponji   Members   -  Reputation: 1359

Like
0Likes
Like

Posted 04 February 2013 - 11:06 AM

I'm not sure, but shouldn't the up vector for lookat be just glm::vec3(0,1,0)?


Derp

#3 Trienco   Crossbones+   -  Reputation: 2224

Like
0Likes
Like

Posted 04 February 2013 - 11:00 PM

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.


f@dzhttp://festini.device-zero.de

#4 mynameisnafe   Members   -  Reputation: 252

Like
0Likes
Like

Posted 05 February 2013 - 03:52 AM

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 :)



#5 This Dying Soul   Members   -  Reputation: 145

Like
0Likes
Like

Posted 07 February 2013 - 12:30 AM

I´m having the same problem.
Any success?



#6 Trienco   Crossbones+   -  Reputation: 2224

Like
0Likes
Like

Posted 07 February 2013 - 11:05 AM

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.


f@dzhttp://festini.device-zero.de

#7 This Dying Soul   Members   -  Reputation: 145

Like
-1Likes
Like

Posted 07 February 2013 - 12:20 PM

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.



#8 Trienco   Crossbones+   -  Reputation: 2224

Like
0Likes
Like

Posted 07 February 2013 - 10:43 PM

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, 07 February 2013 - 10:44 PM.

f@dzhttp://festini.device-zero.de

#9 mynameisnafe   Members   -  Reputation: 252

Like
0Likes
Like

Posted 08 February 2013 - 05:29 AM

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;
            }
        }
 
 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS