How can I rotate the camera based on the direction the player is looking at from a constant distance?
This is the code i got so far:
Code: Select all
void rotate(vector3df rot, ISceneNode* box)
{
matrix4 m;
m.setRotationDegrees(box->getRotation());
matrix4 n;
n.setRotationDegrees(rot);
m *= n;
box->setRotation(m.getRotationDegrees());
box->updateAbsolutePosition();
}
int main()
{
IrrlichtDevice *device;
CIrrEventReceiver* receiver = new CIrrEventReceiver();
device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(800, 600), 32, false, false, false, receiver);
if (device == 0)
return 1;
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
// the thing we want to look at
ISceneNode* box = smgr->addCubeSceneNode();
ISceneNode* sphere = smgr->addSphereSceneNode();
sphere->setPosition(vector3df(0.0,0.0,-100.0));
box->setPosition(vector3df(0.0,0.0,-10.0));
// add camera
ICameraSceneNode* cam = smgr->addCameraSceneNode(); //box
cam->setTarget( box->getAbsolutePosition() );
cam->setFarValue(10000);
smgr->addSkyBoxSceneNode
(
driver->getTexture("data/irrlicht2_up.jpg"),
driver->getTexture("data/irrlicht2_dn.jpg"),
driver->getTexture("data/irrlicht2_lf.jpg"),
driver->getTexture("data/irrlicht2_rt.jpg"),
driver->getTexture("data/irrlicht2_ft.jpg"),
driver->getTexture("data/irrlicht2_bk.jpg")
);
ICursorControl* cursor = device->getCursorControl();
cursor->grab();
cursor->setVisible(false);
core::position2d<f32> cursorCenterPos = cursor->getRelativePosition();
while(device->run())
{
// window is active, so render scene
if (device->isWindowActive())
{
receiver->endEventProcess();
if (driver->beginScene(true, true, video::SColor(255, 100, 100, 140)))
{
smgr->drawAll();
driver->endScene();
}
f32 deltaYaw = 0;
f32 deltaPitch = 0;
position2d<f32> cursorDelta = cursor->getRelativePosition() - cursorCenterPos;
if (cursor)
{
if (cursorDelta.X || cursorDelta.Y)
{
deltaYaw = cursorDelta.X * 50.0; //cMouseSensitivity;
deltaPitch = -cursorDelta.Y * 50.0; //cMouseSensitivity;
cursor->setPosition(cursorCenterPos);
cursorCenterPos = cursor->getRelativePosition();
}
}
rotate(vector3df(0, deltaYaw, 0), box);
if(receiver->isKeyPressed(KEY_KEY_W))
{
box->setPosition(box->getPosition() + core::vector3df(0.0,0.0,-1.0));
box->updateAbsolutePosition();
core::stringc s = "Box position: ";
s += (int)box->getPosition().X;
s += ", ";
s += (int)box->getPosition().Y;
s += ", ";
s += (int)box->getPosition().Z;
cout << s.c_str() << endl;
}
if(receiver->isKeyPressed(KEY_KEY_S))
{
box->setPosition(box->getPosition() + core::vector3df(0.0,0.0,1.0));
box->updateAbsolutePosition();
core::stringc s = "Box position: ";
s += (int)box->getAbsolutePosition().X;
s += ", ";
s += (int)box->getAbsolutePosition().Y;
s += ", ";
s += (int)box->getAbsolutePosition().Z;
cout << s.c_str() << endl;
}
if(cam->getPosition().getDistanceFromSQ(box->getPosition()) > 50)
{
cam->setPosition(box->getAbsolutePosition() - cam->getTarget().normalize() * 50);
cam->setTarget(box->getAbsolutePosition());
vector3df ypos = cam->getPosition();
ypos.Y += 25.0f;
ypos.Y += cam->getPosition().Y;
cam->setPosition(ypos);
cam->updateAbsolutePosition();
}
receiver->startEventProcess();
}
}
device->drop();
cursor->drop();
return 0;
}
Thanks in advance!