islandguy10
asked on
QT QPoint problems
I'm trying to get a section of code working for a first person shooter I'm working on. But I'm having issues getting my mouse control for my camera working properly. It seems the way I've implemented only moves left and right. I'm not sure why my Y coordinates are not being considered.
void Camera::SetViewByMouse(QPoint d)
{
int middleX = SCREEN_WIDTH/2; // This is a binary shift to get half the width
int middleY = SCREEN_HEIGHT/2; // This is a binary shift to get half the height
float angleY = 0.0f; // This is the direction for looking up or down
float angleZ = 0.0f; // This will be the value we need to rotate around the Y axis (Left and Right)
static float currentRotX = 0.0f;
// Get the mouse's current X,Y position
//GetCursorPos(&mousePos);
// If our cursor is still in the middle, we never moved... so don't update the screen
if( (d.x() == middleX) && (d.y() == middleY) ) return;
float dx = d.x();
float dy = d.y();
d.setX(middleX);
d.setY(middleY);
// Set the mouse position to the middle of our window
//SetCursorPos(middleX, middleY);
// Get the direction the mouse moved in, but bring the number down to a reasonable amount
angleY = (float)( (middleX - dx) ) / 100000.0f;
angleZ = (float)( (middleY - dy) ) / 100000.0f;
static float lastRotX = 0.0f;
lastRotX = currentRotX; // We store off the currentRotX and will use it in when the angle is capped
// Here we keep track of the current rotation (for up and down) so that
// we can restrict the camera from doing a full 360 loop.
currentRotX += angleZ;
// If the current rotation (in radians) is greater than 1.0, we want to cap it.
if(currentRotX > 1.0f)
{
currentRotX = 1.0f;
// Rotate by remaining angle if there is any
if(lastRotX != 1.0f)
{
// To find the axis we need to rotate around for up and down
// movements, we need to get a perpendicular vector from the
// camera's view vector and up vector. This will be the axis.
// Before using the axis, it's a good idea to normalize it first.
Vector3 vAxis;
vAxis.Cross(m_vView - m_vPosition, m_vUpVector);
vAxis.Normalize(vAxis);
// rotate the camera by the remaining angle (1.0f - lastRotX)
RotateView( 1.0f - lastRotX, vAxis.x, vAxis.y, vAxis.z);
}
}
// Check if the rotation is below -1.0, if so we want to make sure it doesn't continue
else if(currentRotX < -1.0f)
{
currentRotX = -1.0f;
// Rotate by the remaining angle if there is any
if(lastRotX != -1.0f)
{
// To find the axis we need to rotate around for up and down
// movements, we need to get a perpendicular vector from the
// camera's view vector and up vector. This will be the axis.
// Before using the axis, it's a good idea to normalize it first.
Vector3 vAxis;
vAxis.Cross(m_vView - m_vPosition, m_vUpVector);
vAxis.Normalize(vAxis);
// rotate the camera by ( -1.0f - lastRotX)
RotateView( -1.0f - lastRotX, vAxis.x, vAxis.y, vAxis.z);
}
}
// Otherwise, we can rotate the view around our position
else
{
// To find the axis we need to rotate around for up and down
// movements, we need to get a perpendicular vector from the
// camera's view vector and up vector. This will be the axis.
// Before using the axis, it's a good idea to normalize it first.
Vector3 vAxis;
vAxis.Cross(m_vView - m_vPosition, m_vUpVector);
vAxis.Normalize(vAxis);
// Rotate around our perpendicular axis
RotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);
}
// Always rotate the camera around the y-axis
RotateView(angleY, 0, 1, 0);
}
ASKER
I will try looking into your suggestions. Unfortunately I have run into some other problems. My program is a graphics scene using OpenGL and GLSL. I did some tweaking and have broken it. I seem to have messed up my class that loads my shaders and get the following error when dealing with a shader object:
OpenGLExample.o: In function `main':
OpenGLExample.cpp:(.text+0
I've googled this but the answers all seem to to say use the g++ compiler. Which is what I'm using. Something interesting that I've noticed is that if I #include "Shader.cpp" i do not receive this errorm however I do get a seg fault in the member function. If I just include the shader.h I get the original error. Needless to say, I'm baffled.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did have a linker issue. I did not know that when linking in a static library, even though the name of the library starts with "lib" you have to drop that when placing it in the make file. Thanks for pointing me in the right direction.
Open in new window
the right side computing doesn't look like 'angle' computing and why do you have a suffix Y when on the right side there are dx and middleX?
another point where you probably has to care for are comparision between floats like
if(lastRotX != 1.0f)
the problem is that floats are not really precise and can have different internal representation for a value that may output with the same value. in math the value 1.9999.... is equal to 2.0000.... in c/c++ we unfortunately may have that f1 != f2 though both print out as 2.0 and the only difference is that f1 was the result of a computation while f2 is not.
Sara