Correct Kinematic Object

zarfius
Posts: 6
Joined: Wed Jan 07, 2009 11:19 pm

Correct Kinematic Object

Post by zarfius »

Hi, I've been playing with Bullet for a few weeks now in my spare time. I have also tried a few of the other physics SDK's available and I keep coming back to Bullet because it seems to be well written and feels right :)

Anyway, as we all know, Bullet is still in development and a few features are not perfect yet like the character controller. So what I would like to do is start developing my own using a kinematic approach.

The user manual mentions the basic idea behind kinematic objects, being rigid bodies animated by the user that push away dynamic bodies but have no influence from dynamic bodies.

It seems to me that kinematic bodies are ideal for character controllers, but also for other things found in games like moving platforms.

I don't fully understand the demo character controller, so what I would like is a minimal example of how to setup a kinematic object.

Thanks
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Correct Kinematic Object

Post by B_old »

If you have code to add a dynamic object to the simulation, you don't have to change very much.
Here is what I use (more or less):

Code: Select all

//Bullet will query yourMotionState.getWorldTransform() every frame to position the object in the simulation, so thats what you can use to move the body around
btRigidBody::btRigidBodyConstructionInfo rbInfo(btScalar(0), yourMotionState, shape, btVector3(btScalar(0), btScalar(0), btScalar(0)));
btRigidBody* body = new btRigidBody(rbInfo);

body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
body->setActivationState(DISABLE_DEACTIVATION);

m_dynamicsWorld->addRigidBody(body);
zarfius
Posts: 6
Joined: Wed Jan 07, 2009 11:19 pm

Re: Correct Kinematic Object

Post by zarfius »

Thanks for the reply, although I got this far already and it still doesn't work the way I expected.

If I understand correctly, a kinematic object is kind of the reverse of a dynamic object in the sense rather than the motion state telling where the object should be displayed, it's the displayed object reporting it's position back to the physics engine via getWorldTransform().

I am using Ogre so I tried to setup my MotionState class using the examples found here:
http://www.bulletphysics.com/mediawiki- ... tionStates

The problem is, it seems that even though I can change the position using getWorldTransform(), it never seems to update the location of my SceneNode because it appears that setWorldTransform() is never called.

I did manage to get some movement working by putting the code that sets the SceneNode into getWorldTransform() instead.

More importantly, the end result is still not pushing dynamic objects away or colliding with static objects.
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Correct Kinematic Object

Post by B_old »

As you said yourself setWorldTransform() will never be called. There is no real reason for it. The physicssimulation calls getWorldTransform() and does not change it, so no new transform has to be set.
I use the same MotionState for both dynamic and kinematic objects. It is very simple and looks more or less like this:

Code: Select all

void MotionState::getWorldTransform(btTransform &transform) const
{
	Vector3 pos = m_object->getPos();
	transform.setOrigin(btVector3(btScalar(pos.x), btScalar(pos.y), btScalar(pos.z)));

	Matrix3 rot = m_object->getRot();
	transform.setBasis(btMatrix3x3(btScalar(rot.m00), btScalar(rot.m10), btScalar(rot.m20), 
			                         btScalar(rot.m01), btScalar(rot.m11), btScalar(rot.m21), 
						                btScalar(rot.m02), btScalar(rot.m12), btScalar(rot.m22)));
}


void MotionState::setWorldTransform(const btTransform &transform)
{
	btScalar m[16];
	transform.getOpenGLMatrix(m);

	m_object->setPos(Vector3(m[12], m[13], m[14]));
	m_object->setRot(Matrix3(m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10])); 
}
Maybe if you substitute the m_object parts with the corresponding methods of a Ogre scenenode, it will work for you, too.
If I move an object around and it has a kinematic body attached, the physicssimulation will automatically know the new position by querying getWorldTransform().

Kinematic bodys will never collide with static bodies. They move exactly the way you tell them to.
I don't understand why they don't push away dynamic bodies for you though.
zarfius
Posts: 6
Joined: Wed Jan 07, 2009 11:19 pm

Re: Correct Kinematic Object

Post by zarfius »

Thanks for the help.

I have decided that I am not going to use Bullet for my physics layer. Instead I am moving to PhysX.

It's not that I don't like Bullet, but at this stage I think it's lacking some important features and in particular documentation. I'm very surprised that such a common task such as a working character controller doesn't really exist, or if it does I can't seem to find it.

It's one thing to have fast, accurate physics with tearing cloth and soft bodies, but when it comes to game development those things are really just "nice to haves".

When it comes to the important things like being able to move a character around the level, I would have thought that'd be something that many people have implemented and there would be plenty of info on how to do it correctly.

Don't get me wrong, I can understand there are many situations when kinematic objects would never need to collide with static ones, but there are also many other times when they should.

I also realise that it's up to the developer how each object reacts to a collision, but would it be too much to ask for an option to simply "collide and slide" as one would expect to see in 90% of games.
donn
Posts: 1
Joined: Fri Jan 16, 2009 3:31 pm

Re: Correct Kinematic Object

Post by donn »

Why not make your object Static instead of Kinematic?
You can still constantly update the position of a static object,
and you will still get collision detection with dynamic objects.
zarfius
Posts: 6
Joined: Wed Jan 07, 2009 11:19 pm

Re: Correct Kinematic Object

Post by zarfius »

donn wrote:Why not make your object Static instead of Kinematic?
You can still constantly update the position of a static object,
and you will still get collision detection with dynamic objects.
Your kidding right? If that works then I think there are some issues with the documentation. This is taken from the User Manual:

Static rigidbodies
- cannot move but just collide
- zero mass

Kinematic rigidbodies
- animated by the user
- only one?way interaction: dynamic objects will be pushed away but there is no influence from dynamics objects
- every simulation frame, dynamics world will get new world transform using btMotionState::getWorldTransform


I'm not saying your wrong about that, but it doesn't seem like you'd get very far going against what things where designed for.

Again, the real issue is that Bullet has yet to prove that it can create a working character controller. If the Bullet developers are having trouble doing it, what hope has anyone else got?
B_old
Posts: 79
Joined: Tue Sep 26, 2006 1:10 pm

Re: Correct Kinematic Object

Post by B_old »

zarfius wrote: Again, the real issue is that Bullet has yet to prove that it can create a working character controller. If the Bullet developers are having trouble doing it, what hope has anyone else got?
What's wrong with the character controller as shown in the CharacterDemo?
zarfius
Posts: 6
Joined: Wed Jan 07, 2009 11:19 pm

Re: Correct Kinematic Object

Post by zarfius »

B_old wrote:What's wrong with the character controller as shown in the CharacterDemo?
This is from the btKinematicCharacterController.cpp file provided with what appears to be the latest version of the Bullet SDK.

Code: Select all

///@todo Interact with dynamic objects,
///Ride kinematicly animated platforms properly
///More realistic (or maybe just a config option) falling
/// -> Should integrate falling velocity manually and use that in stepDown()
///Support jumping
///Support ducking
Now, I am not saying that the demo's should do everything for us, but I haven't been able to find out how to implement interactions with dynamic objects correctly anywhere.

Since I posted this thread I have managed to implement a working character controller in PhysX. It took about 10 hours total including reading the documentation and I didn't have to search any forums.

Once there is more documentation on kinematic to dynamic interactions I would consider using Bullet in a project. At this stage it was easier to pull out all the Bullet code and replace it with PhysX code. It wasn't what I wanted to do, but after about 40 hours of looking for answers I felt this path was easier. I just wanted to let the Bullet community know about my experience because it's likely there are other users feeling the same way.