Beispiel #1
0
 public static FSFrictionJoint CreateFrictionJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                                 FVector2 anchorB)
 {
     FSFrictionJoint frictionJoint = new FSFrictionJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(frictionJoint);
     return frictionJoint;
 }
Beispiel #2
0
 public void Add(FSBody body)
 {
     Debug.Assert(BodyCount < BodyCapacity);
     body.IslandIndex = BodyCount;
     Bodies[BodyCount] = body;
     ++BodyCount;
 }
Beispiel #3
0
        /// <summary>
        /// Creates an angle joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="bodyA">The first body.</param>
        /// <param name="bodyB">The second body.</param>
        /// <returns></returns>
        public static FSAngleJoint CreateAngleJoint(FSWorld world, FSBody bodyA, FSBody bodyB)
        {
            FSAngleJoint angleJoint = new FSAngleJoint(bodyA, bodyB);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
Beispiel #4
0
 public static FSDistanceJoint CreateDistanceJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                                 FVector2 anchorB)
 {
     FSDistanceJoint distanceJoint = new FSDistanceJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
Beispiel #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 FSFixedAngleJoint CreateFixedAngleJoint(FSWorld world, FSBody body)
        {
            FSFixedAngleJoint angleJoint = new FSFixedAngleJoint(body);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
Beispiel #6
0
        public FSBreakableBody(IEnumerable<Vertices> vertices, FSWorld world, float density, object userData)
        {
            _world = world;
            _world.ContactManager.PostSolve += PostSolve;
            MainBody = new FSBody(_world);
            MainBody.BodyType = BodyType.Dynamic;

            foreach (Vertices part in vertices)
            {
                PolygonShape polygonShape = new PolygonShape(part, density);
                FSFixture fixture = MainBody.CreateFixture(polygonShape, userData);
                Parts.Add(fixture);
            }
        }
Beispiel #7
0
        public virtual bool IsActiveOn(FSBody body)
        {
            if (body == null || !body.Enabled || body.IsStatic)
                return false;

            if (body.FixtureList == null)
                return false;

            foreach (FSFixture fixture in body.FixtureList)
            {
                //Disable
                if ((fixture.CollisionGroup == DisabledOnGroup) &&
                    fixture.CollisionGroup != 0 && DisabledOnGroup != 0)
                    return false;

                if ((fixture.CollisionCategories & DisabledOnCategories) != Category.None)
                    return false;

                if (EnabledOnGroup != 0 || EnabledOnCategories != Category.All)
                {
                    //Enable
                    if ((fixture.CollisionGroup == EnabledOnGroup) &&
                        fixture.CollisionGroup != 0 && EnabledOnGroup != 0)
                        return true;

                    if ((fixture.CollisionCategories & EnabledOnCategories) != Category.None &&
                        EnabledOnCategories != Category.All)
                        return true;
                }
                else
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #8
0
 /// <summary>
 /// Creates a Wheel Joint
 /// </summary>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="anchor"></param>
 /// <param name="axis"></param>
 /// <returns></returns>
 public static FSWheelJoint CreateWheelJoint(FSBody bodyA, FSBody bodyB, FVector2 anchor, FVector2 axis)
 {
     FSWheelJoint joint = new FSWheelJoint(bodyA, bodyB, anchor, axis);
     return joint;
 }
Beispiel #9
0
        public FSBody Clone()
        {
            FSBody body = new FSBody();
            body.World = World;
            body.UserData = UserData;
            body.LinearDamping = LinearDamping;
            body.LinearVelocityInternal = LinearVelocityInternal;
            body.AngularDamping = AngularDamping;
            body.AngularVelocityInternal = AngularVelocityInternal;
            body.Position = Position;
            body.Rotation = Rotation;
            body._bodyType = _bodyType;
            body.Flags = Flags;

            World.AddBody(body);

            return body;
        }
Beispiel #10
0
		public FSFixture Clone(FSBody body) {
			FSFixture fixture = new FSFixture();
			fixture.Body = body;

			if (FSSettings.ConserveMemory)
				fixture.Shape = Shape;
			else
				fixture.Shape = Shape.Clone();

			fixture.UserData = UserData;
			fixture.UserBits = UserBits;
			fixture.Restitution = Restitution;
			fixture.Friction = Friction;
			fixture.IsSensor = IsSensor;
			fixture._collisionGroup = CollisionGroup;
			fixture._collisionCategories = CollisionCategories;
			fixture._collidesWith = CollidesWith;

			if (_collisionIgnores != null) {
				fixture._collisionIgnores = new Dictionary<int, bool>();

				foreach (KeyValuePair<int, bool> pair in _collisionIgnores)
					fixture._collisionIgnores.Add(pair.Key, pair.Value);
			}

			fixture.RegisterFixture();
			return fixture;
		}
Beispiel #11
0
        /// <summary>
        /// This is used to prevent connected bodies from colliding.
        /// It may lie, depending on the collideConnected flag.
        /// </summary>
        /// <param name="other">The other body.</param>
        /// <returns></returns>
        internal bool ShouldCollide(FSBody other)
        {
            // At least one body should be dynamic.
            if (_bodyType != BodyType.Dynamic && other._bodyType != BodyType.Dynamic)
            {
                return false;
            }

            // Does a joint prevent collision?
            for (JointEdge jn = JointList; jn != null; jn = jn.Next)
            {
                if (jn.Other == other)
                {
                    if (jn.Joint.CollideConnected == false)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Beispiel #12
0
 public static FSFixedMouseJoint CreateFixedMouseJoint(FSWorld world, FSBody body, FVector2 target)
 {
     FSFixedMouseJoint joint = new FSFixedMouseJoint(body, target);
     world.AddJoint(joint);
     return joint;
 }
Beispiel #13
0
 /// <summary>
 /// Creates a revolute joint.
 /// </summary>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="localAnchorB">The anchor of bodyB in local coordinates</param>
 /// <returns></returns>
 public static FSRevoluteJoint CreateRevoluteJoint(FSBody bodyA, FSBody bodyB, FVector2 localAnchorB)
 {
     FVector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localAnchorB));
     FSRevoluteJoint joint = new FSRevoluteJoint(bodyA, bodyB, localanchorA, localAnchorB);
     return joint;
 }
Beispiel #14
0
 public static FSPulleyJoint CreatePulleyJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 groundAnchorA,
                                             FVector2 groundAnchorB, FVector2 anchorA, FVector2 anchorB, float ratio)
 {
     FSPulleyJoint pulleyJoint = new FSPulleyJoint(bodyA, bodyB, groundAnchorA, groundAnchorB, anchorA, anchorB,
                                               ratio);
     world.AddJoint(pulleyJoint);
     return pulleyJoint;
 }
Beispiel #15
0
 public void AddBody(FSBody body)
 {
     Bodies.Add(body);
 }
Beispiel #16
0
        void SetIsland(FSBody body)
        {
#if USE_ISLAND_SET
            if (!IslandSet.Contains(body))
            {
                IslandSet.Add(body);
            }
#endif
            body.Flags |= BodyFlags.Island;
        }
Beispiel #17
0
        /// <summary>
        /// Destroy a rigid body.
        /// Warning: This automatically deletes all associated shapes and joints.
        /// </summary>
        /// <param name="body">The body.</param>
        public void RemoveBody(FSBody body)
        {
            Debug.Assert(!_bodyRemoveList.Contains(body),
                         "The body is already marked for removal. You are removing the body more than once.");

            if (!_bodyRemoveList.Contains(body))
                _bodyRemoveList.Add(body);

#if USE_AWAKE_BODY_SET
            if (AwakeBodySet.Contains(body))
            {
                AwakeBodySet.Remove(body);
            }
#endif
        }
Beispiel #18
0
        /// <summary>
        /// Add a rigid body.
        /// </summary>
        /// <returns></returns>
        internal void AddBody(FSBody body)
        {
            Debug.Assert(!_bodyAddList.Contains(body), "You are adding the same body more than once.");

            if (!_bodyAddList.Contains(body))
                _bodyAddList.Add(body);
        }
Beispiel #19
0
 /// <summary>
 /// Creates a prsimatic joint
 /// </summary>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="localanchorB"></param>
 /// <param name="axis"></param>
 /// <returns></returns>
 public static FSPrismaticJoint CreatePrismaticJoint(FSBody bodyA, FSBody bodyB, FVector2 localanchorB, FVector2 axis)
 {
     FVector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localanchorB));
     FSPrismaticJoint joint = new FSPrismaticJoint(bodyA, bodyB, localanchorA, localanchorB, axis);
     return joint;
 }
Beispiel #20
0
		public FSFixture(FSBody body, Shape shape)
			: this(body, shape, null)
		{
		}
Beispiel #21
0
 /// <summary>
 /// Creates a revolute joint and adds it to the world
 /// </summary>
 /// <param name="world"></param>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="anchor"></param>
 /// <returns></returns>
 public static FSRevoluteJoint CreateRevoluteJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchor)
 {
     FSRevoluteJoint joint = CreateRevoluteJoint(bodyA, bodyB, anchor);
     world.AddJoint(joint);
     return joint;
 }
Beispiel #22
0
        public void RestoreCollisionWith(FSBody other)
        {
            for (int i = 0; i < FixtureList.Count; i++)
            {
                FSFixture f = FixtureList[i];
                for (int j = 0; j < other.FixtureList.Count; j++)
                {
                    FSFixture f2 = other.FixtureList[j];

                    f.RestoreCollisionWith(f2);
                }
            }
        }
Beispiel #23
0
 /// <summary>
 /// Creates a prismatic joint and adds it to the world
 /// </summary>
 /// <param name="world"></param>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="localanchorB"></param>
 /// <param name="axis"></param>
 /// <returns></returns>
 public static FSPrismaticJoint CreatePrismaticJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 localanchorB,
                                                   FVector2 axis)
 {
     FSPrismaticJoint joint = CreatePrismaticJoint(bodyA, bodyB, localanchorB, axis);
     world.AddJoint(joint);
     return joint;
 }
Beispiel #24
0
 public static FSSliderJoint CreateSliderJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 anchorA,
                                             FVector2 anchorB, float minLength, float maxLength)
 {
     FSSliderJoint sliderJoint = new FSSliderJoint(bodyA, bodyB, anchorA, anchorB, minLength, maxLength);
     world.AddJoint(sliderJoint);
     return sliderJoint;
 }
Beispiel #25
0
		public FSFixture(FSBody body, Shape shape, object userData) {
			_collisionCategories = FSSettings.DefaultFixtureCollisionCategories;
			_collidesWith = FSSettings.DefaultFixtureCollidesWith;
			_collisionGroup = 0;

#if USE_IGNORE_CCD_CATEGORIES
			IgnoreCCDWith = FSSettings.DefaultFixtureIgnoreCCDWith;
#endif

			//Fixture defaults
			Friction = 0.2f;
			Restitution = 0;

			Body = body;
			IsSensor = false;

			UserData = userData;

			if (FSSettings.ConserveMemory)
				Shape = shape;
			else
				if (shape != null) Shape = shape.Clone();

			RegisterFixture();
		}
Beispiel #26
0
 /// <summary>
 /// Creates a weld joint
 /// </summary>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="worldAnchor">World space coordinates of weld joint</param>
 /// <returns></returns>
 public static FSWeldJoint CreateWeldJoint(FSBody bodyA, FSBody bodyB, FVector2 worldAnchor)
 {
     FSWeldJoint joint = new FSWeldJoint(bodyA, bodyB, bodyA.GetLocalPoint(worldAnchor),
                                     bodyB.GetLocalPoint(worldAnchor));
     return joint;
 }
Beispiel #27
0
        public override void Start()
        {
            base.Start();

            FSBody body;

            tScale = physScale * 2f;

            // St position in world space
            m_offset = new FVector2(180f/physScale, -200f/physScale);
            m_motorSpeed = 2f;
            m_motorOn = true;

            FVector2 pivot = new FVector2(0f, 24f/tScale);

            for(int i = 0; i < 50; i++)
            {
                body = BodyFactory.CreateCircle(FSWorldComponent.PhysicsWorld, 3.75f /  physScale, 1f, new FVector2((Random.value * 620f + 10f)/physScale, -340f/physScale));
                body.BodyType = BodyType.Dynamic;
            }

            // chassis
            {
                m_chassis = BodyFactory.CreateBody(FSWorldComponent.PhysicsWorld, FVector2.Add(pivot, m_offset));
                m_chassis.BodyType = BodyType.Dynamic;
                FSFixture m_chassis_f = FixtureFactory.AttachRectangle(150f / tScale, 60f / tScale, 1f, FVector2.Zero, m_chassis);
                //m_chassis_f.CollisionGroup = -1;
                m_chassis_f.CollisionCategories = Category.Cat10;
                m_chassis_f.CollidesWith = Category.Cat1;
            }
            // wheel
            {
                m_wheel = BodyFactory.CreateBody(FSWorldComponent.PhysicsWorld, FVector2.Add(pivot, m_offset));
                m_wheel.BodyType = BodyType.Dynamic;
                FSFixture m_wheel_f = FixtureFactory.AttachCircle(48f / tScale, 1f, m_wheel);
                //m_wheel_f.CollisionGroup = -1;
                m_wheel_f.CollisionCategories = Category.Cat10;
                m_wheel_f.CollidesWith = Category.Cat1;
            }
            // glue chassis & wheel
            {
                m_motorJoint = JointFactory.CreateRevoluteJoint(FSWorldComponent.PhysicsWorld, m_wheel, m_chassis, FVector2.Zero);
                m_motorJoint.MotorSpeed = m_motorSpeed;
                m_motorJoint.MaxMotorTorque = 400f;
                m_motorJoint.CollideConnected = false;
                m_motorJoint.MotorEnabled = m_motorOn;
            }

            FVector2 wheelAnchor;

            wheelAnchor = new FVector2(0f, -24f/tScale) + pivot;

            CreateLeg(-1f, wheelAnchor);
            CreateLeg(1f, wheelAnchor);

            m_wheel.Rotation = 120f * Mathf.Deg2Rad;
            CreateLeg(-1f, wheelAnchor);
            CreateLeg(1f, wheelAnchor);

            m_wheel.Rotation = -120f * Mathf.Deg2Rad;
            CreateLeg(-1f, wheelAnchor);
            CreateLeg(1f, wheelAnchor);
        }
Beispiel #28
0
 /// <summary>
 /// Creates a weld joint and adds it to the world
 /// </summary>
 /// <param name="world"></param>
 /// <param name="bodyA"></param>
 /// <param name="bodyB"></param>
 /// <param name="worldAnchor">World space coordinates of weld joint</param>
 /// <returns></returns>
 public static FSWeldJoint CreateWeldJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 worldAnchor)
 {
     FSWeldJoint joint = CreateWeldJoint(bodyA, bodyB, worldAnchor);
     world.AddJoint(joint);
     return joint;
 }
Beispiel #29
0
        public override void Start()
        {
            base.Start();

            AABB waterBounds = new AABB(new FVector2(0f, -360f / physScale), new FVector2(640f / physScale, -200f / physScale));
            controller = new BuoyancyController(waterBounds, 2.0f, 5f, 2f, FSWorldComponent.PhysicsWorld.Gravity);

            // add the controller
            FSWorldComponent.PhysicsWorld.AddController(controller);

            bodies = new List<FSBody>();
            FSBody tbody;
            // Spawn in a bunch of crap
            for(int i = 0; i < 5; i++)
            {
                tbody = BodyFactory.CreateRectangle(FSWorldComponent.PhysicsWorld, (Random.value * 5f + 10f) / physScale, (Random.value * 5f + 10f) / physScale, 1f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale));
                tbody.Rotation = Random.value * Mathf.PI;
                tbody.BodyType = BodyType.Dynamic;
                bodies.Add(tbody);
            }
            for(int i = 0; i < 5; i++)
            {
                tbody = BodyFactory.CreateCircle(FSWorldComponent.PhysicsWorld, (Random.value * 5f + 10f) / physScale, 0.5f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale));
                tbody.Rotation = Random.value * Mathf.PI;
                tbody.BodyType = BodyType.Dynamic;
                bodies.Add(tbody);
            }
            for(int i = 0; i < 15; i++)
            {
                Vertices vlist = new Vertices();
                if(Random.value > 0.66f)
                {
                    vlist.Add(new FVector2((10f + Random.value * 10f) / physScale, (-10f - Random.value * 10f) / physScale));
                    vlist.Add(new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale));
                    vlist.Add(new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale));
                    vlist.Add(new FVector2((-10f - Random.value * 10f) / physScale, (-10f - Random.value * 10f) / physScale));
                }
                else if(Random.value > 0.5f)
                {
                    FVector2 v00 = new FVector2(0f, (-10f - Random.value * 10f) / physScale);
                    FVector2 v02 = new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale);
                    FVector2 v03 = new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale);
                    FVector2 v01 = new FVector2(v00.X + v02.X, v00.Y + v02.Y);
                    v01 *= Random.value / 2f + 0.8f;
                    FVector2 v04 = new FVector2(v03.X + v00.X, v03.Y + v00.Y);
                    v04 *= Random.value / 2f + 0.8f;
                    vlist.Add(v04); vlist.Add(v03); vlist.Add(v02); vlist.Add(v01); vlist.Add(v00);
                }
                else
                {
                    vlist.Add(new FVector2((5f + Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale));
                    vlist.Add(new FVector2((-5f - Random.value * 10f) / physScale, (10f + Random.value * 10f) / physScale));
                    vlist.Add(new FVector2(0f, (-10f - Random.value * 10f) / physScale));
                }
                tbody = BodyFactory.CreateCompoundPolygon(FSWorldComponent.PhysicsWorld, new List<Vertices>{vlist}, 1f, new FVector2((Random.value * 400f + 120f) / physScale, (Random.value * -150f - 50f) / physScale));
                tbody.Rotation = Random.value * Mathf.PI;
                tbody.BodyType = BodyType.Dynamic;
                bodies.Add(tbody);
            }

            //Add some exciting bath toys
            tbody = new FSBody(FSWorldComponent.PhysicsWorld);
            tbody.Position = new FVector2(50f / physScale, -300f / physScale);
            tbody.BodyType = BodyType.Dynamic;
            FixtureFactory.AttachRectangle(80f / physScale, 20f / physScale, 3f, FVector2.Zero, tbody);
            bodies.Add(tbody);

            tbody = new FSBody(FSWorldComponent.PhysicsWorld);
            tbody.Position = new FVector2(300f / physScale, -300f / physScale);
            tbody.BodyType = BodyType.Dynamic;
            FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale,
                new FVector2(30f / physScale, 0f), 0f, tbody);
            FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale,
                new FVector2(-30f / physScale, 0f), 0f, tbody);
            FixtureFactory.AttachRectangle(60f / physScale, 4f / physScale, 2f, FVector2.Zero, tbody);
            FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale,
                new FVector2(0f, 30f / physScale), 0f, tbody);
            FixtureFactory.AttachSolidArc(2f, Mathf.PI * 2f, 8, 7f / physScale,
                new FVector2(0f, -30f / physScale), 0f, tbody);
            FixtureFactory.AttachRectangle(4f / physScale, 60f / physScale, 2f, FVector2.Zero, tbody);
        }
Beispiel #30
0
 public static FSWeldJoint CreateWeldJoint(FSWorld world, FSBody bodyA, FSBody bodyB, FVector2 localAnchorA,
                                         FVector2 localAnchorB)
 {
     FSWeldJoint weldJoint = new FSWeldJoint(bodyA, bodyB, localAnchorA, localAnchorB);
     world.AddJoint(weldJoint);
     return weldJoint;
 }