public void Update(GameTime gameTime, CameraOperator cameraOperator, World world)
 {
     _sprite.Body.Center = Input.Mouse.Position;
     if (_mouseJoint != null)
     {
         if (Input.Mouse.IsButtonDown(Input.Mouse.MouseButton.Left))
         {
             _mouseJoint.WorldAnchorB = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position);
         }
         else
         {
             world.RemoveJoint(_mouseJoint);
             _mouseJoint = null;
         }
     }
     else
     {
         Vector2 cursorWorldPosition = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position);
         if (Input.Mouse.IsButtonClicked(Input.Mouse.MouseButton.Left))
         {
             Fixture fixture;
             float radiusMeters = _sprite.Body.Projection.BoundingBox.Dimensions.X.ToMeters();
             if ((fixture =
                 world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters, -radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters, -radiusMeters))) != null)
             {
                 if (fixture.UserData is Coin)
                 {
                     _mouseJoint = new FixedMouseJoint(fixture.Body, cursorWorldPosition)
                     {
                         MaxForce = 200*fixture.Body.Mass
                     };
                     world.AddJoint(_mouseJoint);
                 }
             }
         }
     }
 }
Beispiel #2
0
        public bool IsEmpty(Vector2 Position)
        {
            var  fixture     = PhysicsWorld.TestPoint(Position);
            int  terrainPosX = (int)(Position.X / Terrain.Scale);
            int  terrainPosY = (int)(Position.Y / Terrain.Scale);
            bool hasTerrain  = false;

            if (terrainPosX > 0 && terrainPosX < Terrain.width && terrainPosY > 0 && terrainPosY < Terrain.height)
            {
                hasTerrain = Terrain.PublicShape.TerrainBitmap[terrainPosX, terrainPosY] == Terrain.TerrainType.Earth ||
                             Terrain.PublicShape.TerrainBitmap[terrainPosX, terrainPosY] == Terrain.TerrainType.Stone ||
                             Terrain.PublicShape.TerrainBitmap[terrainPosX, terrainPosY] == Terrain.TerrainType.Sand;
            }

            return(fixture == null && !hasTerrain);
        }
Beispiel #3
0
        /// <summary>
        /// This is a high-level function to cuts fixtures inside the given world, using the start and end points.
        /// Note: We don't support cutting when the start or end is inside a shape.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="start">The startpoint.</param>
        /// <param name="end">The endpoint.</param>
        /// <returns>True if the cut was performed.</returns>
        public static bool Cut(World world, Vector2 start, Vector2 end)
        {
            List<Fixture> fixtures = new List<Fixture>();
            List<Vector2> entryPoints = new List<Vector2>();
            List<Vector2> exitPoints = new List<Vector2>();

            //We don't support cutting when the start or end is inside a shape.
            if (world.TestPoint(start) != null || world.TestPoint(end) != null)
                return false;

            //Get the entry points
            world.RayCast((f, p, n, fr) =>
                              {
                                  fixtures.Add(f);
                                  entryPoints.Add(p);
                                  return 1;
                              }, start, end);

            //Reverse the ray to get the exitpoints
            world.RayCast((f, p, n, fr) =>
                              {
                                  exitPoints.Add(p);
                                  return 1;
                              }, end, start);

            //We only have a single point. We need at least 2
            if (entryPoints.Count + exitPoints.Count < 2)
                return false;

            for (int i = 0; i < fixtures.Count; i++)
            {
                // can't cut circles or edges yet !
                if (fixtures[i].Shape.ShapeType != ShapeType.Polygon)
                    continue;

                if (fixtures[i].Body.BodyType != BodyType.Static)
                {
                    //Split the shape up into two shapes
                    Vertices first;
                    Vertices second;
                    SplitShape(fixtures[i], entryPoints[i], exitPoints[i], out first, out second);

                    //Delete the original shape and create two new. Retain the properties of the body.
                    if (first.CheckPolygon() == PolygonError.NoError)
                    {
                        Body firstFixture = BodyFactory.CreatePolygon(world, first, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                        firstFixture.Rotation = fixtures[i].Body.Rotation;
                        firstFixture.LinearVelocity = fixtures[i].Body.LinearVelocity;
                        firstFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        firstFixture.BodyType = BodyType.Dynamic;
                    }

                    if (second.CheckPolygon() == PolygonError.NoError)
                    {
                        Body secondFixture = BodyFactory.CreatePolygon(world, second, fixtures[i].Shape.Density, fixtures[i].Body.Position);
                        secondFixture.Rotation = fixtures[i].Body.Rotation;
                        secondFixture.LinearVelocity = fixtures[i].Body.LinearVelocity;
                        secondFixture.AngularVelocity = fixtures[i].Body.AngularVelocity;
                        secondFixture.BodyType = BodyType.Dynamic;
                    }

                    world.RemoveBody(fixtures[i].Body);
                }
            }

            return true;
        }
Beispiel #4
0
        public void MouseHoverEventProcessing(World world)
        {
            var selectedFixture = world.TestPoint(ConvertUnits.ToSimUnits(MouseVector2)) as Fixture;

            if (selectedFixture != null)
                if (OnMouseHover != null)
                {
                    selectedItemID = selectedFixture.Body.BodyId;
                    OnMouseHover(selectedItemID, mouseState);
                }
                else
                    selectedItemID = -1;
        }
Beispiel #5
0
        /// <summary>
        /// returns false if there are no CollisionCategorie 2 objects between startPosition and endPosition, else return true
        /// less accuracy is more accuracy ;)
        /// </summary>
        /// <param name="aPlayer"></param>
        /// <param name="world"></param>
        /// <returns></returns>
        private bool rayCast(Vector2 startPosition, Vector2 endPosition, int accuracy, World world)
        {
            Vector2 startRay = startPosition;
            Vector2 endRay = endPosition;
            Vector2 direction = new Vector2(endRay.X - startRay.X, endRay.Y - startRay.Y);
            direction.Normalize();

            while (new Vector2((int)startRay.X, (int)startRay.Y) != new Vector2((int)endRay.X, (int)endRay.Y))
            {
                startRay = startRay + direction * accuracy;

                Fixture fixture = world.TestPoint(ConvertUnits.ToSimUnits(startRay));

                if (fixture != null && fixture.CollisionCategories == Category.Cat2)
                {
                    return true;
                }
            }
            return false;
        }