Rotate Camera around object or 3d location

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
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Rotate Camera around object or 3d location

Post by Asimov »

Hi all,

I have been trying to rotate a camera around a point in space or an object or node.
I have set the target of the camera to 0,0,0 and then used setRotation.

I have two cameras in my scene and I am trying to rotate camera[0] around a point in space, thus (0,0,0) being that point.
Because I am making a board game, I will require the camera to have the ability to rotate around the board.

You may be wondering why in the setrotation I put (vector3df(1,1, camrot)). This is because when I used (vector3df(0,0, camrot)) nothing moved. However all I get is a gentle bobbing of the cameras like as if you are on the sea, but no rotation. I must remember these settings if I ever do a boat game LOL.

I am guessing this line is wrong
camrot+=50 * frameDeltaTime;
Perhaps it should be using some kind of radian or something, but I am far from being a mathmation.
I think maybe quaterians might be used, if I knew what I was doing doh.

Will paste relavent code.

Code: Select all

ICameraSceneNode *camera[2]={0,0};
camera[0] = smgr->addCameraSceneNode(0,vector3df(100, 70, 150), vector3df(0, 70, 60));
camera[1] = smgr->addCameraSceneNode(0,vector3df(100, 70, 550), vector3df(0, 70, 60));
camera[0]->setTarget(vector3df(0, 0, 0));
camera[0]->bindTargetAndRotation(true);
 
 
smgr->setActiveCamera(camera[0]);
 
//smgr->addCameraSceneNodeFPS();
 
float rotation=0;
float camrot=0;
 
// In order to do framerate independent movement, we have to know
    // how long it was since the last frame
    u32 then = device->getTimer()->getTime();
    // This is the movemen speed in units per second.
    const f32 MOVEMENT_SPEED = 5.f;
 
 
    while(device->run())
    {
        // Work out a frame delta time.
        const u32 now = device->getTimer()->getTime();
        const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
 
 
    receiver.Checkkeys(receiver,frameDeltaTime,&rotation);
    if(receiver.ChangeCamera1(receiver)) smgr->setActiveCamera(camera[0]);
    if(receiver.ChangeCamera2(receiver)) smgr->setActiveCamera(camera[1]);
 
    spaceinvader.RotateMesh(&rotation,frameDeltaTime);
 
camera[0]->setRotation(vector3df(1,1,  camrot));
camrot+=50 * frameDeltaTime;
 
 
        driver->beginScene(true,true,video::SColor(100,100,100,255));
 
        smgr->drawAll();
        guienv->drawAll();
        driver->endScene();
    }
    device->drop();
    return 0;
 
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Rotate Camera around object or 3d location

Post by mongoose7 »

To rotate a camera around (0, 0, 0) you presumably need to do two things - translate the camera and then rotate it. But it might be easier for you to create a dummy node at (0, 0, 0) and set the camera as the child. When you rotate the dummy node the camera will swing about the origin. (You may have to set bindTargetAndRotation true, i don't know.)
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Rotate Camera around object or 3d location

Post by Asimov »

Hi mongoose,

(0,0,0) is just an arbitary location I picked, for example, it might not actually be there, once I load my scene in. I am not sure how to create a dummy node yet.
As for translating the matrix I haven't a clue. My mathematics are not top notch if you know what I mean.

I did once get similar code to work in XNA, but it has been a while since I have used that, and that was using C# as well, so my memory is defeating me.

Rotating the camera is one of the main hurdles to start working on my game. I still have a lot more hurdles to go etc LOL.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Rotate Camera around object or 3d location

Post by Asimov »

Hi all,

I have changed my code slightly, so now it just does a 360 degree turn, but not around the target. It just turns.
Nice if I was doing an fps character, but not what I want. I wish it to rotate around (0,0,0) or any arbitary location at a given distance.
Or even a dummy node if I knew how to make one.

Code: Select all

ICameraSceneNode *camera[2]={0,0};
camera[0] = smgr->addCameraSceneNode(0,vector3df(100, 70, 150), vector3df(0, 70, 60));
camera[1] = smgr->addCameraSceneNode(0,vector3df(100, 70, 550), vector3df(0, 70, 60));
camera[0]->setTarget(vector3df(0, 0, 0));
camera[0]->bindTargetAndRotation(true);
 
 
smgr->setActiveCamera(camera[0]);
 
//smgr->addCameraSceneNodeFPS();
 
float rotation=0;
float camrot=0;
 
// In order to do framerate independent movement, we have to know
    // how long it was since the last frame
    u32 then = device->getTimer()->getTime();
    // This is the movemen speed in units per second.
    const f32 MOVEMENT_SPEED = 5.f;
 
s32 lastFPS = -1;
    while(device->run())
    {
        // Work out a frame delta time.
        const u32 now = device->getTimer()->getTime();
        const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
 
    receiver.Checkkeys(receiver,frameDeltaTime,&rotation);
    if(receiver.ChangeCamera1(receiver)) smgr->setActiveCamera(camera[0]);
    if(receiver.ChangeCamera2(receiver)) smgr->setActiveCamera(camera[1]);
 
    spaceinvader.RotateMesh(&rotation,frameDeltaTime);
 
camera[0]->setRotation(vector3df(0,camrot,0));
 camrot+=50.0f * frameDeltaTime;
 
        driver->beginScene(true,true,video::SColor(100,100,100,255));
 
        smgr->drawAll();
        guienv->drawAll();
        driver->endScene();
    }
    device->drop();
    return 0;
 
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Rotate Camera around object or 3d location

Post by Asimov »

Hi mongoose,

I know there is a way to rotate camera based on PI and cos and stuff, but that sounds complicated, so I thought I would try it the way you said, and I have to say that you are a genius.
Now I can seriously start working on my game. At first I didn't know how to add an empty node, so I had to research that. Then I didn't know how to add a camera as a child, so I just guessed replacing the 0 with the camTarget would work. camTarget being my camera target node. I also had to spend a couple of days looking at posts on this forum.

Anyway here is how I did it.
First I set up an empty node. I wasn't sure how to do this, but I found this code in someones post, and it seemed to do the trick

Code: Select all

ISceneNode* camTarget =smgr->addEmptySceneNode();
camTarget->setPosition(vector3df(0,50,0));
camTarget->setRotation(core::vector3df(0,0,0));
Then I set up my cameras like this. Remember I have 2 cameras. A rotating one and a fixed one.

Code: Select all

ICameraSceneNode *camera[2]={0,0};
camera[0] = smgr->addCameraSceneNode(camTarget,vector3df(100, 70, 150), vector3df(0, 70, 60));
camera[1] = smgr->addCameraSceneNode(0,vector3df(100, 70, 550), vector3df(0, 70, 60));
 
camera[0]->setTarget(camTarget->getPosition());
camera[0]->bindTargetAndRotation(true);
smgr->setActiveCamera(camera[0]);
 
float camrot=0;
 
and in my main game loop I have this for rotating the node. Amazingly it works.

Code: Select all

camTarget->setRotation(vector3df(0,camrot,0));
camrot+=50.0f * frameDeltaTime;
I hope this helps someone else also. Although I have a sneaking suspicion I may have done it wrong, but it works anyway LOL.
Anyway now I have got this working, I might actually build a camera class to keep all the camera stuff in one place.

So now I have learned how to do keyboard input,
I have learned how to make a meshloading class, and I have learnt how to rotate a camera.

I still have a lot to learn, but I have the basic tools now to make my game.
dc740
Posts: 12
Joined: Fri Dec 26, 2014 1:48 am

Re: Rotate Camera around object or 3d location

Post by dc740 »

looks like it's too late and I'm a newbie myself, so I don't know if this helps, but you can use the code from the Example N°8: Special Effects.

Code: Select all

    // create light
 
    node = smgr->addLightSceneNode(0, core::vector3df(0,0,0),
        video::SColorf(1.0f, 0.6f, 0.7f, 1.0f), 800.0f);
    scene::ISceneNodeAnimator* anim = 0;
    anim = smgr->createFlyCircleAnimator (core::vector3df(0,150,0),250.0f);
    node->addAnimator(anim);
    anim->drop();
 
    // attach billboard to light
 
    node = smgr->addBillboardSceneNode(node, core::dimension2d<f32>(50, 50));
    node->setMaterialFlag(video::EMF_LIGHTING, false);
    node->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
    node->setMaterialTexture(0, driver->getTexture("../../media/particlewhite.bmp"));
It's a simple light that circles around in the example. I think you could:
a) Read the source code of createFlyCircleAnimator in CSceneManager. Find out how the animator is called, open that file and copy the algorithm from the animator implementation
b) attach a FlyCircle animator to the camera. And make the camera look at a specified target (I haven't done it, I don't know if it can be done)

Good luck
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: Rotate Camera around object or 3d location

Post by Asimov »

Hi dc740,

Yes you are too late, but don't worry I am building a library of useful code snippets, and your code will definately be going in there for later use.

Thanks.
Post Reply