Beispiel #1
0
        public void update()
        {
            // remove outdated collisions and get current collisionPoints
            collisionPoints.Clear();
            List <Box2DX.Collision.Shape> cachedShapesToBeDeleted = new List <Box2DX.Collision.Shape>();

            foreach (Box2DX.Collision.Shape shape in collisionShapes)
            {
                Box2DX.Common.Vec2[] contactPoints = new Box2DX.Common.Vec2[2];
                float distance = Collision.Distance(out contactPoints[0], out contactPoints[1], this.body.GetShapeList(), this.body.GetXForm(), shape, shape.GetBody().GetXForm());

                if (distance > 0.1F)
                {
                    cachedShapesToBeDeleted.Add(shape);
                }
                collisionPoints.Add(contactPoints[0]);
            }
            foreach (Box2DX.Collision.Shape shape in cachedShapesToBeDeleted)
            {
                collisionShapes.Remove(shape);
            }

            // movement
            float deltaTime   = (float)Program.gameTime.EllapsedTime.TotalSeconds;
            float speedFactor = 20F * deltaTime;

            ControllerInput input = controller.update(this);

            Vector2 inputMovement = Vector2.Zero;

            inputMovement.X += input.xMovement;

            if (inputMovement != Vector2.Zero)
            {
                movement += inputMovement * speedFactor;
            }

            if (input.startJumping && collisionShapes.Count != 0)
            {
                List <Vector2> lowerCollisionPoints = collisionPoints.FindAll((p) => (p.Y + (size.Y / 10F) >= midPoint.Y));
                if (lowerCollisionPoints.Count > 0)
                {
                    Vector2 avgCollisionPoint = Vector2.average(lowerCollisionPoints.ToArray());
                    if (avgCollisionPoint.lengthSqr < float.Epsilon)
                    {
                        avgCollisionPoint = midPoint + Vector2.Down;
                    }

                    Vector2 normal = (midPoint - avgCollisionPoint).normalized;

                    Vector2 jumpMovement = (Vector2.Up * 2 + normal).normalized * 6F;

                    movement = new Vector2(movement.X + jumpMovement.X, jumpMovement.Y);
                }
            }

            float friction = 2F;

            movement = new Vector2(movement.X * (1F - deltaTime * friction), movement.Y);    // friction
        }
Beispiel #2
0
        public void draw(RenderWindow win, View view)
        {
            // Draw Player
            sprite.Position       = midPoint.PixelCoord;
            sprite.Texture.Smooth = true;
            if ((movement.X > 0.1F && sprite.Scale.X > 0F) || (movement.X < -0.1F && sprite.Scale.X < 0F))
            {
                sprite.Scale = new Vector2(sprite.Scale.X * -1F, sprite.Scale.Y);
            }

            win.Draw(sprite.updateFrame(Program.gameTime));

            //
            collisionRectangle.Size      = new Vector2(3, 3);
            collisionRectangle.Origin    = new Vector2(1, 1);
            collisionRectangle.FillColor = SFML.Graphics.Color.Red;

            foreach (Box2DX.Collision.Shape shape in collisionShapes)
            {
                Box2DX.Common.Vec2[] contactPoints = new Box2DX.Common.Vec2[2];
                float distance = Collision.Distance(out contactPoints[0], out contactPoints[1], this.body.GetShapeList(), this.body.GetXForm(), shape, shape.GetBody().GetXForm());

                collisionRectangle.Position = ((Vector2)contactPoints[0]).PixelCoord;
                win.Draw(collisionRectangle);
                collisionRectangle.Position = ((Vector2)contactPoints[1]).PixelCoord;
                win.Draw(collisionRectangle);
            }
        }
Beispiel #3
0
 public SimData(PLAYER p, SIM_OBJ o, OBJ_ACTION a, Box2DX.Common.Vec2 pos, float angle, int id)
 {
     _player  = p;
     _obj     = o;
     _action  = a;
     _pos     = pos;
     _angle   = angle;
     _inst_id = id;
 }
Beispiel #4
0
        public PhysicsWorld(Box2DX.Common.Vec2 gravity, Box2DX.Common.Vec2 worldLowerBound, Box2DX.Common.Vec2 worldUpperBound)
        {
            AABB bound = new AABB();

            bound.LowerBound.Set(worldLowerBound.X, worldLowerBound.Y);
            bound.UpperBound.Set(worldUpperBound.X, worldUpperBound.Y);
            world = new World(bound, gravity, true);
            Engine.Physics.ContactListener listener = new Engine.Physics.ContactListener();
            world.SetContactListener(listener);
        }
Beispiel #5
0
 public PhysicsWorld(Box2DX.Common.Vec2 gravity, AABB worldBoundaries)
 {
     world = new World(worldBoundaries, gravity, true);
     Engine.Physics.ContactListener listener = new Engine.Physics.ContactListener();
     world.SetContactListener(listener);
 }
Beispiel #6
0
 public static Microsoft.Xna.Framework.Vector2 ToVector(this Box2DX.Common.Vec2 vector)
 {
     return(new Microsoft.Xna.Framework.Vector2(vector.X, vector.Y));
 }
Beispiel #7
0
 public static Vector2 FromVec2(Box2DX.Common.Vec2 vector)
 {
     return(new Vector2(vector.X, vector.Y));
 }
Beispiel #8
0
 public static Vector3 FromVec2To3(Box2DX.Common.Vec2 vector)
 {
     return(new Vector3(vector.X, vector.Y, 0));
 }