public PlayerPhysicsCharacter(Entity owner, World world, Vector2 position, float width, float height, float density, OnCollision onCollision, OnSeparation onSeparation) : base(owner, world, position, width, height, density, onCollision, onSeparation) { // Load resources ConfigFile configFile = PhysicsSystem.GetPhysicsConfigFile(); runSpeed = configFile.SettingGroups[physicsSettings].Settings["runSpeed"].GetValueAsFloat(); runAirControlSpeed = configFile.SettingGroups[physicsSettings].Settings["airControlSpeed"].GetValueAsFloat(); jumpImpulse = configFile.SettingGroups[physicsSettings].Settings["jumpImpulse"].GetValueAsFloat(); wallJumpImpulse = configFile.SettingGroups[physicsSettings].Settings["wallJumpImpulse"].GetValueAsFloat(); airControlAbility = configFile.SettingGroups[physicsSettings].Settings["airControlAbility"].GetValueAsFloat(); terminalVelocity = configFile.SettingGroups[physicsSettings].Settings["terminalVelocity"].GetValueAsFloat(); slideTerminalVelocity = configFile.SettingGroups[physicsSettings].Settings["slideTerminalVelocity"].GetValueAsFloat(); dashMultiplier = configFile.SettingGroups[physicsSettings].Settings["dashMultiplier"].GetValueAsFloat(); wallSlideEnabled = false; onGroundTimer = new Timer(); onLeftWallTimer = new Timer(); onRightWallTimer = new Timer(); enableOnGroundDelay = true; enableOnLeftWallDelay = true; enableOnRightWallDelay = true; moveSpeed = runSpeed; airControlSpeed = runAirControlSpeed; }
protected override void SetUpPhysics(Entity owner, World world, Vector2 position, float width, float height, float density) { // Load resources ConfigFile configFile = PhysicsSystem.GetPhysicsConfigFile(); brakeSpeed = configFile.SettingGroups[physicsSettings].Settings["brakeSpeed"].GetValueAsFloat(); defaultBodyFriction = configFile.SettingGroups[physicsSettings].Settings["defaultBodyFriction"].GetValueAsFloat(); defaultBodyRestitution = configFile.SettingGroups[physicsSettings].Settings["defaultBodyRestitution"].GetValueAsFloat(); defaultWheelFriction = configFile.SettingGroups[physicsSettings].Settings["defaultWheelFriction"].GetValueAsFloat(); defaultWheelRestitution = configFile.SettingGroups[physicsSettings].Settings["defaultWheelRestitution"].GetValueAsFloat(); //Create a fixture with a body almost the size of the entire object //but with the bottom part cut off. float upperBodyHeight = height - (width / 2); body = BodyFactory.CreateRectangle(world, (float)ConvertUnits.ToSimUnits(width), (float)ConvertUnits.ToSimUnits(upperBodyHeight), density / 2); body.BodyType = BodyType.Dynamic; // tested a rounded rectangle.. didn't work so well. Should I revisit? *** //body = BodyFactory.CreateRoundedRectangle(world, ConvertUnits.ToSimUnits(width), ConvertUnits.ToSimUnits(upperBodyHeight), ConvertUnits.ToSimUnits(0.5f), ConvertUnits.ToSimUnits(0.5f), 2, density / 2, ConvertUnits.ToSimUnits(position)); //body.BodyType = BodyType.Dynamic; // player needs low restitution so he won't bounce on the ground // and low friction to avoid the "edge catching" problem body.Restitution = defaultBodyRestitution; body.Friction = defaultBodyFriction; fixture = body.FixtureList[0]; //also shift it up a tiny bit to keep the new object's center correct body.Position = ConvertUnits.ToSimUnits(position - (Vector2.UnitY * (width / 4))); centerOffset = position.Y - (float)ConvertUnits.ToDisplayUnits(body.Position.Y); //remember the offset from the center for drawing //Now let's make sure our upperbody is always facing up. fixedAngleJoint = JointFactory.CreateFixedAngleJoint(world, body); // Create our bottom part of the player which interacts with the terrain wheelBody = BodyFactory.CreateCircle(world, (float)ConvertUnits.ToSimUnits(width / 2), density / 2); //And position its center at the bottom of the upper body wheelBody.Position = body.Position + ConvertUnits.ToSimUnits(Vector2.UnitY * (upperBodyHeight / 2)); wheelBody.BodyType = BodyType.Dynamic; wheelBody.Restitution = defaultWheelRestitution; //Set the friction higher for fast stopping/starting //or set it lower to make the character slip. wheelBody.Friction = defaultWheelFriction; //These two bodies together are width wide and height high :) //So lets connect them together motor = JointFactory.CreateRevoluteJoint(world, body, wheelBody, Vector2.Zero); motor.MotorEnabled = true; motor.MaxMotorTorque = defaultMaxMotorTorque; motor.MotorSpeed = 0; //Make sure the two fixtures don't collide with each other wheelBody.IgnoreCollisionWith(body); body.IgnoreCollisionWith(wheelBody); // make sure the bodies point to the owners wheelBody.UserData = owner; body.UserData = owner; // so that we don't slip off the edge brakeJoint = JointFactory.CreateFixedAngleJoint(world, wheelBody); // Set contact handlers body.OnCollision += new OnCollisionEventHandler(onCollisionWithBody); body.OnSeparation += new OnSeparationEventHandler(onSeparationWithBody); wheelBody.OnCollision += new OnCollisionEventHandler(onCollisionWithBottom); wheelBody.OnSeparation += new OnSeparationEventHandler(onSeparationWithBottom); }