protected override void SetupPhysics(World world) { #if !EDITOR Vector2 simPosition = ConvertUnits.ToSimUnits(this._position); if (_useShape) { float simHeight = ConvertUnits.ToSimUnits(this._height); float simWidth = ConvertUnits.ToSimUnits(this._width); this.Body = new Body(world); this.Body.Position = simPosition; this._origin = new Vector2(this._texture.Width, this._texture.Height) * 0.5f; switch (_shapeType) { case ObjectShape.Quadrilateral: { Fixture fixture = FixtureFactory.AttachRectangle(simWidth, simHeight, _mass, Vector2.Zero, Body); break; } case ObjectShape.Circle: { Fixture fixture = FixtureFactory.AttachCircle(simWidth * 0.5f, _mass, this.Body); break; } } this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, Vector2.Zero, simPosition); } else { bool useCentroid = false; if (_rotatesWithLevel) { useCentroid = true; } TexVertOutput input = SpinAssist.TexToVert(world, _texture, _mass, false); this._origin = Vector2.Zero; this.Body = input.Body; this.Body.Position = simPosition; if (useCentroid) { this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, this.Body.LocalCenter, simPosition); } else { this._revoluteJoint = JointFactory.CreateFixedRevoluteJoint(world, Body, this.Body.LocalCenter, simPosition); } this._revoluteJoint.MaxMotorTorque = float.MaxValue; this._revoluteJoint.MotorEnabled = true; if (!_rotatesWithLevel) { this.Body.BodyType = BodyType.Dynamic; } else { this.Body.BodyType = BodyType.Dynamic; this.Body.Rotation = this._rotation; float newSpeed = 1 / _motorSpeed; this._motorSpeed = newSpeed; } if (this._motorEnabled) { this._revoluteJoint.MotorSpeed = _motorSpeed; } else { this._revoluteJoint.MotorSpeed = 0.0f; } } this.Body.CollidesWith = Category.All & ~Category.Cat20; this.Body.CollisionCategories = Category.Cat20; this.Body.Friction = 3.0f; this.Body.UserData = _materialType; #endif }
protected override void SetupPhysics(World world) { float textureWidth = ConvertUnits.ToSimUnits(this._texture.Width); float textureHeight = ConvertUnits.ToSimUnits(this._texture.Height); Vector2 axis = SpinAssist.ModifyVectorByOrientation(new Vector2(0, -1), _orientation); Body body; #region Shaft for (int i = 0; i < _shaftPieces; i++) { body = new Body(world); body.Position = ConvertUnits.ToSimUnits(this._position); body.Rotation = SpinAssist.RotationByOrientation(this._orientation); if (i == 0) { Fixture fixture = FixtureFactory.AttachRectangle(textureWidth, textureHeight, 1.0f, Vector2.Zero, body); body.BodyType = BodyType.Static; } else { Fixture fixture = FixtureFactory.AttachRectangle(textureWidth - ((textureWidth * 0.04f) * i), textureHeight, 1.0f, Vector2.Zero, body); body.BodyType = BodyType.Dynamic; FixedPrismaticJoint _joint = JointFactory.CreateFixedPrismaticJoint(world, body, ConvertUnits.ToSimUnits(this._position), axis); _joint.MotorEnabled = true; _joint.MaxMotorForce = float.MaxValue; _joint.LimitEnabled = true; _joint.LowerLimit = 0; _joint.UpperLimit = (textureHeight * i); _joints.Add(_joint); } body.Friction = 3.0f; body.CollisionCategories = Category.Cat2; // Ignore collision with Statics and other pistons. body.CollidesWith = Category.All & ~Category.Cat2 & ~Category.Cat20; body.UserData = _materialType; _shaftBodies.Add(body); } #endregion #region Endpiece TexVertOutput input = SpinAssist.TexToVert(world, _crusherTexture, ConvertUnits.ToSimUnits(10), true); _crusherTextureOrigin = -ConvertUnits.ToSimUnits(input.Origin); this.Body = input.Body; this.Body.Position = ConvertUnits.ToSimUnits(this._position); this.Body.Rotation = this._rotation; this.Body.Friction = 3.0f; this.Body.Restitution = 0.0f; this.Body.BodyType = BodyType.Dynamic; this.Body.Mass = 100.0f; this.Body.CollisionCategories = Category.Cat2; // Ignore collision with Statics and other pistons. this.Body.CollidesWith = Category.All & ~Category.Cat2 & ~Category.Cat20; this.Body.UserData = _materialType; this._prismaticJoint = JointFactory.CreateFixedPrismaticJoint(world, this.Body, ConvertUnits.ToSimUnits(this._position + new Vector2(0, 40)), axis); this._prismaticJoint.UpperLimit = (textureHeight * (_shaftPieces)); this._prismaticJoint.LowerLimit = (textureHeight * 0.5f); this._prismaticJoint.LimitEnabled = true; this._prismaticJoint.MotorEnabled = true; this._prismaticJoint.MaxMotorForce = float.MaxValue; #endregion if (_isLethal) { float endHeight = ConvertUnits.ToSimUnits(_crusherTexture.Height); Fixture fix = FixtureFactory.AttachRectangle( ConvertUnits.ToSimUnits(_crusherTexture.Width - 20), endHeight * 0.38f, 1.0f, new Vector2(0, -endHeight * 0.4f), Body); fix.IsSensor = true; fix.IgnoreCollisionWith(this.Body.FixtureList[0]); fix.Body.OnCollision += TouchedLethal; fix.Body.OnSeparation += LeftLethal; } }