How to move an Entity?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Duckocide
Posts: 18
Joined: Sat May 29, 2010 4:51 pm

How to move an Entity?

Post by Duckocide »

Hi

I've been integrating BEPU physics in to my latest game effortand have a problem. Based on controller movement, I'm trying to move a Physics entity around. I've successfully got a space up and running and things bounce around quite happily from their initial state. I've been trying to use the applyImpulse() methods, but my dynamic object just sits there. I'm probably missing something really simple, but I've tried all sorts of things to get the object moving. In the code below, the CompoundBody only contains 1 entity (a cyclinder). Acceleration and Torque are scalar values from the controller sticks. As you can see, I've tried iterating the entities, but still no joy. Help :D

Code: Select all

#if BEPU
            if (BEPUBody != null)
            {
                CompoundBody cb = (CompoundBody)BEPUBody;
                //cb.isAlwaysActive = true;
                //cb.applyLinearImpulse(Acceleration*BEPUBody.mass*10);
                //cb.applyAngularImpulse(Torque);
                List<BEPUphysics.Entities.Entity> cbe = new List<BEPUphysics.Entities.Entity>();
                cb.getAllRealChildren(cbe);
                foreach (BEPUphysics.Entities.Entity e in cbe)
                {
                    e.applyLinearImpulse(Acceleration*BEPUBody.mass*10);
                    e.applyAngularImpulse(Torque);
                }

            }
#endif
Duckocide
Posts: 18
Joined: Sat May 29, 2010 4:51 pm

Re: How to move an Entity?

Post by Duckocide »

Spot the Noob! :oops:

I can get some motion by changing the linearVelocity and linearMomentum properties. I'd still like to understand what the Impulse methods are used for :?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How to move an Entity?

Post by Norbo »

Your first approach of applying the impulses to the parent entity was the correct one between the two. Children of compound bodies are just shapes rigidly attached to their parents and should not be pushed around.

Generally, when using applyLinear/AngularImpulse, a common problem is that the object fell asleep. These methods are meant to be pretty direct and therefore do not manage entity activity; some other applyImpulse overloads and the velocity properties do manage activity. However, you set it to always active in the first attempt, so this is likely not the issue.

Another option is that your entity is kinematic. Kinematic entities are entities constructed without a mass parameter or that were converted using entity.becomeKinematic. Kinematic entities have infinite inertia and no forces can make them move. An impulse is an instant change in momentum which is ignored by the infinite inertia. It's still possible to set velocities directly on kinematic entities to make them move.

Generally, when trying to define character motion with defined velocity goals, it's best to just stick with the entity's velocity properties. The impulse methods are used for applying general forces as opposed to velocities. For example, you could make a push force volume which just exerts a constant 100 * dt impulse per frame. If you put a 1000 mass box in that field, it would pretty much not move. If you put a 1 mass box there, it would fly away.
Duckocide
Posts: 18
Joined: Sat May 29, 2010 4:51 pm

Re: How to move an Entity?

Post by Duckocide »

Many thanks for the clarification. The intellisense docs (and API reference) on isActive describes using a couple of methods (activate and deactivate) to control entity activity (awake etc). These no longer exist? Is the following code reasonable (i.e. setting isActive true)?

Code: Select all

                CompoundBody cb = (CompoundBody)BEPUBody;
                if (Acceleration != Vector3.Zero || Torque != Vector3.Zero)
                {
                    if (!cb.isActive) cb.isActive = true;
                    cb.applyLinearImpulse(Acceleration);
                    cb.applyAngularImpulse(Torque);
                }
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How to move an Entity?

Post by Norbo »

That code looks good. The activate/deactivate methods were indeed removed since they were redundant with setting the isActive property. I've now removed that XML comment, thanks for the report.
Duckocide
Posts: 18
Joined: Sat May 29, 2010 4:51 pm

Re: How to move an Entity?

Post by Duckocide »

Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How to move an Entity?

Post by Norbo »

Very nice! 8)
Post Reply