Right, so, this is my problem. I am creating my own custom camera for a FPS game. I do not want to use the FPSCamera because I want to have more control over the camera and do some tricks with it later. So far, the camera look is working perfectly. The problem is moving the camera.
The code I am posting below has been pruned of unnecessary code. Firstly, my init() method:
Code: Select all
//INITIALIZES THE LEVEL
void gameManager::init()
{
cursor->setVisible(true);
device->setEventReceiver(eventReceiver);
smgr->loadScene("resources/meshes/test.irr");
camera = smgr->addCameraSceneNode();
target = smgr->addCubeSceneNode(0.1f,camera); //for debug purposes
NoVerticalMovement = true;
target->setPosition(vector3df(0,0,1));
camera->setPosition(vector3df(0,0,0));
smgr->setActiveCamera(camera);
camera->bindTargetAndRotation(false);
camera->render();
isMovingForwards = isMovingBackwards = isMovingLeft = isMovingRight = false;
moveSpeed = 0.2f;
}
Code: Select all
//UPDATE PLAYER MOVEMENT
void gameManager::updateMovement()
{
vector3df camPos = camera->getPosition();
vector3df direction = (camera->getTarget() - camera->getAbsolutePosition());
vector3df relativeRotation = direction.getHorizontalAngle();
direction.setLength(1.0f);
//direction.set(0,0, max_(1.0f, camPos.getLength()));
vector3df movedir = direction;
matrix4 mat;
mat.setRotationDegrees(vector3df(relativeRotation.X, relativeRotation.Y, 0));
mat.transformVect(direction);
if (NoVerticalMovement)
{
mat.setRotationDegrees(vector3df(0, relativeRotation.Y, 0));
mat.transformVect(movedir);
}
else
{
movedir = direction;
}
movedir.normalize();
if (isMovingForwards) camPos += movedir * moveSpeed;
if (isMovingBackwards) camPos -= movedir * moveSpeed;
updateCamera(camPos);
}
Code: Select all
//UPDATE PLAYER CAMERA WHEN MOVING
void gameManager::updateCamera(vector3df camPos)
{
camera->setPosition(camPos);
camera->setTarget(target->getPosition());
camera->updateAbsolutePosition();
target->updateAbsolutePosition();
}
When I move the camera with the mouse keys, the target, in most cases, goes shooting off into the distance. With some messing around, I can get it to behave chaotically, however, I can not keep the target within the 1.0f sphere around the camera. I hope I have explained the problem in enough detail.
Thanks.