Page 1 of 1

Get a mesh to move the way its facing?

Posted: Tue Nov 27, 2007 3:04 am
by buzz44
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.

Posted: Tue Nov 27, 2007 11:27 am
by SwitchCase
Your question is quite vague but...

You could manually position it each frame, use an animator, or let a physics library handle it.

If you read through the turorials you'll find that they show you how to do each of these methods.

Posted: Tue Nov 27, 2007 11:30 am
by rogerborg
Snippet:

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);
Full example app:

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;
}

Posted: Tue Nov 27, 2007 6:56 pm
by overburn
hmm.. an ex ogre user on irrlicht... this is going to get interesting