quaternion 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
kamikaze942
Posts: 52
Joined: Wed Mar 03, 2010 7:11 pm

quaternion camera

Post by kamikaze942 »

does anyone have a tutorial or code snippets of how to rotate a camera with quaternions? I've plowed through the tutorials, and i thought I saw one in the past, but haven't been able to find one. :(
Kamikaze
Jake-GR
Posts: 41
Joined: Wed Jan 07, 2009 12:32 am
Location: Colorado
Contact:

Post by Jake-GR »

this one might be worth looking into
not sure if its quaternion or not (havent looked myself)
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=26526
kamikaze942
Posts: 52
Joined: Wed Mar 03, 2010 7:11 pm

Post by kamikaze942 »

i think he is using the FPS camera, thethered behind the ship
Kamikaze
kamikaze942
Posts: 52
Joined: Wed Mar 03, 2010 7:11 pm

Post by kamikaze942 »

anyone? I think my idea is the following

i believe you have two quaternions

quat1 (node->getrotation * degtorad, 1)

quat2 (axis, angle)

quat1 *= quat2;

quat1(toeuler) = new vector

node->setrotation(new vector);

is this close?
Kamikaze
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

well, what exactly do you mean by "rotate"? what is the effect you want to achieve? and why do you want to use quaternions for it?

Conversions are here:
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=38283
kamikaze942
Posts: 52
Joined: Wed Mar 03, 2010 7:11 pm

Post by kamikaze942 »

I want to rotate the camera in the X and Y axis's. (up, down, left, right).

the class i have right now which is suffering from gimbal lock, as i'm not using quaternions correctly is...

Code: Select all

void CameraRotate(irr::scene::ISceneNode *node, irr::f32 cameraangle, irr::core::vector3df axis)
{
	vector3df rotation;
	quaternion first;
	//create quaternion from angle and axis
	first.fromAngleAxis(cameraangle, axis);
	first.normalize();
	//converting angle and axis to euler angle
	first.toEuler(rotation);
    //getting current rotation of your node
	vector3df currentrot = node->getRotation();

	matrix4 m1;
	//setup your first matrix with your current rotation 
	m1.setRotationDegrees(currentrot);
	matrix4 m2;
	//setup your second matrix with your quaternion angle and axis 
	m2.setRotationDegrees(rotation);
	//multiply together 
	m1 *= m2;
	//set node to your resultant rotation 
	node->setRotation(m1.getRotationDegrees());
	
	
}
Kamikaze
xDan
Competition winner
Posts: 673
Joined: Thu Mar 30, 2006 1:23 pm
Location: UK
Contact:

Post by xDan »

you have to convert the result from toEuler to degrees.

first.toEuler(rotation);
rotation *= core::RADTODEG;

I guess your code using only quaternions would be something like:

Code: Select all

void CameraRotate(irr::scene::ISceneNode *node, irr::f32 cameraangle, irr::core::vector3df axis)
{
   quaternion first;
   //create quaternion from angle and axis
   first.fromAngleAxis(cameraangle, axis);
   first.normalize();
   
	
	core::quaternion qCurrentRot( node->getRotation() * core::DEGTORAD );
	qCurrentRot = first * qCurrentRot;
	
	core::vector3df newRotEuler;
	qCurrentRot.toEuler(newRotEuler);
	newRotEuler *= core::RADTODEG;
	node->setRotation(newRotEuler);
   
}
Not tested. Also I don't know if the order of multiplication matters.
kamikaze942
Posts: 52
Joined: Wed Mar 03, 2010 7:11 pm

Post by kamikaze942 »

well it seems to move alot faster, but it still appears to suffer from gimbal lock, at some points ill be moving the Y axis up and down and will instead around the Z axis. =(

for cameraangle and axis im passing in this values (don't know if they make a difference)

cameraangle (-0.2f) or (0.2f) for direction

axis = (0.0f, 0.3f, 0.0f) for moving in Y direction
Kamikaze
Post Reply