Creating and updating physics objects

From MaratisWiki
Jump to: navigation, search

Unless you've set up the physics properties of an entity in the Maratis Editor, physics won't be enabled for that entity. If you want to activate physics for the entity via code, you need two steps:

1) create the physics properties;

2) create the real physics object.

The physics context is the what manages the game physics. MScene is using it to prepare all the necessary objects at the start of the scene, but you'll want to use it too if you want to change something in real time.

Get/create and edit the physics properties

You can get physics properties like this:

MPhysicsProperties *phyProps = entity->getPhysicsProperties();

They may as well not exist, so unless you're sure, after this line you should do a check:

if(!phyProps) // or if(phyProps == NULL)
  phyProps = entity->createPhysicsProperties();

If they do not exist, you create them.

Now you have to edit them.

phyProps->setCollisionShape(M_COLLISION_SHAPE_BOX);
phyProps->setMass(1.0f);
phyProps->setFriction(1.00f);
phyProps->setRestitution(0.01f);
phyProps->setLinearDamping(0.01f);
phyProps->setAngularDamping(0.01f);
phyProps->setAngularFactor(0.0f);
phyProps->setLinearFactor(MVector3(1,1,1));

This is straightforward. For the meaning of every property see physics properties.

Create the real physics object

Our physics properties currently have the collisionObjectId equal to zero, which is none, NULL. The collisionObjectId is the ID of the actual object (which we don't "see" and don't manage) which will be really used for the collisions. Such object doesn't currently even exist.

So, after having set up physics properties, we'll call the prepareCollisionShape and prepareCollisionObject functions of the MScene class to allow the creation of the collision object. If you had set constraints you would also have to call prepareConstraints.

scene->prepareCollisionShape(entity);
scene->prepareCollisionObject(entity);
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox