Get a mesh to move the way its facing?

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
buzz44
Posts: 9
Joined: Wed Sep 12, 2007 6:53 am

Get a mesh to move the way its facing?

Post 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.
SwitchCase
Posts: 170
Joined: Sun Jul 01, 2007 11:41 pm
Location: Manchester, UK

Post 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.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post 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;
}
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
overburn
Posts: 101
Joined: Sun Nov 04, 2007 8:08 am
Location: Romania, Ploiesti

Post by overburn »

hmm.. an ex ogre user on irrlicht... this is going to get interesting
One Tequila, Two Tequila, Three Tequila, Floor.
_______________________________________
ASUS P5V-VM Ultra
Intel Pentium D930
1x 2GB DDR2 Kingmax 800mhz/533mhz mb
Leadtek PX9600GT W 512MB DDR3
200GB Maxtor HDD
Post Reply