I'm used to Ogre and it has a MoveRelative function, and say the mesh is rotate and I want it to move forward, it will move forward the way its facing, but I can't seem to find a similar function in Irrlicht.
Is there one or another way?
Thanks.
Get a mesh to move the way its facing?
-
- Posts: 170
- Joined: Sun Jul 01, 2007 11:41 pm
- Location: Manchester, UK
-
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
Snippet:
Full example app:
Code: Select all
// Start with a direction vector that represents the
// direction that the model is 'facing', e.g. towards +Z
vector3df directionVector(0.f, 0.f, 1.f);
matrix4 rotationMatrix;
rotationMatrix.setRotationDegrees(node->getRotation());
rotationMatrix.rotateVect(directionVector);
// In a real app, multiply directionVector * speed * frame time
node->setPosition(node->getPosition() + directionVector);
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
#pragma comment(lib, "Irrlicht.lib")
int main()
{
IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480),
32, false, false, false);
if (device == 0)
return 1; // could not create selected driver.
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
// Note that the model in dwarf.x faces towards -Z. To have it face the cursor, we have to apply a correction
// to the calculated rotation. For a -Z facing, it's -90 degrees.
const float MODEL_CORRECTION = -90.f;
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( smgr->getMesh("../../media/dwarf.x") );
node->setMaterialFlag(EMF_LIGHTING, false);
ICameraSceneNode * camera = smgr->addCameraSceneNode(0, vector3df(0.f, 100.f, -100.f), vector3df(0.f, 0.f, 0.f));
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,113,113,133));
// Work out where the cursor is on the node's XZ plane
position2di const cursorPosition(device->getCursorControl()->getPosition());
plane3df const planeXZ(node->getAbsolutePosition(), vector3df(0.f, 1.f, 0.f));
line3df const ray(collMan->getRayFromScreenCoordinates(cursorPosition, camera));
vector3df intersectWithPlane;
if(planeXZ.getIntersectionWithLine(ray.start, ray.getVector(), intersectWithPlane))
{
vector3df const toTarget = intersectWithPlane - node->getAbsolutePosition();
// Do the trig to get the correct rotations (corrected for this mesh, which faces -Z).
vector3df const rotation(
(-atan2(vector2df(toTarget.Z, toTarget.X).getLength(), toTarget.Y) * RADTODEG) - MODEL_CORRECTION,
(atan2(-toTarget.Z, toTarget.X) * RADTODEG) + MODEL_CORRECTION,
0.f);
node->setRotation(rotation);
// Show the point that the model is following.
driver->setTransform(video::ETS_WORLD, core::matrix4());
driver->draw3DLine(node->getAbsolutePosition(), intersectWithPlane, SColor(255, 255, 0, 0));
}
// This accursed model faces -Z, so use that as the base direction vector
vector3df directionVector(0.f, 0.f, -1.f);
matrix4 rotationMatrix;
rotationMatrix.setRotationDegrees(node->getRotation());
rotationMatrix.rotateVect(directionVector);
node->setPosition(node->getPosition() + directionVector);
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way