Creating a movable camera (FPS style)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ceultem
Posts: 7
Joined: Sat Feb 25, 2017 10:44 am

Creating a movable camera (FPS style)

Post by Ceultem »

Hi! :) I have been trying to create my own class for moving the camera. I know that you can achieve this in irrlicht with the FPSAnimator, but it would be nice to have your own class, since it gives you more control and such. :) So, I have been trying to get my class to work for a while, but haven't been able to get it working porberly, and now I'm running out of ideas on how I could get it working. In my class I have 3 functions, that handle the FPS-like actions:

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. :D

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));
}
 
With this code the target setting sems to work quite well at first, but after you have moved and looked around for a bit it starts to act strangely. For example, moving the mouse up moves the target downwards, when it should move upwards. Also the target distance isn't staying on the defined lenght. I had the Y-coordinate defined like this at first:
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);
        }
    }
}
 
Like I said earlier, this code might be working correctly, but I'm not sure.

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. :)
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Creating a movable camera (FPS style)

Post by CuteAlien »

You likely don't want to rotate around global x/y axes but around local one (you can get those from your transformation matrix).

Not sure about updatTarget, might be correct. Otherwise copy code from CCameraSceneNode.cpp in Irrlicht code: Target = getAbsolutePosition() + rotation.rotationToDirection();
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Ceultem
Posts: 7
Joined: Sat Feb 25, 2017 10:44 am

Re: Creating a movable camera (FPS style)

Post by Ceultem »

Okay, thanks for the answer. :)
Post Reply