Page 1 of 1

Using Bullet for Camera Physics

Posted: Sat Jun 11, 2011 6:42 pm
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);

Re: Using Bullet for Camera Physics

Posted: Sat Jun 11, 2011 7:10 pm
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


Posted: Sun Jun 12, 2011 6:30 am
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

Posted: Sun Jun 12, 2011 9:02 am
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.

Posted: Sun Jun 12, 2011 7:39 pm
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);

Posted: Sun Jun 12, 2011 7:53 pm
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.

Posted: Sun Jun 12, 2011 7:57 pm
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.

Posted: Sun Jun 12, 2011 8:06 pm
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

Posted: Sun Jun 12, 2011 8:26 pm
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.

Posted: Sun Jun 12, 2011 8:32 pm
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

Re: Using Bullet for Camera Physics

Posted: Fri Jul 04, 2014 2:14 pm
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