How to rotate a billboard, still facing to the camera?

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
Clash
Posts: 80
Joined: Thu Apr 19, 2007 4:20 pm

How to rotate a billboard, still facing to the camera?

Post by Clash »

Hello!

I want to rotate a billboard, but still make it look to the camera, like this:
Before Rotation:

Code: Select all

 _
|_|
After Rotation:

Code: Select all

/\
\/
I've used the search and they say a billboard can't be rotated, it should aways face the camera, but it will still be facing the camera!

If this is not possible, what kind of thing can I use to rotate stuff that should look 2D? I'm making a 2D Game

Thanks!
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Make custom node (look at custom node example). You can duplicate billboard scene node code. Only thing you have to change is position of vertices.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Code: Select all

#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

int main()
{
	IrrlichtDevice * device =
		createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 16,
			false, false, false, 0);
	IVideoDriver * driver = device->getVideoDriver();
	ISceneManager * smgr = device->getSceneManager();
	IGUIEnvironment * guienv = device->getGUIEnvironment();

	// Create a simple quad mesh
	SMeshBuffer * buffer = new SMeshBuffer();
	S3DVertex vertices[4];
	vertices[0] = S3DVertex(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, SColor(0, 255, 255, 255), 1.0f, 1.0f);
	vertices[1] = S3DVertex(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, SColor(0, 255, 255, 255), 1.0f, 0.0f);
	vertices[2] = S3DVertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, SColor(0, 255, 255, 255), 0.0f, 0.0f);
	vertices[3] = S3DVertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, SColor(0, 255, 255, 255), 0.0f, 1.0f);
	u16 indices[6] = { 0, 1, 2, 0, 2, 3 };
	buffer->append(vertices, 4, indices, 6);

	SMesh * mesh = new SMesh();
	mesh->addMeshBuffer(buffer);
	buffer->drop();
	// That's our quad mesh created

	// Create 3 mesh nodes that use it.
	IMeshSceneNode * nodes[3];
	
	for(int node = 0; node < 3; ++node)
	{
		nodes[node] = smgr->addMeshSceneNode(mesh);

		if (nodes[node])
		{
			nodes[node]->setMaterialFlag(EMF_LIGHTING, false);
			ITexture * texture = driver->getTexture("../../media/wall.bmp");
			nodes[node]->setMaterialTexture( 0, texture);
		}

		nodes[node]->setPosition(vector3df(node * 4.f, node * 3.f, 0.f));
	}
	mesh->drop();


	// Create an orthogonal (orthographic projection) camera
	ICameraSceneNode * camera = smgr->addCameraSceneNode(0, vector3df(0,0,-10), vector3df(0,0,0));
	matrix4 orthoMatrix;
	orthoMatrix.buildProjectionMatrixOrthoLH(20.f, 15.f, 5.f, 100.f);
	camera->setProjectionMatrix(orthoMatrix);
	camera->setIsOrthogonal(true);

	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));

		// Rotate the 3 nodes at different rates
		for(int node = 0; node < 3; ++node)
		{
			vector3df rotation = nodes[node]->getRotation();
			rotation.Z += 0.1f * (node + 1); // Framerate dependent!
			nodes[node]->setRotation(rotation);
		}

		smgr->drawAll();
		driver->endScene();
	}

	device->drop();

	return 0;
}
Last edited by rogerborg on Mon Nov 12, 2007 7:17 pm, edited 3 times in total.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

rotation.Z += 0.1f * (node + 1); // Not framerate dependent!
What? I'm pretty the rotation rate is both frame rate dependent and index dependent. Say rotation.Z for node[0] is 0.f. If the render loop is executed 100 times per second, after 1 second, rotation.Z will be 100 * (.1f * 1) => 10.f. But if the loop runs at 10 frames per second, after 1 second, rotation.Z will be 10 * (.1f * 1) => 1.f. You can easily prove it to yourself by inserting device->sleep(250); in the end of the main loop.

And while I'm being a code nazi, I should mention that it might be a good idea to drop the mesh and buffer.

Travis
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

vitek wrote:
rotation.Z += 0.1f * (node + 1); // Not framerate dependent!
What? I'm pretty the rotation rate is both frame rate dependent and index dependent.
You make one little typo, and get sent to live with your auntie and uncle in Bel-Air. ;)
And while I'm being a code nazi, I should mention that it might be a good idea to drop the mesh and buffer.
I haf obeyed ze orders, and fixed it.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Clash
Posts: 80
Joined: Thu Apr 19, 2007 4:20 pm

Post by Clash »

Thanks a lot rogerborg, vitek and arras! :D
GameCreator
Posts: 22
Joined: Wed Jun 08, 2005 2:44 pm

Post by GameCreator »

rogerborg, thanks a lot for the code!! I needed to do this as well.
Post Reply