Custom camera movement like addCameraSceneNodeFPS

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
sader
Posts: 28
Joined: Sat Sep 29, 2007 1:38 pm

Custom camera movement like addCameraSceneNodeFPS

Post by sader »

How they do rotation for camera?
Now I am writing custom camera function. My camera fallows model that's great but now I wish to make camera turn left/right up/down when mouse is moving...

In my OnEvent function I have variables mouseForceX and mouseForceY,
those gets values -1 0 1 depend how mouse is moving along axis.

These two variables would be very useful if I could set camera angle with
camera->setRotation();
instead of that i must use setTarget(); and this is place where I get stuck, I don't know how recalculate target position for camera...

I tryed do somthing with ISceneCollisionManager::getRayFromScreenCoordinates() but result wasn't what I have expected.
Maybe this task is done with matrices? but I am not sure exactly how :/

I'm still trying to find answer via 'search' :)
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

may be you can dig something out of this code:

Code: Select all

/*-----------------------------------------------------------------------------*
| headerfile MCamera.h                                                         |
|                                                                              |
| version 1.30                                                                 |
| date: (19.09.2007)                                                           |
|                                                                              |
| author:  Michal Švantner                                                     |
|                                                                              |
| for Irrlicht engine                                                          |
| firsth person and orbiting cameras                                           |
*-----------------------------------------------------------------------------*/

#ifndef MCAMERA_H
#define MCAMERA_H

#include "ICameraSceneNode.h"
using namespace irr;



class MCamera
{
protected:

    scene::ICameraSceneNode* camera;
    
public:
    
    MCamera(scene::ISceneManager* smgr)
    {
        camera = smgr->addCameraSceneNode();
    }
    
    ~MCamera(){}
    
    virtual void setPosition(core::vector3df pos)
    {
        camera->setPosition(pos);
        camera->updateAbsolutePosition();
    }
    
    virtual core::vector3df getPosition() { return camera->getPosition(); }
    
    virtual core::vector3df getTarget() { return camera->getTarget(); }
    
    virtual f32 getFarValue() { return camera->getFarValue(); }
    
    virtual void setFarValue(f32 f) { camera->setFarValue(f); }
    
    virtual f32 getNearValue() { return camera->getNearValue(); }
    
    virtual void setNearValue(f32 n) { camera->setNearValue(n); }
    
    virtual f32 getFOV()  { return camera->getFOV(); }
    
    virtual void setFOV(f32 v) { camera->setFOV(v); }
    
    virtual f32 getAspectRatio() { return camera->getAspectRatio(); }
    
    virtual void setAspectRatio(f32 a) { camera->setAspectRatio(a); }
    
    virtual scene::ISceneNode* getISceneNode() { return camera; }
};



class MCameraFPS : public MCamera
{
    f32 rotationX;
    f32 rotationY;
    core::vector3df direction;
    
public:
    MCameraFPS(scene::ISceneManager* smgr) : MCamera(smgr)
    {
        rotationX = 0.0f;
        rotationY = 0.0f;
        direction = core::vector3df(0,0,1);
    }
    
    ~MCameraFPS(){}
    
    void turnRight(f32 i)
    {
        rotationY += i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;
        
        direction = core::vector3df(0,0,1);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);
        
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void turnLeft(f32 i)
    {
        rotationY -= i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;
        
        direction = core::vector3df(0,0,1);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);
        
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void turnUp(f32 i)
    {
        rotationX += i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;
        
        direction = core::vector3df(0,0,1);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);
        
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void turnDown(f32 i)
    {
        rotationX -= i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;
        
        direction = core::vector3df(0,0,1);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);
        
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void moveForward(f32 i)
    {
        core::vector3df step = core::vector3df(0,0,i);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);
        
        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void moveBack(f32 i)
    {
        core::vector3df step = core::vector3df(0,0,-i);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);
        
        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void moveRight(f32 i)
    {
        core::vector3df step = core::vector3df(i,0,0);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);
        
        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void moveLeft(f32 i)
    {
        core::vector3df step = core::vector3df(-i,0,0);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (0,rotationY,0));
        matrix.rotateVect(step);
        
        camera->setPosition(camera->getPosition() + step);
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    void setHeight(f32 i)
    {
        camera->setPosition(core::vector3df(camera->getPosition().X, i, camera->getPosition().Z));
        camera->setTarget(camera->getPosition() + direction);
        camera->updateAbsolutePosition();
    }
    
    core::vector3df getDirection() { return direction; }
    
    f32 getHeading() { return rotationY; }
    
    f32 getPitch() { return rotationX; }
};



class MCameraOrbit : public MCamera
{
    f32 rotationX;
    f32 rotationY;
    f32 distance;
    
    void update()
    {
        core::vector3df direction = core::vector3df(0,0,distance);
        
        core::matrix4 matrix;
        matrix.setRotationDegrees(core::vector3df (rotationX,rotationY,0));
        matrix.rotateVect(direction);
        
        camera->setPosition(camera->getTarget() + direction);
        camera->updateAbsolutePosition();
    }
    
public:
    MCameraOrbit(scene::ISceneManager* smgr) : MCamera(smgr)
    {
        rotationX = 0.0f;
        rotationY = 0.0f;
        distance = 1.0f;
        camera->setTarget(core::vector3df(0,0,0));
        update();
    }
    
    ~MCameraOrbit(){}
    
    void turnRight(f32 i)
    {
        rotationY += i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;
        update();
    }
    
    void turnLeft(f32 i)
    {
        rotationY -= i;
        if(rotationY>=360)rotationY-=360;
        if(rotationY<0)rotationY+=360;
        update();
    }
    
    void turnUp(f32 i)
    {
        rotationX += i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;
        update();
    }
    
    void turnDown(f32 i)
    {
        rotationX -= i;
        if(rotationX>=360)rotationX-=360;
        if(rotationX<0)rotationX+=360;
        update();
    }
    
    void setTarget(core::vector3df target)
    {
        camera->setTarget(target);
        update();
    }
    
    f32 getDistance() { return distance; }
    
    void setDistance(f32 i)
    {
        distance = i;
        update();
    }
    
    void adjustDistance(f32 i)
    {
        distance += i;
        update();
    }
    
    f32 getHeading() { return rotationY; }
    
    f32 getPitch() { return rotationX; }
};
#endif
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

As Irrlicht is opensource and you have the source in the SDK you downloaded you could always have a rummage around to see how it's done ;)
Image Image Image
sader
Posts: 28
Joined: Sat Sep 29, 2007 1:38 pm

Post by sader »

Thank you both :roll:

LOL never did look in "source" folder :oops:
Post Reply