[solved] type casting vectors. btVector --> vector3df

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
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

[solved] type casting vectors. btVector --> vector3df

Post by marrabld »

Hi can anyone help explain why when I try to synchronise my irrlicht node with my bullet node I get a seg fault

My Code ::

Code: Select all


// create test cube
        scene::ISceneNode* test = smgr->addCubeSceneNode(60);

        // let the cube rotate and set some light settings
        scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
                core::vector3df(0.3f, 0.3f,0));

        test->setPosition(core::vector3df(0,0,500));
        test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
        test->addAnimator(anim);
        anim->drop();

btVector3 testVect = physics.body->getCenterOfMassPosition();
  test->setPosition(core::vector3df((f32)testVect[0], (f32)testVect[1], (f32)testVect[2]));
I get the seg fault at the last line according to my debugger. Which then points me to this line in vector3d.h

Code: Select all

vector3d(T nx, T ny, T nz) : X(nx), Y(ny), Z(nz) {}
Seems to me that its having trouble casting between the two vector types but I cant see what is wrong with my code.

Any help would be much appreciated.
Last edited by marrabld on Tue May 19, 2009 12:43 pm, edited 1 time in total.
erwincoumans
Posts: 46
Joined: Tue Oct 02, 2007 6:46 am
Contact:

Post by erwincoumans »

Where is the cast between vectors in your code?
Generally, a Bullet btVector3 has to be 16-byte aligned, and it has 4 components.

You might want to check out this gamekit project, it uses the irrMotionState so synchronize transforms between Bullet and Irrlicht:
http://code.google.com/p/gamekit/source ... in.cpp#266

http://gamekit.googlecode.com
Hope this helps,
Erwin
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Thanks for the reply Erwin,

I have looked at your code, very impressive. However, I am still not sure why my code isn't working.

What is the difference between my code

Code: Select all

test->setPosition(core::vector3df((f32)testVect[0], (f32)testVect[1], (f32)testVect[2]));
And your code

Code: Select all

m_irrNodes[i]->setPosition(core::vector3df((f32)Point[IRR_X], (f32)Point[IRR_Y], (f32)Point[IRR_Z])/physicsWorldScaling);
What am I missing?

cheers
kh_
Posts: 78
Joined: Fri May 19, 2006 4:29 pm

Post by kh_ »

I think your probably wrong on the vector being the cause.I've used this code without problem:

Code: Select all

 btVector3 Point = BallBody->getCenterOfMassPosition();
            Node->setPosition(vector3df((f32)Point[0], (f32)Point[1], (f32)Point[2]));
Before asking for help I would do at least 2 things first.

1. Test all pointers.

Code: Select all


///physics.body should have been initialized to 0
if(physics.body &&  test) //or use try/catch block
{

btVector3 testVect = physics.body->getCenterOfMassPosition();
  test->setPosition(core::vector3df((f32)testVect[0], (f32)testVect[1], (f32)testVect[2]));
}
else
{  print error,either test these everywhere else or cleanup and exit        program.
}
Edit:As Vitek suggests below,assert is easier to use, but personally I never use it (C# programmer). It aborts the program without calling any destructors or giving you any chance to do any cleanup yourself.


2.Try to reproduce the problem in the simplest test possible and 9 times out of 10 you will find the bug yourself. If you can reproduce the bug, post the full code to make it easier for people to help you.

I should add, you don't normally want to attach a move/rotate type animator to a node that is going to be physics controlled.
Last edited by kh_ on Mon May 18, 2009 6:51 pm, edited 1 time in total.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Re: type casting vectors. btVector --> vector3df

Post by vitek »

marrabld wrote:

Code: Select all

btVector3 testVect = physics.body->getCenterOfMassPosition();
test->setPosition(core::vector3df((f32)testVect[0], (f32)testVect[1], (f32)testVect[2]));
The simplest thing to do is to break this down into multiple parts to see if/why it is crashing.

Code: Select all

assert (0 != physics.body);
const btVector3 testVect = physics.body->getCenterOfMassPosition();

const f32 x = testVector[0];
const f32 y = testVector[1];
const f32 z = testVector[2];

const core::vector3df v (x, y, z);

assert (0 != test);
test->setPosition(v);
Travis
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

OK, appreciate the help so far.

the revised code after taking on your comments is

Code: Select all

 	if(physics.body && test)
{
	btVector3 testVector = physics.body->getCenterOfMassPosition();

	const f32 x = testVector[0]; 
	const f32 y = testVector[1];
	const f32 z = testVector[2];

	const core::vector3df v (x, y, z); 

	assert (0 != test);
//	test->setPosition(v); 

//	cout<<"no error!" << x <<endl;
}
	else
{
	cout<<"error!"<<endl;
}
if I leave

Code: Select all

//	test->setPosition(v); 
//	cout<<"no error!" << x <<endl;
commented it runs fine. If I un-comment either of them, it seg faults. What is more confusing is, if I run in my debugger and try to break point anywhere in game.cpp, it segfaults at game.run() in my main.cpp. I thought that I would break point, call stack and see what was stored in x or testvector. This approach usually works for me.

I thought it might crash if physics or body weren't initialised properly but the "if(physics.body && test)" should take care of that, right? I get a "no error!" at the command line if I leave the <<x<<endl; bit out, but it crashes the second I include it.

I am a little confused. Any other ideas on how I can debug this problem ? Thanks
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

P.S I did remove the animator, it was an artefact of the demo I hacked for testing purposes. It has made no difference
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

From the sound of it, test is a non-null pointer, but it doesn't point to a valid ISceneNode instance. Either that, or your Irrlicht.dll is out of date with respect to the headers that you are compiling against.

Travis
kh_
Posts: 78
Joined: Fri May 19, 2006 4:29 pm

Post by kh_ »

marrabld wrote:
I thought it might crash if physics or body weren't initialised properly but the "if(physics.body && test)" should take care of that, right?
Thanks
Not necessarily true. It also depends on if you initialize the body member to zero.It could be garbage.

It's almost impossible for anyone to help you when you don't show ALL your code. (At least show your Physics class constructor(s))

Can you get the irrlicht examples to run/debug with no problems?
A good place to start is to get it to run and just set the node position without calling on any physics stuff.Then if it works the problem is most likely in your physics code.
marrabld
Posts: 37
Joined: Wed Jul 11, 2007 9:16 am

Post by marrabld »

Thanks for you help guys.

A bit embarrassed to admit. In my constructor I used

Code: Select all

 btRigidBody* body = new btRigidBody(rbInfo);
When I should have had

Code: Select all

  body = new btRigidBody(rbInfo);
As I had allready defined body as a member in the class

:-( I won't make that mistake again.
Post Reply