problem sticking camera to node

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
cr33
Posts: 22
Joined: Fri Mar 27, 2009 3:37 pm

problem sticking camera to node

Post by cr33 »

i know this is really easy, and there are many chase cameras, 3rd person cameras on the forum, but this is really simple problem and it would be embarrasing not to solve it :P

what i want to do is: stick camera behind a node, a little bit above and behind it . like vector3df(0,20,-50).
the node is 'space ship' freely flying in space.

i've managed to make camera follow my node(behind it), but it transfroms upside down when node rotates every 90 degrees vertically.
i don't know how to translate matrix relatively to node :(

i've tried to follow CuteAlien's advices on IRC but i failed miserably doing this using matrices.

also i tried parenting, bindTargetAndRotation, but i couldn't achieve desired effect...

here's code for rotating the 'ship'(lame way):
(in short - the further cursor is from center of screen, the faster 'ship' rotates in that direction)

Code: Select all

void Ship::updateRot() {
        vector3df rotation(0,0,0);
        vector3df shrotation(0,0,0);

f32 xrot=0;
f32 yrot=0;
        if (my > halfy)
            xrot=(((my-halfy)*1.00/halfy)*2);
       else if (my < halfy)
            xrot=-(((halfy-my)*1.00/halfy)*2);
    else if (my == halfy) xrot=0;

      if (mx > halfx)
            yrot=(((mx-halfx)*1.00/halfx)*2);
       else if (mx < halfx)
            yrot=-(((halfx-mx)*1.00/halfx)*2);
    else if (mx == halfx) xrot=0;
      rotation.X=xrot;
      rotation.Y=yrot;
      shrotation=shipnode->getRotation();
      std::cout << shrotation.X << "  " << shrotation.Y << "  " << shrotation.Z << std::endl;
        shipnode->setRotation(shrotation+rotation);

}
halfx,y are X,Y screen resolutions divided by 2, for 'optimization' :P

and main loop(cut and edited with only related functions left):

Code: Select all

while (device->run()) {
         if (device->isWindowActive()) {
                driver->beginScene(1,1,SColor(255,155,5,5));
                              
                playership.updateRot();
                playership.updatePos();  //move ship constant value forward
                          srot=playership.shipnode->getRotation().rotationToDirection().normalize();
              
        cam->setPosition(playership.shipnode->getPosition()-20*srot);
        cam->setRotation(playership.shipnode->getRotation());
           smgr->drawAll();
   driver->endScene();
         }

if someone could provide proper solution, preferably using matrices, i would be very thankful :)
cr33
Posts: 22
Joined: Fri Mar 27, 2009 3:37 pm

Post by cr33 »

i really need just a clue. this is really simple but i haven't done this before
Post Reply