Using Bullet for Camera Physics

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
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Using Bullet for Camera Physics

Post by RageD »

Is anyone familiar with using bullet (or irrBullet wrapper for that matter) to add collision detection and physics to the camera? Or can this only be done using irrlicht's collision animators?

Also, just a quick aside; I was having issues with IBvhTriangleMeshShape*, so I fixed things up a bit (i.e. I could not create it with buildbvh set to true). So basically, a second constructor I have for IBvhTriangleMeshShape* looks something like this:

Code: Select all

btTriangleMeshShape * shape = new btTriangleMeshShape(getTriangleMesh(mesh), true, false);
In any case, that works. However, I have been reading that this is particularly optimized for maps and static objects (which is what I was using it for - the map), but using it I get merely half the FPS that I do using GImpact - anyone have any ideas?

Primarily though, I am looking for some help with getting realistic collisions to happen with my camera :) Thanks!

-RageD

P.S. I am currently trying something like this:

Code: Select all

	IBoxShape * box = new IBoxShape(camera, 1.0, false);
	IRigidBody * cam = world->addRigidBody(box);
	cam->setGravity(core::vector3df(0,0,0));
	box->setLocalScaling(camera->getScale(), ESP_VISUAL);
	cam->setCollisionFlags(ECF_CHARACTER_OBJECT);
	cam->activate(true);
DuF = (del)F.u
Microsonic Development
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Using Bullet for Camera Physics

Post by randomMesh »

I use a convex sweep test in my bullet camera animator to prevent the camera going through static geometry.

This is directly pasted from the animateNode() method:

Code: Select all

//other stuff


	//use the convex sweep test to find a safe position for the camera (not blocked by static geometry)
	btVector3 characterWorldTrans;
	playerPos.getAs4Values(characterWorldTrans.m_floats);

	btVector3 desired_cameraPosition = characterWorldTrans + offset;

	btSphereShape cameraSphere(0.5f);
	btTransform cameraFrom, cameraTo;
	cameraFrom.setIdentity();
	cameraFrom.setOrigin(characterWorldTrans);
	cameraTo.setIdentity();
	cameraTo.setOrigin(desired_cameraPosition);


	btCollisionWorld::ClosestConvexResultCallback cb(characterWorldTrans, cameraTo.getOrigin());
	cb.m_collisionFilterMask = btBroadphaseProxy::StaticFilter;

	this->dynamicsWorld->convexSweepTest(&cameraSphere, cameraFrom, cameraTo, cb);
	if (cb.hasHit())
	{
		const btScalar minFraction = cb.m_closestHitFraction; //btMax(btScalar(0.3), cb.m_closestHitFraction);
		desired_cameraPosition.setInterpolate3(cameraFrom.getOrigin(), cameraTo.getOrigin(), minFraction);
	}

//other stuff

"Whoops..."
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Post by RageD »

Looks good - would this work for a first person camera? Basically, at the moment, I'm still using the Irrlicht FPS camera until I have the chance to work on my own. That said, how are you updating the scene node? Is all of your movement, etc. going through bullet and no longer being rendered through irrlicht's cameras?

-RageD
DuF = (del)F.u
Microsonic Development
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

RageD wrote:Looks good - would this work for a first person camera? Basically, at the moment, I'm still using the Irrlicht FPS camera until I have the chance to work on my own. That said, how are you updating the scene node? Is all of your movement, etc. going through bullet and no longer being rendered through irrlicht's cameras?

-RageD
You can update node either by using custom motion state or just by having a list of object and updating them manually.
Bullet does not render anything, you just make the camera follow the physics body you assigned to it.
Working on game: Marrbles (Currently stopped).
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Post by RageD »

Ah, I understand. Now it seems I'm having issues moving objects in the scene though (I actually hadn't tested this feature yet). I have tried to mess around with motion states and things but it seems like a no go - any ideas?

Basically the setup is this:

Code: Select all

// Add cube node with irrlicht
// Create an IBoxShape* (btBoxShape* without irrBullet) and add it to world
// Try to move
When I start, it falls properly and collides with the map; I just can't move it. If I try to move it with irrlicht, it doesn't collide with anything (I am assuming this is because bullet is not updating its position, etc.)

-RageD

NOTE: I've even tried something like this to simply illicit a response - I'm getting nothing

Code: Select all

btTransform t = player->getCollision()->getPointer()->getWorldTransform();
t.setRotation(btQuaternion(1,1,1,1));
player->getCollision()->getPointer()->setWorldTransform(t);
Last edited by RageD on Sun Jun 12, 2011 7:56 pm, edited 1 time in total.
DuF = (del)F.u
Microsonic Development
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

RageD wrote:Ah, I understand. Now it seems I'm having issues moving objects in the scene though (I actually hadn't tested this feature yet). I have tried to mess around with motion states and things but it seems like a no go - any ideas?

-RageD
I haven't made my physics implementation work with motionstates yet, but I think they would make things a bit nicer for me.

Have you read this article:http://www.bulletphysics.org/mediawiki- ... tionStates? the ogre3D integration part should help you understand how you should make it work with irrlicht.
Working on game: Marrbles (Currently stopped).
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

RageD wrote:Ah, I understand. Now it seems I'm having issues moving objects in the scene though (I actually hadn't tested this feature yet). I have tried to mess around with motion states and things but it seems like a no go - any ideas?

-RageD

NOTE: I've even tried something like this to simply illicit a response - I'm getting nothing

Code: Select all

btTransform t = player->getCollision()->getPointer()->getWorldTransform();
t.setRotation(btQuaternion(1,1,1,1));
player->getCollision()->getPointer()->setWorldTransform(t);
Quaternion rotations are a bit hard to understand at first.
I think I posted some code that rotates a node by some value of degrees.
*searches for the post*
There is the code, I hope its the right one because I had to fix it a few times.

Code: Select all

void rotateRidigBody(const btScalar & angle, const btVector3 & axis, CRigidBody * body) 
{ 
    btTransform trans; 
    trans=body->getWorldTransform(); 

    btQuaternion quat,quat2; 

    quat2.setW(cos(angle*irr::core::DEGTORAD/2)); 
    quat2.setX(axis.getX()*sin(angle*irr::core::DEGTORAD/2)); 
    quat2.setY(axis.getY()*sin(angle*irr::core::DEGTORAD/2)); 
    quat2.setZ(axis.getZ()*sin(angle*irr::core::DEGTORAD/2)); 

    trans.setRotation(quat2*quat); 
    body->setWorldTransform(trans); 
} 
first parameter is angle by degrees like 90,45,10 etc. the second should be axis on which you want the body to rotate i.e.: btVector3(0,1,0); would rotate the body on Y axis. CRigidBody is just a derived class from btRigidBody, so you can just replace it with btRigidBody.

Note:This function appends the rotation to last body's rotation, I needed it to be like this for my editor.
Working on game: Marrbles (Currently stopped).
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Post by RageD »

Alright, I have adapted your code to rotate about a normalized vector which should allow for a rotation about the Y and Z axes, however, it did not work. Perhaps this is some other problem since all of my transforms are having no effect. Are you able to move objects linearly?

As a side note, I am not very good with quaternion coordinates; do you know of any reading material that I can find? I can google ;) so if you can't think of anything off the top of your head, don't worry about it. Even though that my intention is not really rotation right now, it will be useful in the future. Currently, I'm only trying to get some sort of motion from my object; I'm looking, overall, for proper linear movement, then I can begin to worry about rotations xD

Thanks so much for your help.

-RageD

EDIT: Sorry, I'm new to using bullet xD. But I found my first naive mistake; I didn't zero out the gravity for the object... Now to figure out the rest of the problems xD
DuF = (del)F.u
Microsonic Development
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

RageD wrote:Alright, I have adapted your code to rotate about a normalized vector which should allow for a rotation about the Y and Z axes, however, it did not work. Perhaps this is some other problem since all of my transforms are having no effect. Are you able to move objects linearly?

As a side note, I am not very good with quaternion coordinates; do you know of any reading material that I can find? I can google ;) so if you can't think of anything off the top of your head, don't worry about it. Even though that my intention is not really rotation right now, it will be useful in the future. Currently, I'm only trying to get some sort of motion from my object; I'm looking, overall, for proper linear movement, then I can begin to worry about rotations xD

Thanks so much for your help.

-RageD
Well, I had hard time finding some understandable explanation on how quaternions work so I hacked the code I found while googling and made it to work like I needed.

If you need to move the visual node that has physics body assigned to it, you simply can try:

Code: Select all

player->getCollision()->getPointer()->translate(btVector3(1.0f,0.0f,0.0f));
Since you use a wrapper, it should update the node on its own.
Working on game: Marrbles (Currently stopped).
RageD
Posts: 34
Joined: Mon Jun 13, 2005 2:34 pm

Post by RageD »

Perfect! That translate works very well - exactly what I was looking for. Now to actually sort out the collision detection so it doesn't get stuck :) thanks!

-RageD
DuF = (del)F.u
Microsonic Development
emadof85
Posts: 31
Joined: Sun May 25, 2014 6:10 pm

Re: Using Bullet for Camera Physics

Post by emadof85 »

hi all

we tried to put this code in our project but we couldn't do that

can any one gives us a little help with some steps that we can follow to make this code works
Post Reply