Kinematic bodies with friction applied to dynamic bodies?

Post Reply
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Hello everyone,

I am enjoying my work with Bullet. What a great library.

I have a problem/question. I have an environment made up of a triangle mesh using a kinematic body. The environment moves (think of a room on a ship). I have dynamic bodies inside the environment (think people in the room on the ship). When I move the room (specifically, I am rotating it - so the room slowly spins), I want the people (dynamic bodies) to move with the floor. Right now, it doesn't work for me. I mean, collision and bounce work fine, but seems like no friction is applied.

Here's what I have:

My room, a triangle mesh with kinematic body.
A cube, which is a dynamic rigid body.
The room is (very) slowly rotating around it's vertical axis (like a carousel).

When I throw the cube in the room, It bounces off the walls and comes to rest as expected. However, it does not move with the floor (until it is hit by one of the walls, which then proceeds to push it). I am updating the room position/rotation using MotionState (same code as used in the ccdphysicsdemo) and both the room and the cube have friction coefficient of 0.8.

*EDIT* Is what I'm trying to do even possible?
*EDIT2* I forgot to mention that I am setting the kinematic body position directly, not setting a velocity or angular velocity through the motionState (well, I'm calculating a velocity myself, and then calculating the resulting position from that)... should I be using some function like setLinearVelocity or setAngularVelocity instead? Hmmm... I guess since there are no answers yet (I'm assuming because it's the weekend) people just haven't seen my question yet... Hopefully, someone can provide some insight...?

Any advice?

Chaster
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

*ping*... Anybody have any input on the proper way to handle this? (imparting friction from kinematic moving bodies to dynamic rigid bodies properly)? A hint perhaps? Or am I just trying to do something which isn't really possible?

Chaster
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Erwin Coumans »

This should just work. Can you check out a working example in the CcdPhysicsDemo: enable at the top of CcdPhysicsDemo.cpp the define

Code: Select all

#define USE_KINEMATIC_GROUND 1
The floor kinematically moves, and stack of cylinders moves with it due to friction.

Make sure you are setting these properties for the kinematic object:

Code: Select all

	if (mass == 0.f)
	{
		body->setCollisionFlags( body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
		body->setActivationState(DISABLE_DEACTIVATION);
	}
Hope this helps,
Erwin
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Update:

Okay, more testing reveals that linear motion seems to impart friction (although not quite sure how accurate) but angular motion of the kinematic body imparts very little (if any) friction force to the rigid body sitting on the kinematic body.

Is this a bug?

Chaster
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Erwin Coumans wrote:This should just work. Can you check out a working example in the CcdPhysicsDemo: enable at the top of CcdPhysicsDemo.cpp the define

Code: Select all

#define USE_KINEMATIC_GROUND 1
The floor kinematically moves, and stack of cylinders moves with it due to friction.

Make sure you are setting these properties for the kinematic object:

Code: Select all

	if (mass == 0.f)
	{
		body->setCollisionFlags( body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
		body->setActivationState(DISABLE_DEACTIVATION);
	}
Hope this helps,
Erwin
Oh, we must have posted at the same time. Yes, I based my code on the kinematic floor demo, and while linear motion seems to impart friction, angular (rotational) motion doesn't (at least, if it is, it is a lot less than friction imparted by linear motion of the kinematic body)... I notice that the kinematic floor only moves up and down, with no rotation - perhaps this is a bug which is not revealed because the demo does not test it?

Chaster
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Erwin Coumans »

Chaster wrote:
Erwin Coumans wrote: Oh, we must have posted at the same time. Yes, I based my code on the kinematic floor demo, and while linear motion seems to impart friction, angular (rotational) motion doesn't (at least, if it is, it is a lot less than friction imparted by linear motion of the kinematic body)... I notice that the kinematic floor only moves up and down, with no rotation - perhaps this is a bug which is not revealed because the demo does not test it?

Chaster
Rotational kinematic objects have been tested to work. You can download Blender 2.4x which uses Bullet physics, with the regression physics demos. Those tests includes a kinematic cube that is slowly rotating, with a stack of boxes following properly. The CcdPhysicsDemo ground is supposed to move sidewards, not up/down. Are you using the latest version of Bullet (2.63 of now) ?

Can you reproduce the issue in the CcdPhysicsDemo?
Thanks for the feedback,
Erwin
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Thanks for taking the time to reply Erwin. Really appreciate it.

I am currently compiling 2.63 to compare with what is going on in my simulation. I am glad that it should work - that narrows it down to something I am doing wrong (not surprising since I am new to Bullet). I will report back with whatever I figure out. In the meantime, let's just assume it's something stupid I am doing which is causing the problem.

Chaster
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Erwin Coumans »

Chaster wrote:Thanks for taking the time to reply Erwin. Really appreciate it.

I am currently compiling 2.63 to compare with what is going on in my simulation. I am glad that it should work - that narrows it down to something I am doing wrong (not surprising since I am new to Bullet). I will report back with whatever I figure out. In the meantime, let's just assume it's something stupid I am doing which is causing the problem.

Chaster
What are the collision shapes, resting on the ground? If they have more then one contact point, it should work fine. Sphere and capsules only have one contact point, so there might be no rotational friction imposed on them. It is possible to improve the friction model (both performance and this rotational friction) if necessary.

Thanks,
Erwin
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

The collision shape for the dynamic body is a cube. Although cones exhibit the same behavior. I am looking at the CCDPhysics demo right now with USE_KINEMATIC_GROUND 1 defined, and I see that the stack of cylinders is moving horizontally with the ground, but I don't see a rotational element to the movement. I assume there is no rotational element? Assuming not, I will add a rotational velocity and see what happens..

Chaster
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Update:

I modified the CCDApp demo to rotate instead of move linearly, and it works as expected (although not perfect, it's pretty good). The problem in my app is either something in my code or perhaps the inaccuracy is prominent because of the scale of the objects maybe...

Thanks again Erwin,

Chaster
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Update:

After a LOT of testing/tweaking/exploring I think I found the problem. My stepSimulation call is being called with timeDelta = 1/frame rate, maxSubSteps = 1. The kinematic object had it's rotation being updated according to frameRate (which varies anywhere from 80fps to 200fps). When I updated the kinematic object rotation using a fixed value (say, 0.001 radians), the dynamic rigid body (a cube) seemed to follow along pretty well. Once I tied it to the frame rate, the rotational friction went bye-bye..

So, it looks like I need to figure out how to properly update my kinematic objects to match with the simulation stepping...

Chaster
ola
Posts: 169
Joined: Sun Jan 14, 2007 7:56 pm
Location: Norway
Contact:

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by ola »

So, it looks like I need to figure out how to properly update my kinematic objects to match with the simulation stepping...
Hi, I've done this in the game I'm working on, by creating my own derived dynamics world class, something like this:

Code: Select all

class MyBulletDynamicsWorld : public btDiscreteDynamicsWorld
{
protected:

   virtual void   internalSingleStepSimulation( btScalar timeStep);

public:

   MyBulletDynamicsWorld(  btDispatcher* dispatcher,
                           btBroadphaseInterface* pairCache,
                           btConstraintSolver* constraintSolver);

   virtual ~MyBulletDynamicsWorld();
};
and then you can re-implement internalSingleStepSimulation like this:

Code: Select all

MyBulletDynamicsWorld::MyBulletDynamicsWorld(btDispatcher* dispatcher,
                                             btBroadphaseInterface* pairCache,
                                             btConstraintSolver* constraintSolver)
:btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver)
{
}

MyBulletDynamicsWorld::~MyBulletDynamicsWorld()
{
}

void MyBulletDynamicsWorld::internalSingleStepSimulation( btScalar timeStep)
{
 btDiscreteDynamicsWorld::internalSingleStepSimulation(timeStep);

 // then do your per-step code here, the timestep is the proper 1/60 one

}
Works very well for me :-)

Cheers,
Ola
Chaster
Posts: 19
Joined: Mon Oct 01, 2007 7:24 pm

Re: Kinematic bodies with friction applied to dynamic bodies?

Post by Chaster »

Hi Ola,

Thanks! That's a very smart (obvious) thing to do. Of course, last night I found out that my REAL problems were:

1) OgreBullet (interface library I am using which sits between Ogre3D and Bullet) was using SimpleDynamicsWorld for simulation (Doh!). Fixed.
2) I was passing in milliseconds instead of seconds for timestep (doh!!). Fixed.

Now it works properly. However, your suggestion to derive my own dynamics world class is a good one. I'll see about doing that too.

Many thanks!

Chaster
Post Reply