Beispiel #1
0
        public static Body CreateCircle(float radius, Vector2 position, Units units, BodyType bodyType, Entity entity)
        {
            if (units == Units.Pixels)
            {
                radius = PhysicsConvert.ToMeters(radius);
            }

            Body body = BodyFactory.CreateCircle(world, radius, 1);

            body.BodyType = bodyType;
            body.UserData = entity;

            return(body);
        }
Beispiel #2
0
        public static Body CreateRectangle(float width, float height, Vector2 position, Units units, BodyType bodyType, Entity entity)
        {
            if (units == Units.Pixels)
            {
                width    = PhysicsConvert.ToMeters(width);
                height   = PhysicsConvert.ToMeters(height);
                position = PhysicsConvert.ToMeters(position);
            }

            Body body = BodyFactory.CreateRectangle(world, width, height, 1, position);

            body.BodyType = bodyType;
            body.UserData = entity;

            return(body);
        }
Beispiel #3
0
        public static Fixture AttachEdge(Body body, Edge line, Units units, object userData = null)
        {
            Vector2 start = line.Start;
            Vector2 end   = line.End;

            if (units == Units.Pixels)
            {
                start = PhysicsConvert.ToMeters(start);
                end   = PhysicsConvert.ToMeters(end);
            }

            Fixture fixture = FixtureFactory.AttachEdge(start, end, body);

            fixture.UserData = userData;

            return(fixture);
        }
        public static RayCastResults RayCast(Vector2 start, Vector2 end, Units units)
        {
            if (units == Units.Pixels)
            {
                start = PhysicsConvert.ToMeters(start);
                end   = PhysicsConvert.ToMeters(end);
            }

            Vector2 hitPosition = Vector2.Zero;
            Fixture hitFixture  = null;

            float closestFraction = float.PositiveInfinity;

            world.RayCast((fixture, point, normal, fraction) =>
            {
                if (fraction < closestFraction)
                {
                    hitPosition     = point;
                    hitFixture      = fixture;
                    closestFraction = fraction;

                    return(fraction);
                }

                return(1);
            }, start, end);

            if (hitFixture == null)
            {
                return(null);
            }

            hitPosition = PhysicsConvert.ToPixels(hitPosition);

            return(new RayCastResults(hitPosition, (Entity)hitFixture.Body.UserData));
        }