コード例 #1
0
        private void CreatePlayerPhysicsObjects(Vector2 gameWorldPosition)
        {
            MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.5f, 0.5f, 1);
            MainFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player);
            Bodies.Add(MainFixture.Body);
            MainFixture.Body.Position        = Engine.Physics.PositionToPhysicsWorld(gameWorldPosition);
            MainFixture.Body.BodyType        = BodyType.Dynamic;
            MainFixture.Body.SleepingAllowed = false;

            WheelFixture = FixtureFactory.CreateCircle(Engine.Physics.World, 0.3f, 1.0f);
            WheelFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player);
            Bodies.Add(WheelFixture.Body);
            WheelFixture.Body.Position = MainFixture.Body.Position + new Vector2(0.0f, 0.6f);
            WheelFixture.Body.BodyType = BodyType.Dynamic;

            WheelFixture.Body.SleepingAllowed = false;
            WheelFixture.Friction             = 0.5f;

            playerFAJ       = JointFactory.CreateFixedAngleJoint(Engine.Physics.World, MainFixture.Body);
            playerFAJ.BodyB = WheelFixture.Body;

            wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(MainFixture.Body, WheelFixture.Body, Vector2.Zero);
            wheelMotorRevJoint.MaxMotorTorque = 10.0f;
            wheelMotorRevJoint.MotorEnabled   = true;
            Engine.Physics.World.AddJoint(wheelMotorRevJoint);
        }
コード例 #2
0
        public FixedAngleJoint CreateFixedAngleJoint(PhysicsSimulator physicsSimulator, Body body)
        {
            FixedAngleJoint fixedAngleJoint = CreateFixedAngleJoint(body);

            physicsSimulator.Add(fixedAngleJoint);
            return(fixedAngleJoint);
        }
コード例 #3
0
        /// <summary>
        /// Sets up all the physical properties of the StickMan object.
        /// </summary>
        /// <param name="physicsWorld">The physics world to set the objects up in.</param>
        private void SetUpPhysicsObjects(ref World physicsWorld)
        {
            float density     = 1.1f;
            float restitution = 0.125f;

            // Upper body for standing
            this.fullBody                     = BodyFactory.CreateRectangle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 2.0f), ConvertUnits.ToSimUnits(this.standingSprite.Height * 0.75f), density, ConvertUnits.ToSimUnits(this.fullBodyOffset));
            this.fullBody.BodyType            = BodyType.Dynamic;
            this.fullBody.Restitution         = restitution;
            this.fullBody.CollisionCategories = EntityConstants.StickManCategory;

            // Middle body for crouching
            this.smallBody          = BodyFactory.CreateRectangle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 2.0f), ConvertUnits.ToSimUnits(this.standingSprite.Height * 0.25f), density, ConvertUnits.ToSimUnits(this.smallBodyOffset));
            this.smallBody.BodyType = BodyType.Dynamic;
            this.smallBody.IgnoreCollisionWith(this.fullBody);
            this.smallBody.Restitution         = restitution;
            this.smallBody.CollisionCategories = EntityConstants.StickManCategory;

            // Wheel for movement
            this.wheelBody          = BodyFactory.CreateCircle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 4.0f), density, ConvertUnits.ToSimUnits(this.wheelBodyOffset));
            this.wheelBody.BodyType = BodyType.Dynamic;
            this.wheelBody.IgnoreCollisionWith(this.smallBody);
            this.wheelBody.IgnoreCollisionWith(this.fullBody);
            this.wheelBody.Restitution         = restitution;
            this.wheelBody.Friction            = float.MaxValue;
            this.wheelBody.CollisionCategories = EntityConstants.StickManCategory;

            // Joints to connect the bodies.
            this.bodyJoint                 = JointFactory.CreateWeldJoint(physicsWorld, this.fullBody, this.smallBody, this.smallBody.Position);
            this.angleUprightJoint         = JointFactory.CreateFixedAngleJoint(physicsWorld, this.smallBody);
            this.motorJoint                = JointFactory.CreateRevoluteJoint(physicsWorld, this.smallBody, this.wheelBody, Vector2.Zero);
            this.motorJoint.MotorSpeed     = 0.0f;
            this.motorJoint.MaxMotorTorque = 1000.0f;
            this.motorJoint.MotorEnabled   = true;
        }
コード例 #4
0
        private AngleJointTest()
        {
            BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            Body fA = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(-5, 4));

            fA.BodyType = BodyType.Dynamic;

            Body fB = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(5, 4));

            fB.BodyType = BodyType.Dynamic;

            AngleJoint joint = new AngleJoint(fA, fB);

            joint.TargetAngle = (float)Math.PI / 2;
            World.AddJoint(joint);

            Body fC = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(10, 4));

            fC.BodyType = BodyType.Dynamic;

            FixedAngleJoint fixedJoint = new FixedAngleJoint(fC);

            fixedJoint.TargetAngle = (float)Math.PI / 3;
            World.AddJoint(fixedJoint);
        }
コード例 #5
0
        /// <summary>
        /// Creates a fixed angle joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="body">The body.</param>
        /// <returns></returns>
        public static FixedAngleJoint CreateFixedAngleJoint(World world, Body body)
        {
            FixedAngleJoint angleJoint = new FixedAngleJoint(body);

            world.AddJoint(angleJoint);

            return(angleJoint);
        }
コード例 #6
0
        public override PhysicsJoint CreateFixedAngleJoint(PhysicsBody body, float targetAngle)
        {
            FixedAngleJoint farseerJoint = JointFactory.CreateFixedAngleJoint(world,
                                                                              ((FarseerBody)body).Body);

            farseerJoint.TargetAngle = targetAngle;
            return(new FarseerJoint(farseerJoint, body, body));
        }
コード例 #7
0
        internal override void UpdateJoint()
        {
            base.UpdateJoint();
            if (this.joint == null)
            {
                return;
            }

            FixedAngleJoint j = this.joint as FixedAngleJoint;

            j.TargetAngle = this.angle;
            j.BiasFactor  = this.biasFactor;
            j.Softness    = this.softness;
            j.MaxImpulse  = this.maxImpulse < 0.0f ? float.MaxValue : PhysicsConvert.ToPhysicalUnit(this.maxImpulse);
        }
コード例 #8
0
        public EnemyPhysicsComponent(Engine engine, Vector2 gameWorldPosition, Global.Shapes shape)
            : base(engine)
        {
            switch (shape)
            {
            case Global.Shapes.Circle:
                MainFixture          = FixtureFactory.CreateCircle(Engine.Physics.World, 0.25f, 1.0f);
                MainFixture.Friction = 0.5f;
                break;

            case Global.Shapes.Player:
                MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.5f, 0.5f, 1);
                Bodies.Add(MainFixture.Body);
                MainFixture.Body.Position        = Engine.Physics.PositionToPhysicsWorld(gameWorldPosition);
                MainFixture.Body.BodyType        = BodyType.Dynamic;
                MainFixture.Body.SleepingAllowed = false;

                Fixture WheelFixture = FixtureFactory.CreateCircle(Engine.Physics.World, 0.3f, 1.0f);
                Bodies.Add(WheelFixture.Body);
                WheelFixture.Body.Position = MainFixture.Body.Position + new Vector2(0.0f, 0.6f);
                WheelFixture.Body.BodyType = BodyType.Dynamic;

                WheelFixture.Body.SleepingAllowed = false;
                WheelFixture.Friction             = 0.5f;

                FixedAngleJoint playerFAJ = JointFactory.CreateFixedAngleJoint(Engine.Physics.World, MainFixture.Body);
                playerFAJ.BodyB = WheelFixture.Body;

                RevoluteJoint wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(MainFixture.Body, WheelFixture.Body, Vector2.Zero);
                wheelMotorRevJoint.MaxMotorTorque = 10.0f;
                wheelMotorRevJoint.MotorEnabled   = true;
                Engine.Physics.World.AddJoint(wheelMotorRevJoint);
                break;

            case Global.Shapes.Square:
                MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.5f, 0.5f, 1.0f);
                break;

            default:
                throw new Exception("shape " + shape.ToString() + " not recognized when creating EnemyPhysicsComponent");
            }

            Bodies.Add(MainFixture.Body);
            MainFixture.Body.Position = Engine.Physics.PositionToPhysicsWorld(gameWorldPosition);
            MainFixture.Body.BodyType = BodyType.Dynamic;
        }
コード例 #9
0
        protected override void SetUpPhysics(World world, Vector2 position, float width, float height, float mass)
        {
            //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, ConvertUnits.ToSimUnits(width), ConvertUnits.ToSimUnits(upperBodyHeight), mass / 2);
            body.BodyType    = BodyType.Dynamic;
            body.Restitution = 0.3f;
            body.Friction    = 0.5f;
            //also shift it up a tiny bit to keey 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 a wheel as wide as the whole object
            wheel = BodyFactory.CreateCircle(world, ConvertUnits.ToSimUnits(width / 2), mass / 2);
            //And position its center at the bottom of the upper body
            wheel.Position    = body.Position + ConvertUnits.ToSimUnits(Vector2.UnitY * (upperBodyHeight / 2));
            wheel.BodyType    = BodyType.Dynamic;
            wheel.Restitution = 0.3f;
            wheel.Friction    = 0.5f;

            //These two bodies together are width wide and height high :)
            //So lets connect them together
            motor = JointFactory.CreateRevoluteJoint(world, body, wheel, Vector2.Zero);
            motor.MotorEnabled   = true;
            motor.MaxMotorTorque = 1000f; //set this higher for some more juice
            motor.MotorSpeed     = 0;

            //Make sure the two fixtures don't collide with each other
            wheel.IgnoreCollisionWith(body);
            body.IgnoreCollisionWith(wheel);

            //Set the friction of the wheel to float.MaxValue for fast stopping/starting
            //or set it higher to make the character slip.
            wheel.Friction = float.MaxValue;
        }
コード例 #10
0
        public FixedAngleJoint CreateFixedAngleJoint(Body body)
        {
            FixedAngleJoint fixedAngleJoint = new FixedAngleJoint(body);

            return(fixedAngleJoint);
        }
コード例 #11
0
        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);
        }