Example #1
0
        public void Update(FrameEventArgs e)
        {
            var dt = e.Time;

            //Space.Update(dt);
            PhysicsWorld.Step((float)dt);
            // If nothing is listening for an event it will be null
            if (OnUpdate != null)
            {
                OnUpdate(this, e);
            }
            foreach (var act in ActorsToAdd)
            {
                ImmediateAddActor(act);
            }
            foreach (var act in ActorsToRemove)
            {
                ImmediateRemoveActor(act);
            }

            ActorsToAdd.Clear();
            ActorsToRemove.Clear();

            //emit.Update(dt, grp);
            //cont.Update(dt, grp);
        }
Example #2
0
        public void ChangeCentroidTest0()
        {
            Scene scene = new Scene();
            FloatPortal portal0 = new FloatPortal(scene);
            FloatPortal portal1 = new FloatPortal(scene);
            portal0.SetTransform(new Transform2(new Vector2(0, 0), 1, (float)Math.PI / 2));
            portal1.SetTransform(new Transform2(new Vector2(10, 0), 2, (float)Math.PI / 2));
            Portal.SetLinked(portal0, portal1);
            PortalCommon.UpdateWorldTransform(scene);

            World world = new World(new Xna.Vector2(0, 0f));
            Body body0 = Factory.CreateBox(world, new Vector2(1, 2));
            Body body1 = Factory.CreateBox(world, new Vector2(1, 2));

            Xna.Vector2 startPos = new Xna.Vector2(0, 1);
            body0.Position = startPos;
            body1.Position = startPos;
            Portal.Enter(portal0, body1);

            PortalJoint portalJoint = Factory.CreatePortalJoint(world, body0, body1, portal0);

            for (int i = 0; i < 10; i++)
            {
                body0.LocalCenter += new Xna.Vector2(0, 0.1f);
                body1.LocalCenter += new Xna.Vector2(0, 0.1f);

                world.Step(1 / (float)60);
                Assert.IsTrue(body0.Position == startPos);
            }
        }
Example #3
0
        /// <summary>
        /// Runs a single simulation step in the physics engine and syncs the state of the physics bodies back to the given world.
        /// </summary>
        public void PhysicsStep(World.World worldState, float elapsedSeconds)
        {
            // Update the physics world
            PhysicsWorld.Step(elapsedSeconds);
            worldState.Water.Step(worldState, elapsedSeconds);

            bool    useCustomGravity = worldState.StaticGeometry.HasGravityPoint;
            Vector2 gravityPoint     = worldState.StaticGeometry.GravityPoint;

            // Sync back the positions and velocities
            foreach (var e in worldState.Entities)
            {
                var body = e.PhysicsBody;

                if (e.PhysicsBody == null || e.Disposed)
                {
                    continue;
                }

                // Manually apply gravity
                if (useCustomGravity)
                {
                    body.ApplyForce(9.81f * Vector2.Normalize(gravityPoint - body.Position) * body.Mass);
                }
                else
                {
                    body.ApplyForce(9.81f * new Vector2(0, -1) * body.Mass);
                }

                e.Position = body.Position;
                e.Velocity = body.LinearVelocity;

                if (e is Ball)
                {
                    float eps = 0.01f;
                    if (e.Velocity.X > eps)
                    {
                        e.Rotation = (float)Math.PI * 0.5f;
                    }
                    else if (e.Velocity.X < -eps)
                    {
                        e.Rotation = -(float)Math.PI * 0.5f;
                    }
                }
            }
        }
Example #4
0
 /// <summary>
 /// Called once per frame.
 /// </summary>
 public void UpdateStep(double worldStep)
 {
     // Update world simulation. (cap min update to 60fps)
     World.Step((float)worldStep);
 }
Example #5
0
        public void PortalJointTest1()
        {
            Scene scene = new Scene();
            FloatPortal portal0 = new FloatPortal(scene);
            FloatPortal portal1 = new FloatPortal(scene);
            portal0.SetTransform(new Transform2(new Vector2(), 1, 0, true));
            portal1.SetTransform(new Transform2(new Vector2(10, 0), 1, 0));
            portal0.Linked = portal1;
            portal1.Linked = portal0;
            PortalCommon.UpdateWorldTransform(scene);

            World world = new World(new Xna.Vector2(0, 0f));
            Body body0 = Factory.CreateBox(world, new Vector2(1, 2));
            Body body1 = Factory.CreateBox(world, new Vector2(1, 2));
            Portal.Enter(portal0, body1);

            PortalJoint portalJoint = Factory.CreatePortalJoint(world, body0, body1, portal0);

            world.Step(1 / (float)60);

            AssertPortalJoint(body0, body1, portal0);
        }
Example #6
0
 void IWorld.Step(float deltaTime) => _world.Step(deltaTime);
Example #7
0
        static void Main()
        {
            window = new RenderWindow(new VideoMode(1280, 720), "", Styles.Close);
            window.SetFramerateLimit(60);
            window.Closed += (sender, eventArgs) => window.Close();

            shipTex = new Texture("Ship.png");
            asteroidTex = new Texture("Asteroid.png");

            var shipSpr = new Sprite(shipTex);
            shipSpr.Origin = new Vector2f(shipTex.Size.X / 2f, shipTex.Size.Y / 2f);

            var asteroidSpr = new Sprite(asteroidTex);
            asteroidSpr.Origin = new Vector2f(asteroidTex.Size.X / 2f, asteroidTex.Size.Y / 2f);

            world = new World(new Vector2(0, 0));

            var debugView = new SFMLDebugView(world);
            debugView.AppendFlags(DebugViewFlags.Shape);

            CreateBounds(20, 11.25f);

            var ship = CreateShip();
            ship.Position = new Vector2(3, 1);
            ship.Rotation = 1.7f;

            var ship2 = CreateShip();
            ship2.Position = new Vector2(3, 1);
            ship2.Rotation = 1.7f;

            var asteroid = CreateAsteroid();
            asteroid.Position = new Vector2(4, 4);

            window.KeyPressed += (sender, eventArgs) =>
            {
                if (eventArgs.Code == Keyboard.Key.Space)
                {
                    CreateBullets(ship);
                }
            };

            while (window.IsOpen())
            {
                window.DispatchEvents();

                if (Keyboard.IsKeyPressed(Keyboard.Key.W))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, -25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.S))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, 25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.A))
                {
                    ship.ApplyTorque(-10);
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.D))
                {
                    ship.ApplyTorque(10);
                }

                world.Step(1f / 60);

                window.Clear(Color.Black);

                shipSpr.Position = new Vector2f(ship.Position.X * 64, ship.Position.Y * 64);
                shipSpr.Rotation = (ship.Rotation * (180 / (float)Math.PI));
                window.Draw(shipSpr);

                asteroidSpr.Position = new Vector2f(asteroid.Position.X * 64, asteroid.Position.Y * 64);
                asteroidSpr.Rotation = (asteroid.Rotation * (180 / (float)Math.PI));
                window.Draw(asteroidSpr);

                var start = ship.Position;
                var step = (float)(2 * Math.PI) / 200;
                byte col = 0;
                var line = new VertexArray(PrimitiveType.Lines, 2);
                line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), Color.White);
                for (var dir = 0f; dir <= 2 * Math.PI; dir += step)
                {
                    float min = 100;
                    byte res = 255;
                    var point = start + LengthDir(dir, 20);
                    world.RayCast((f, p, n, fr) =>
                    {
                        if (fr > min)
                            return 1;

                        min = fr;
                        res = (byte)(fr * 255);
                        point = p;
                        return fr;
                    }, start, point);

                    line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), new Color(col, 0, 0));
                    line[1] = new Vertex(new Vector2f(point.X * 64, point.Y * 64), new Color(col, 0, 0));
                    window.Draw(line);
                    col++;
                }

                debugView.Draw(window);

                window.Display();
            }
        }
Example #8
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            mines = new List<Mine>();
            explosions = new List<Explosion>();
            weaponCharges = new List<IPowerup>();
            bombs = new List<Bomb>();

            world = new World(new Vector2(0.0f, -20f));

            PrimitiveDrawing.SetUp(this.world);
            //FarseerPhysics.Settings.EnableDiagnostics = false;
            FarseerPhysics.Settings.MaxPolygonVertices = 30;

            highScore = 20; //TODO

            world.Clear();
            shielded = false;
            botherBall = null;
            speedTimer = 0;
            timeSinceStart = 0;
            touchedGround = true;
            score = 0;
            ball = BodyFactory.CreateCircle(world, 1.5f, 1f, new Vector2(0.0f, 3f), ballColor);
            ball.BodyType = BodyType.Dynamic;
            ball.Friction = 10;
            floorBody = new Body(world)
            {
                BodyType = BodyType.Static,
                Position = Vector2.Zero,
                FixedRotation = true
            };
            currentFloorLength = 0;
            BodyFactory.CreateRectangle(world, 4f, (float)(1706665.0 / 512.0), 1f, new Vector2(-35.4333f, ball.Position.Y), Color.Black).Friction = 0.0f;
            BodyFactory.CreateRectangle(world, 4f, (float)(1706665.0 / 512.0), 1f, new Vector2(35.3333f, ball.Position.Y), Color.Black).Friction = 0.0f;
            highScoreLine = BodyFactory.CreateRectangle(
                world,
                66.6666f,
                (float)(66.6666030883789 / (double)this.GraphicsDevice.Viewport.AspectRatio / 40.0),
                1f,
                new Vector2(0.0f, (float)(-this.highScore - 66.6666030883789 / (double)this.GraphicsDevice.Viewport.AspectRatio / 80.0)),
                Color.Red
            );
            highScoreLine.IsStatic = true;
            highScoreLine.IsSensor = true;
            ball.OnCollision += Ball_OnCollision;
            world.Step(1000);

            base.Initialize();
        }
Example #9
0
 internal void Update(GameTime gameTime)
 {
     // http://weblogs.asp.net/bsimser/farseer-tutorial-for-the-absolute-beginners
     _farseerWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));
 }