True Axis Physics integration, rotation troubles... [SOLVED]

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
jimbo
Posts: 5
Joined: Thu Nov 08, 2007 8:27 am

True Axis Physics integration, rotation troubles... [SOLVED]

Post by jimbo »

Hi All,

I'm puzzling a bit with True Axis Physics integration by creating a scenenode animator. Problem is dat when adding a bunch of cubes to the scene their rotation looks distorted. Some rotate only over one angle and through the ground. Could this have anything to do with Gimbal Lock? Is there a way to overcome this with True Axis?

Code: Select all

 // Add some cubes with some randiness...
for (int i=0; i<30;i++) { 
 irr::f32 r=float(rand()%20);
 ISceneNode *cube = smgr->addCubeSceneNode(1+r,0,-1,irr::core::vector3df(300.0f+r,150.0f+(i*10),280.0f+r));
 CSceneNodeAnimatorTA *tok = new CSceneNodeAnimatorTA(cube, r);
 cube->addAnimator(tok);
 tok->drop();
}
Here's code from the animator:

Code: Select all

#include "SceneNodeAnimatorTA.h"

#define WORLD_SCALE 1.0f;

#define PI 3.1415926f
#define DEG2RAD(a) ((a)*PI/180) 
#define RAD2DEG(a) ((a)*180/PI) 

CSceneNodeAnimatorTA::CSceneNodeAnimatorTA(irr::scene::ISceneNode *node, irr::f32 mass, irr::s32 collissionID) {
 TA::Physics& physics = TA::Physics::GetInstance();

 irr::core::aabbox3df box = node->getBoundingBox();
 irr::core::vector3df extent = box.getExtent()/2;  // don't no why this is needed
 irr::core::vector3df center = box.getCenter();
 
 m_pDynamicObject = TA::DynamicObject::CreateNew();
	
 TA::AABB aabb(convertIrrlichtPos(center),convertIrrlichtPos(extent));
 m_pDynamicObject->InitialiseAsABox(aabb);
 m_pDynamicObject->SetMass(mass);

 irr::core::vector3df pos = node->getPosition();
 irr::core::vector3df rot = node->getRotation();

 TA::MFrame frame(convertIrrlichtPos(pos),convertIrrlichtRot(rot));
 m_pDynamicObject->SetFrame(frame);

 physics.AddDynamicObject(m_pDynamicObject);
 m_pDynamicObject->Release();
}

void CSceneNodeAnimatorTA::animateNode(irr::scene::ISceneNode *node, irr::u32 timeMs) {
 const TA::MFrame &frame = m_pDynamicObject->GetFrame();
 node->setPosition(convertTAPos(frame.v3Translation));
 node->setRotation(convertTARot(frame.m33Rotation));
}

TA::Vec3 CSceneNodeAnimatorTA::convertIrrlichtPos(irr::core::vector3df &pos) {
 TA::Vec3 tokpos(pos.X,pos.Y,pos.Z);
 return tokpos * WORLD_SCALE;
}

irr::core::vector3df CSceneNodeAnimatorTA::convertTAPos(const TA::Vec3 &p) {
 return irr::core::vector3df(p.x,p.y,p.z) / WORLD_SCALE;
}

TA::Mat33 CSceneNodeAnimatorTA::convertIrrlichtRot(irr::core::vector3df &r) {
 TA::EulerAngles tarot(DEG2RAD(r.X),DEG2RAD(r.Y),DEG2RAD(r.Z));
 return TA::Mat33(tarot);
}

irr::core::vector3df CSceneNodeAnimatorTA::convertTARot(const TA::Mat33 &r) {
 TA::EulerAngles rot((TA::Mat33&)r);
 return RAD2DEG(irr::core::vector3df(rot.x,rot.y,rot.z));
}
Last edited by jimbo on Mon Nov 12, 2007 10:03 am, edited 1 time in total.
jimbo
Posts: 5
Joined: Thu Nov 08, 2007 8:27 am

Post by jimbo »

To demonstrate the effect I've made little demo with sources which can be downloaded here:

http://corrado.bsdwebhosting.com/~jimbo/TrueAxis.zip
Slaine
Posts: 120
Joined: Fri May 04, 2007 12:28 pm

Post by Slaine »

Do the objects rotate 90 degrees, then back again, or something along those lines, if so there is a fix on here for that. Not sure where, if it is this a search on here for 90 degree rotation problem will return the fix.
jimbo
Posts: 5
Joined: Thu Nov 08, 2007 8:27 am

Post by jimbo »

I fixed it by copying the matrix and then let irrlicht convert, looks great now!:

Code: Select all

irr::core::vector3df CSceneNodeAnimatorTA::convertTARot(const TA::Mat33 &r) {
	irr::core::matrix4 mat;

	mat(0,0) = r(0,0);
	mat(0,1) = r(0,1);
	mat(0,2) = r(0,2);
	mat(0,3) = 0.0f;

	mat(1,0) = r(1,0);
	mat(1,1) = r(1,1);
	mat(1,2) = r(1,2);
	mat(1,3) = 0.0f;

	mat(2,0) = r(2,0);
	mat(2,1) = r(2,1);
	mat(2,2) = r(2,2);
	mat(2,3) = 0.0f;

	mat(3,0) = 0.0f;
	mat(3,1) = 0.0f;
	mat(3,2) = 0.0f;
	mat(3,3) = 1.0f;

	return mat.getRotationDegrees();
}
Post Reply