getPosition() and getAbsolutePosition()

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
wsw1231
Posts: 148
Joined: Fri Oct 01, 2010 7:55 am

getPosition() and getAbsolutePosition()

Post by wsw1231 »

I do not quite understand the difference between those 2 APIs.

In ISceneNode.h, it states that getAbsolutePosition() is used to retrieve the world coordinates, while getPosition() is used to get the coordinates relative to the parent.

What is the meaning of world coordinates?
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

The world coordinates are the absolute position...

Maybe this will make thing clearer:

|----A(5)------B(20)------->


Lets say A is the parent of B.
A has the absolute positon of 5. Its relativ position is the same since it doesn't have a parent.
B has the absolute position of 20. Its relative position is 15, becase if you go 15 units from A (which has the position 5) you reach 20 which is the B's absolute position.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
yaten
Posts: 41
Joined: Tue Nov 16, 2010 4:57 pm
Location: /home/yaten on a server somewhere East of Asia
Contact:

Post by yaten »

hi sir wsw, in how i understand the command and based on my experience, the getPosition returns the position of the object relative to the parent, so if your object doesn't have any parent, getPosition will have the same result as getAbsolutePosition

I can best demostrate it with an example, see below the source code based on stripped down version of example 04:

Code: Select all

/* Example 004 Movement*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
#include <irrlicht.h>
#include "driverChoice.h"
using namespace irr;
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
		return false;
	}
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	
	MyEventReceiver()
	{
		for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main()
{
	video::E_DRIVER_TYPE driverType=driverChoiceConsole();
	if (driverType==video::EDT_COUNT)
		return 1;
	MyEventReceiver receiver;
	IrrlichtDevice* device = createDevice(driverType,
			core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
	if (device == 0)
		return 1;
	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	scene::ISceneNode * node = smgr->addSphereSceneNode();
	if (node)
	{
		node->setPosition(core::vector3df(0,0,30));
		node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp"));
		node->setMaterialFlag(video::EMF_LIGHTING, false);
	}

	scene::ISceneNode* n = smgr->addCubeSceneNode();
	if (n)
	{
		n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
		n->setParent(node); // <-------------- comment/uncomment this part and compare
		n->setPosition(core::vector3df(10,0,30));
		n->setMaterialFlag(video::EMF_LIGHTING, false);
	}
	smgr->addCameraSceneNodeFPS();
	device->getCursorControl()->setVisible(false);

	gui::IGUIStaticText* diagnostics = device->getGUIEnvironment()->addStaticText(
		L"", core::rect<s32>(10, 10, 400, 20));
	diagnostics->setOverrideColor(video::SColor(255, 255, 255, 0));

	int lastFPS = -1;

	u32 then = device->getTimer()->getTime();

	const f32 MOVEMENT_SPEED = 5.f;

	while(device->run())
	{
		// Work out a frame delta time.
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
		then = now;

		/* Check if keys W, S, A or D are being held down, and move the
		sphere node around respectively. */
		core::vector3df nodePosition = node->getPosition();

		if(receiver.IsKeyDown(irr::KEY_KEY_W))
			nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;

		if(receiver.IsKeyDown(irr::KEY_KEY_A))
			nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_D))
			nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;

		node->setPosition(nodePosition);

		driver->beginScene(true, true, video::SColor(255,113,113,133));

		smgr->drawAll(); // draw the 3d scene
		device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			core::stringw tmp(L"Movement Example - Irrlicht Engine [");
			tmp += driver->getName();
			tmp += L"] fps: ";
			tmp += fps;
			tmp += L" Pos: ";
			tmp += (u32)n->getPosition().X;
			tmp += L", ";
			tmp += (u32)n->getPosition().Y;
			tmp += L", ";
			tmp += (u32)n->getPosition().Z;
			tmp += L" Ab Pos: ";
			tmp += (u32)n->getAbsolutePosition().X;
			tmp += L", ";
			tmp += (u32)n->getAbsolutePosition().Y;
			tmp += L", ";
			tmp += (u32)n->getAbsolutePosition().Z;

			device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
	}
	device->drop();
	
	return 0;
}
on line 58 you will notice that I inserted a line :
n->setParent(node);
run the program and look at the windows caption,
comment out the line and run it again ^_^

I hope that helps.

EDIT: sir sylence beats me with his explanation by a minute hahaha ^_^
Last edited by yaten on Mon Jan 03, 2011 3:55 pm, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

There's one global coordinate system, where all objects exist in. Each object also has a local coordinate system, where vertices are defined in. You wouldn't want to define your plane with vertices at 1.000.000 units away, just because it exists there. Instead, you define it around (0,0,0) and virtually move it over to whereever it should be placed.
If you have a parent node, everything gets one hierarchy level more. Child nodes are placed in the local coordinate system of the parent. The parent is moved to some absolute position and the children follow due to the relative placement. If you call getAbsolutePosition on the child, you will still get the world coordinate, though, e.g. to test some collision against the child.
There are good text books on general 3d concepts...
Post Reply