# 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);

}
``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
i wonder whether the following is correct?

``````angleY = (float)( (middleX - dx) ) / 100000.0f;
angleZ = (float)( (middleY - dy) ) / 100000.0f;
``````

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
0
Author Commented:

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+0x97d): undefined reference to `Shader::InitShaders(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

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.
0
Commented:
the 'undefined reference to `Shader::InitShaders' error more likely is a linker error than a compiler issue. i would say you missed to link against the library where the Shader class was implemented.

perhaps you did a change in the header (for example remove a const) but not in the cpp.

Sara
0

Experts Exchange Solution brought to you by