예제 #1
0
파일: Polygon.cs 프로젝트: fusspawn/Nez
        public override bool collidesWithShape(Shape other, out CollisionResult result)
        {
            if (other is Polygon)
            {
                return(ShapeCollisions.polygonToPolygon(this, other as Polygon, out result));
            }

            if (other is Circle && ShapeCollisions.circleToPolygon(other as Circle, this, out result))
            {
                result.invertResult();
                return(true);
            }

            throw new NotImplementedException(string.Format("overlaps of Polygon to {0} are not supported", other));
        }
예제 #2
0
파일: Polygon.cs 프로젝트: RastaCow/Nez
        public override bool collidesWithShape( Shape other, out CollisionResult result )
        {
            if( other is Polygon )
                return ShapeCollisions.polygonToPolygon( this, other as Polygon, out result );

            if( other is Circle )
            {
                if( ShapeCollisions.circleToPolygon( other as Circle, this, out result ) )
                {
                    result.invertResult();
                    return true;
                }
                return false;
            }

            throw new NotImplementedException( string.Format( "overlaps of Polygon to {0} are not supported", other ) );
        }
예제 #3
0
        /// <summary>
        /// handles collisions between two Fixtures. Note that the first Fixture must have a Circle/PolygonShape and one of the Fixtures must be
        /// static.
        /// </summary>
        /// <returns><c>true</c>, if shapes was collided, <c>false</c> otherwise.</returns>
        /// <param name="fixtureA">Fixture a.</param>
        /// <param name="fixtureB">Fixture b.</param>
        /// <param name="result">Result.</param>
        public static bool collideShapes(Fixture fixtureA, Fixture fixtureB, out CollisionResult result)
        {
            result = new CollisionResult();

            // we need at least one static fixture
            if (!fixtureA.body.isStatic && !fixtureB.body.isStatic)
            {
                // if the body is dyanmic and asleep wake it up
                if (fixtureB.body.isDynamic && !fixtureB.body.isAwake)
                {
                    fixtureB.body.isAwake = true;
                }
                return(false);
            }

            // check normal collision filtering
            if (!ContactManager.shouldCollide(fixtureA, fixtureB))
            {
                return(false);
            }

            // check user filtering
            if (fixtureA.body.world.contactManager.onContactFilter != null && !fixtureA.body.world.contactManager.onContactFilter(fixtureA, fixtureB))
            {
                return(false);
            }

            // gather our transforms
            FSTransform transformA;

            fixtureA.body.getTransform(out transformA);

            FSTransform transformB;

            fixtureB.body.getTransform(out transformB);

            // we only handle Circle or Polygon collisions
            if (fixtureA.shape is CircleShape)
            {
                if (fixtureB.shape is CircleShape)
                {
                    return(collideCircles(fixtureA.shape as CircleShape, ref transformA, fixtureB.shape as CircleShape, ref transformB, out result));
                }

                if (fixtureB.shape is PolygonShape)
                {
                    return(collidePolygonCircle(fixtureB.shape as PolygonShape, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result));
                }

                if (fixtureB.shape is EdgeShape)
                {
                    return(collideEdgeAndCircle(fixtureB.shape as EdgeShape, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result));
                }

                if (fixtureB.shape is ChainShape)
                {
                    var chain = fixtureB.shape as ChainShape;
                    for (var i = 0; i < chain.childCount; i++)
                    {
                        var edge = chain.getChildEdge(i);
                        if (collideEdgeAndCircle(edge, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result))
                        {
                            return(true);
                        }
                    }
                }
            }

            if (fixtureA.shape is PolygonShape)
            {
                if (fixtureB.shape is CircleShape)
                {
                    var res = collidePolygonCircle(fixtureA.shape as PolygonShape, ref transformA, fixtureB.shape as CircleShape, ref transformB, out result);
                    result.invertResult();
                    return(res);
                }

                if (fixtureB.shape is PolygonShape)
                {
                    return(collidePolygons(fixtureA.shape as PolygonShape, ref transformA, fixtureB.shape as PolygonShape, ref transformB, out result));
                }

                if (fixtureB.shape is EdgeShape)
                {
                    return(collideEdgeAndPolygon(fixtureB.shape as EdgeShape, ref transformB, fixtureA.shape as PolygonShape, ref transformA, out result));
                }

                if (fixtureB.shape is ChainShape)
                {
                    var chain = fixtureB.shape as ChainShape;
                    for (var i = 0; i < chain.childCount; i++)
                    {
                        var edge = chain.getChildEdge(i);
                        if (collideEdgeAndPolygon(edge, ref transformB, fixtureA.shape as PolygonShape, ref transformA, out result))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
예제 #4
0
		/// <summary>
		/// handles collisions between two Fixtures. Note that the first Fixture must have a Circle/PolygonShape and one of the Fixtures must be
		/// static.
		/// </summary>
		/// <returns><c>true</c>, if shapes was collided, <c>false</c> otherwise.</returns>
		/// <param name="fixtureA">Fixture a.</param>
		/// <param name="fixtureB">Fixture b.</param>
		/// <param name="result">Result.</param>
		public static bool collideShapes( Fixture fixtureA, Fixture fixtureB, out CollisionResult result )
		{
			result = new CollisionResult();

			// we need at least one static fixture
			if( !fixtureA.body.isStatic && !fixtureB.body.isStatic )
			{
				// if the body is dyanmic and asleep wake it up
				if( fixtureB.body.isDynamic && !fixtureB.body.isAwake )
					fixtureB.body.isAwake = true;
				return false;
			}

			// check normal collision filtering
			if( !ContactManager.shouldCollide( fixtureA, fixtureB ) )
				return false;

			// check user filtering
			if( fixtureA.body.world.contactManager.onContactFilter != null && !fixtureA.body.world.contactManager.onContactFilter( fixtureA, fixtureB ) )
				return false;

			// gather our transforms
			FSTransform transformA;
			fixtureA.body.getTransform( out transformA );

			FSTransform transformB;
			fixtureB.body.getTransform( out transformB );

			// we only handle Circle or Polygon collisions
			if( fixtureA.shape is CircleShape )
			{
				if( fixtureB.shape is CircleShape )
					return collideCircles( fixtureA.shape as CircleShape, ref transformA, fixtureB.shape as CircleShape, ref transformB, out result );

				if( fixtureB.shape is PolygonShape )
					return collidePolygonCircle( fixtureB.shape as PolygonShape, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result );

				if( fixtureB.shape is EdgeShape )
					return collideEdgeAndCircle( fixtureB.shape as EdgeShape, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result );

				if( fixtureB.shape is ChainShape )
				{
					var chain = fixtureB.shape as ChainShape;
					for( var i = 0; i < chain.childCount; i++ )
					{
						var edge = chain.getChildEdge( i );
						if( collideEdgeAndCircle( edge, ref transformB, fixtureA.shape as CircleShape, ref transformA, out result ) )
							return true;
					}
				}
			}

			if( fixtureA.shape is PolygonShape )
			{
				if( fixtureB.shape is CircleShape )
				{
					var res = collidePolygonCircle( fixtureA.shape as PolygonShape, ref transformA, fixtureB.shape as CircleShape, ref transformB, out result );
					result.invertResult();
					return res;
				}

				if( fixtureB.shape is PolygonShape )
					return collidePolygons( fixtureA.shape as PolygonShape, ref transformA, fixtureB.shape as PolygonShape, ref transformB, out result );

				if( fixtureB.shape is EdgeShape )
					return collideEdgeAndPolygon( fixtureB.shape as EdgeShape, ref transformB, fixtureA.shape as PolygonShape, ref transformA, out result );

				if( fixtureB.shape is ChainShape )
				{
					var chain = fixtureB.shape as ChainShape;
					for( var i = 0; i < chain.childCount; i++ )
					{
						var edge = chain.getChildEdge( i );
						if( collideEdgeAndPolygon( edge, ref transformB, fixtureA.shape as PolygonShape, ref transformA, out result ) )
							return true;
					}
				}
			}

			return false;
		}