Switching from dynamic to kinematic.

Discuss any questions about BEPUphysics or problems encountered.
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Switching from dynamic to kinematic.

Post by imtrobin »

Hi

My tank entity is using a kinematic, and when it blows up, I switch to dynamic for a nice rolling effect on the terrain. After which, I set it back to kinematic for reuse, but I find there is still some data which causes it to fly in the air. I have cleared the linear velocity, angular velocity, and clearAllForces. What else is there?

When set dynamic
Entity.eventManager.addEventHook (BepuPhysicsComponent.EventEntityUpdatedCallback);
Entity.becomeDynamic (1, Global.World.CollisionGroups [(int) World.CollisionGroupID.Dynamic]);
Entity.isAffectedByGravity = true;
Entity.applyImpulse (Vector3.Zero, new Vector3 (0.2f, 0.2f, 0.1f));
Entity.activate ();

when set to kinematic
Entity.eventManager.removeEventHook (BepuPhysicsComponent.EventEntityUpdatedCallback);
// restore back kinematic group
Entity.becomeKinematic (Global.World.CollisionGroups [(int) World.CollisionGroupID.GroundUnit]);
Entity.clearForces ();
Entity.isAffectedByGravity = false;
Entity.linearVelocity = Vector3.Zero;
Entity.angularVelocity = Vector3.Zero;
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

Under the debugger, it seems to be the previousLinearVelocity and previousAngularVelocity but I have no access to set that.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

If you mean the previousLinearMomentum/previousAngularMomentum fields, they refer to the last frame's values for the purposes of calculating the totalForce and totalTorque properties and are not used in simulation.

The linearVelocity/angularVelocity properties are buffered, which means they will only be written to the entity after the beginning of the next space update. Could this account for what you are observing? The "internal" properties access the entity data directly.

What exactly is happening with the entity after becoming kinematic again?
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

I have set clearAllForces, the entity flies up up and away in the air unless I set the linervelocity and angularvelocity back to Vector3.Zero.

Then afterward, if I use setLinearVelocity, it will go smoothly, but if I set its worldTransform, it will move forward but wobble about. It is the same piece of code I use to move it forward before turning it to dynamic

I uploaded a video here. two tanks are wobbling, those are the reused one after turning into dynamic.

http://envisagereality.com/downloads/te ... ematic.avi

AttachedEntityBase is my visual, Entity is the physics. This is my synchronization code for kinematic.

Code: Select all

       {
            Matrix worldTransform = AttachedEntityBase.WorldTransform;

            // ignore translation first for offset
            worldTransform.Translation = Vector3.Zero;

            if (Vector3.Zero != PositionOffset)
            {
                // take account of orientation for offset
                Vector3 offset = Vector3.Transform (PositionOffset, worldTransform);

                worldTransform.Translation -= offset;
            }
            // add back translation that was ignored
            worldTransform.Translation += AttachedEntityBase.WorldTransform.Translation;

            Entity.worldTransform = worldTransform;
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

To clarify, Entity.clearForces just gets rid of any Force instances that were previously added using Entity.addForce(Force force). The "Force" framework needs a bit of a redesign as it doesn't fit in the architecture very cleanly.

When an entity switches from dynamic to kinematic or vice versa, the previous velocities are maintained. So if an object was tumbling or exploding before the switch to kinematic, it will continue on some tangent unless the velocities are reset. So, having to reset the velocities when you reuse the object is expected.

If the velocities of the tanks are kept 'correct' relative to their motion, there shouldn't be any wobbling.

If the velocities are correct and you're still seeing wobbling, try setting the entity's allowInterpolation to false (if you're using a internal timestepping).
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

Ok I found why it wobbles. I add a entitupdate event when turning to dynamic and removing them upon kinematic. Turns out I was adding multiple of events due to multiple collisions so even in kinematic mode, the event is still updated. Therefore, I request we have a way to iterate to see the active events so for easier debugging.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

I'll see what I can do.
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

Thanks. I know why this happens and this was becausing I was using the missile was a trigger in 0.9.0. However, with 0.10.0 there is no more trigger and it changed to collisionGroup. The collisionGroup could result in multiple hits where as the trigger will only collide once so I think that's what causing it. Perhaps it might be a good idea to put back triggers.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

If you mean the isDetector property, it is equivalent to setting an entity's collisionRules' personal setting to noResponse. Personal settings take precedence over collision group settings.

By the way, in v0.10.1, I added in a broadphase callback (calculateCollisionRuleCallback) that is used to calculate the collision rule. By default it's just the EntityCollisionRules.getCollisionRule method, but you can change it to anything that you want (and call EntityCollisionRules.getCollisionRule from the replacement if you want).
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

To clarify, v0.9.0's isDetector property could cause multiple contacts to be generated (and multiple collisions between different entities to occur).
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

I see. Then perhaps what I seek is a single report trigger, which I'm used to in PhysX.

Actually, how does the broadphasecalculation meant to be modified? I'm still not getting why I should change that.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

The calculateCollisionRuleCallback is just another method to customize the detection system. It is responsible for determining the CollisionRule to use in between two entities (e.g. noPair, noContacts, noResponse, normal). It allows you to change how CollisionRules are calculated to whatever you want. You don't have to change it, but it is an option. For example, if you wanted to add in a special secondary 'trigger group' tag to the computation, you could add in your own collision rule calculation logic to the callback. Returning noPair from the method will prevent that collision pair from being created.

As a clarification to my last post, the 'initial collision' event handler will only fire on the first contact's creation, not on every contact generated. The 'contact created' event will fire for each contact.

Does a 'single report trigger' only dispatch one event per type per entity per frame?
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

Norbo wrote:As a clarification to my last post, the 'initial collision' event handler will only fire on the first contact's creation, not on every contact generated. The 'contact created' event will fire for each contact.

Does a 'single report trigger' only dispatch one event per type per entity per frame?
Then that would be sufficient. I was confused by it having multiple hits, I'm still not sure why I would get multiple hits, maybe it's because the dynamic was rolling upwards, then it could be going in and out of trigger while doing so. I will investigate again when I have the time.
imtrobin
Posts: 101
Joined: Thu Jun 11, 2009 3:03 am

Re: Switching from dynamic to kinematic.

Post by imtrobin »

Seems I ran into this problem again. I found the reason. When I first collide, I change it to dynamic, and change it's collisiongroup to the Dynamic CollisionGroup (I create this group). This result in another collision event being fired so I'm getting multiple hits from there.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Switching from dynamic to kinematic.

Post by Norbo »

That would do it. However, the becomeDynamic/becomeKinematic methods will still cause another event even if you don't specify a different collision group. Internally, the entities are removed and re-added to the space to complete some initialization.
Post Reply