1) updateTarget()
This function updates the cameras target according to the current rotation of the camera, and also sets the target point to a certain distance. This is handy for example if you want to place some sort of target object on the target point. This function is currently flawed, as I'll explain soon in more detail.
2) updateRotation()
This function checks how much the mouse has been moved since the last update and updates the cameras rotation accordingly. I'm not sure if this function is working correctly. It's hard to tell when the target setting is working so badly.
3) move(Direction d)
This function moves the camera in relation to the target point according to the given direction(forward,back,left or right). This function seems to work without problems.
So, here is the code for updateTarget():
Code: Select all
void OwnCam::updateTarget()
{
float x, y, z; //Target coordinates
//Turning the angles to radians:
float alfa = cam->getRotation().Y*rad_mult;
float beta = cam->getRotation().X*rad_mult;
x = target_dist * cos(beta)*sin(alfa) + cam->getPosition().X;
y = (target_dist * sin(beta)*cos(alfa))*(-1.0f) + cam->getPosition().Y;
z = target_dist * cos(alfa) + cam->getPosition().Z;
cam->setTarget(core::vector3df(x,y,z));
}
y = (target_dist * sin(beta)*sin(alfa))*(-1.0f) + cam->getPosition().Y;
With Y-coordinate defined like that, the distance of the target was correct at all times, but other than that the camera was acting all crazy. So, I changed the sin+sin to sin+cos, which seems to work better.
Here's the code for the updateRotation():
Code: Select all
void OwnCam::updateRotation()
{
if (fpsMode)
{
core::position2d<s32> cur_pos = cursor->getPosition();
if ( cur_pos.X != resX/2 or cur_pos.Y != resY/2) //Checking if the cursor has been moved from the middle of the screen
{
core::vector3df cam_rot = cam->getRotation();
//Calculating how much the mouse has moved and setting the
//new rotation accordingly:
cur_pos.X -= float(resX/2);
cur_pos.Y = (cur_pos.Y-float(resY/2))*(-1.0f);
cam_rot.X -= float(cur_pos.Y)*rotation_speed;
cam_rot.Y += float(cur_pos.X)*rotation_speed;
cam->setRotation(cam_rot);
updateTarget();
cursor->setPosition(resX/2,resY/2);
}
}
}
I hope that this post isn't too long. I would greatly apriciate any help. I can also post the whole of the header and source files for the class, if that is necesary.