[Tutorial] - How to collide moving nodes like lifts...

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Domin

[Tutorial] - How to collide moving nodes like lifts...

Post by Domin »

Sorry for my english - i just hope that someone can understand me and find this example usefull :)

First - where is the problem:
When we use getCollisionResultPosition function we must pass actual position and velocity of node (mainly main hero of our game). This function returns new position which describes location of our hero after collision. But there is no place to any positions and velocities of other moving nodes if our scene. So we must translate somehow this .. vectors to coordinate system of our main node. And this is done in this example. This describes only situation witch one moving node (e.g. lift) but you can easily expand it to list of moving nodes. All code below must be placed in animateNode() method if you want to write new node animator. If not, use it after all rendering stuff in main loop. This is only scheme not working example - I hope you find this usefull.

// boxnode - 'lift' - this is the node that affect our hero
// md2node - main hero

// First of we need to declare one global variable which holds
// last position of our 'lift'. At app start this should be filled
// with starting position of 'lift'
core::vector3df oldPos;

// Next vector needed describes actual position of 'lift'
core::vector3df newPos(boxnode->getPosition());

// Now another vector: startPosition
// To actual position of our main (hero) node we add vector which
// describes the movement of moving node ('lift') that must affect our hero.
// This moves back hero and makes it 'virtually unmoved' in relation to
// moving node. This vector is equal to: newPos - oldPos.
core::vector3df startPosition(md2node->getPosition() + (newPos - oldPos));

// Now we need to calculate 'virtual velocity' of hero.
// This is simply difference beetwen 'moved back' vector and
// actual position of hero.
core::vector3df vel(md2node->getPosition() - startPosition);

// Now comes some const values - this must be initialized in
// constructor of our node animator - I add it here to simplify
// this example
core::vector3df rad(100.0f, 100.0f, 100.0f);
core::triangle3df triangle;
bool f = false;
f32 slidingSpeed = 0.01f;
core::vector3df g(0.0f ,0.0f ,0.0f);

// This sets final position of our hero based on calculated virtual
// position and velocity
md2node->setPosition(smgr->getSceneCollisionManager()->getCollisionResultPosition(
boxnode->getTriangleSelector(), startPosition, rad, vel, triangle, f, slidingSpeed, g));

// Finally we need to remember actual position of 'lift'.
oldPos = boxnode->getPosition();

Now, when the 'lift' moves somewhere, the main hero should be 'pushed' by lift.
All comments and improvements are welcomed.
SwiftCoder
Posts: 32
Joined: Thu Jan 15, 2004 7:33 pm
Location: Carribean

Post by SwiftCoder »

Interesting idea,
But why not just set the hero's parent node to the lift when he/she collides with the lift, then he/she will automatically move with the lift, without all those calculations. (I am also thinking of vehicles, and moving floors, where you need the horizontal movement as well).

SwiftCoder
Post Reply