Creating and updating physics objects

From MaratisWiki
Revision as of 21:05, 20 October 2013 by 255 (Talk | contribs)
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 step:

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

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