protected override void PopulateWorld()
{
// ==== Define the ground body ====
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.Position.Set(_trackLengthHalf, -1f);
// Call the body factory which creates the ground box shape.
// The body is also added to the world.
Body groundBody = _world.CreateBody(groundBodyDef);
// Define the ground box shape.
PolygonDef groundShapeDef = new PolygonDef();
// The extents are the half-widths of the box.
groundShapeDef.SetAsBox(_trackLengthHalf + 1f, 1f);
groundShapeDef.Friction = _simParams._defaultFriction;
groundShapeDef.Restitution = _simParams._defaultRestitution;
groundShapeDef.Filter.CategoryBits = 0x3;
// Add the ground shape to the ground body.
groundBody.CreateShape(groundShapeDef);
// Add some small mounds/bumps to the ground.
for (float x = -1f; x < 40f; x += 0.4f + ((_rng.NextFloat()-0.5f) * 0.15f)) {
WalkerWorldUtils.CreateMound(_world, x, 0f, _simParams._defaultFriction, _simParams._defaultRestitution);
}
// ==== Define walker torso.
float walkerX = 0f;
float walkerY = 1.30f;// + ((float)_rng.NextDouble() * 0.1f);
BodyDef torsoBodyDef = new BodyDef();
torsoBodyDef.Position.Set(walkerX, walkerY);
torsoBodyDef.IsBullet = true;
// Create walker torso.
_torsoBody = _world.CreateBody(torsoBodyDef);
PolygonDef torsoShapeDef = new PolygonDef();
torsoShapeDef.SetAsBox(0.10f, 0.30f);
torsoShapeDef.Friction = _simParams._defaultFriction;
torsoShapeDef.Restitution = 0f;
torsoShapeDef.Density = 2f;
torsoShapeDef.Filter.CategoryBits = 0x2;
_torsoBody.CreateShape(torsoShapeDef);
_torsoBody.SetMassFromShapes();
// ===== Create legs.
// Leg joint definition.
RevoluteJointDef jointDef = new RevoluteJointDef();
jointDef.CollideConnected = false;
jointDef.EnableMotor = true;
jointDef.MaxMotorTorque = 0f;
// Other re-usable stuff .
const float legRadius = 0.05f; // Half the thickness of the leg
Vec2 upperLegPosBase = new Vec2(walkerX, walkerY - 0.25f);
Vec2 lowerLegPosBase = new Vec2(walkerX, walkerY - 0.75f);
// ===== Create left leg.
// Upper leg.
Body upperLeftLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);
// Join to torso (hip joint)
jointDef.Initialize(_torsoBody, upperLeftLegBody, upperLegPosBase);
_leftHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
// Lower leg.
_leftLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
// Join to upper leg (knee joint)
jointDef.Initialize(upperLeftLegBody, _leftLowerLegBody, lowerLegPosBase);
_leftKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
// ===== Create right leg.
// Upper leg.
Body upperRightLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);
// Join to torso (hip joint)
jointDef.Initialize(_torsoBody, upperRightLegBody, upperLegPosBase);
_rightHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
// Lower leg.
_rightLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
// Join to upper leg (knee joint)
jointDef.Initialize(upperRightLegBody, _rightLowerLegBody, lowerLegPosBase);
_rightKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
}