Ejemplo n.º 1
0
        public void Load(PhysicsSimulator physicsSimulator, GraphicsDevice device)
        {
            Random rand = new Random();

            physicsSimulator.Add(_topBody);
            physicsSimulator.Add(_rightLegBody);
            physicsSimulator.Add(_leftLegBody);
            physicsSimulator.Add(_topGeom);
            physicsSimulator.Add(_rightLegGeom);
            physicsSimulator.Add(_leftLegGeom);

            _leftWeldJoint = new WeldJoint(_leftLegBody, _topBody, _leftLegBody.Position - new Vector2(-5, _height / 2));
            _leftWeldJoint.Breakpoint = (float)rand.NextDouble() * 3f + 1f;
            _leftWeldJoint.Broke += _leftWeldJoint_Broke;
            physicsSimulator.Add(_leftWeldJoint);

            _rightWeldJoint = new WeldJoint(_rightLegBody, _topBody, _rightLegBody.Position - new Vector2(5, _height / 2));
            _rightWeldJoint.Breakpoint = (float)rand.NextDouble() * 3f + 1f;
            _rightWeldJoint.Broke += _rightWeldJoint_Broke;
            physicsSimulator.Add(_rightWeldJoint);

            _topBrush = new PolygonBrush(_topGeom.LocalVertices, Color.BurlyWood, Color.Black, 1.0f, 0.5f);
            _leftBrush = new PolygonBrush(_leftLegGeom.LocalVertices, Color.BurlyWood, Color.Black, 1.0f, 0.5f);
            _rightBrush = new PolygonBrush(_rightLegGeom.LocalVertices, Color.BurlyWood, Color.Black, 1.0f, 0.5f);

            _topBrush.Load(device);
            _leftBrush.Load(device);
            _rightBrush.Load(device);
        }
Ejemplo n.º 2
0
        private void CreateBorder(Rectangle rectangle, PhysicsSimulator physicsSimulator)
        {
            var borderWidth = rectangle.Width;
            var borderHeight = rectangle.Height;
            var borderCenter = CalculateCenter(rectangle);

            var borderBody = BodyFactory.Instance.CreateRectangleBody(borderWidth, borderHeight, 1.0f);
            borderBody.IsStatic = true;
            borderBody.Position = borderCenter;
            physicsSimulator.Add(borderBody);

            var borderGeometry = GeomFactory.Instance.CreateRectangleGeom(borderBody, borderWidth, borderHeight);
            // prevent collisions with cannon balls
            borderGeometry.CollisionCategories = ((CollisionCategoryManager)game.Services.GetService(typeof(CollisionCategoryManager))).getCollisionCategory(this.GetType());
            borderGeometry.CollidesWith = ((CollisionCategoryManager)game.Services.GetService(typeof(CollisionCategoryManager))).getCollidesWith(this.GetType());
            physicsSimulator.Add(borderGeometry);
        }
Ejemplo n.º 3
0
        public SpriteManager(Game game, Fighter lutador1, Fighter lutador2)
            : base(game)
        {
            this.lutador1 = lutador1;
            this.lutador2 = lutador2;
            simulador = new PhysicsSimulator(new Vector2(0, 10));
            //simulador.Add(lutador1.Body);
            //simulador.Add(lutador1.Geometry);

            groundBody = BodyFactory.Instance.CreateRectangleBody( 800, 10, 1);
            groundGeom = GeomFactory.Instance.CreateRectangleGeom(groundBody, 800, 10);
            groundBody.Position = new Vector2(0, 550);
            groundBody.IsStatic = true;
            groundGeom.RestitutionCoefficient = .2f;
            groundGeom.FrictionCoefficient = .2f;

            barra = ContentManagerFacade.Load<Texture2D>("barra");
            simulador.Add(groundBody);
            simulador.Add(groundGeom);
        }
Ejemplo n.º 4
0
 public static GameObject CreateRectangleGameObject(PhysicsSimulator simulator, string resourceName, float width, float height, float mass)
 {
     var go = new GameObject();
     go.ResourceName = resourceName;
     go.Body = BodyFactory.Instance.CreateRectangleBody(width,height, mass);
     go.Geom = GeomFactory.Instance.CreateRectangleGeom(simulator,go.Body, width, height);
     simulator.Add(go.Body);
     simulator.Add(go.Geom);
     return go;
 }
Ejemplo n.º 5
0
 public static GameObject CreateCircleGameObject(PhysicsSimulator simulator, string resourceName, float rad, float mass)
 {
     var go  = new GameObject();
     go.Body = BodyFactory.Instance.CreateCircleBody(rad, mass);
     go.Geom = GeomFactory.Instance.CreateCircleGeom(simulator, go.Body, rad, 20);
     go.ResourceName = resourceName;
     simulator.Add(go.Body);
     simulator.Add(go.Geom);
     return go;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Adds to physics simulator.
 /// </summary>
 /// <param name="physicsSimulator">The physics simulator.</param>
 public void AddToPhysicsSimulator(PhysicsSimulator physicsSimulator)
 {
     foreach (Body body in _bodies)
         physicsSimulator.Add(body);
     foreach (Geom geom in _geoms)
         physicsSimulator.Add(geom);
     foreach (Joint joint in _joints)
         physicsSimulator.Add(joint);
     foreach (Spring spring in _springs)
         physicsSimulator.Add(spring);
 }
Ejemplo n.º 7
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()
        {
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            graphics.SynchronizeWithVerticalRetrace = false;
            graphics.ApplyChanges();

            assets = new Dictionary<string, Texture2D>();
            gameObjects = new List<GameObject>();

            phySim = new PhysicsSimulator(new Vector2(0, -200));

            var ballObject = GameObjectFactory.CreateCircleGameObject(phySim, "ball", 22f, 1);
            ballObject.Center = new Vector2(250, 400);
            ballObject.Geom.FrictionCoefficient = 0;

            FixedAngleJoint asdf = new FixedAngleJoint(ballObject.Body, 0);
            phySim.Add(asdf);

            player = ballObject;
            gameObjects.Add(ballObject);

            var platformObject = GameObjectFactory.CreateRectangleGameObject(phySim, "platform", 256, 64, 6);
            platformObject.IsStatic = true;
            platformObject.Center = new Vector2(250, 300);
            platformObject.Rotation = MathHelper.ToRadians(-30f);

            var platformObject2 = GameObjectFactory.CreateRectangleGameObject(phySim, "platform", 256, 64, 6);
            platformObject2.Center = new Vector2(5, 150);
            platformObject2.Rotation = MathHelper.ToRadians(90f);
            platformObject2.Geom.FrictionCoefficient = 10;

            var floorObject = GameObjectFactory.CreateRectangleGameObject(phySim, "floor", 1024, 12, 1);
            floorObject.Center = new Vector2(500, 0);
            floorObject.IsStatic = true;
            floorObject.Geom.FrictionCoefficient = 1;

            camera = new Camera2d(new Vector2(0, 0), new Vector2(800, 600), new Vector2(100, 100));

            gameObjects.Add(floorObject);
            gameObjects.Add(platformObject2);
            gameObjects.Add(platformObject);

            base.Initialize();
        }
Ejemplo n.º 8
0
        public void Initialize(PhysicsSimulator physicsSimulator)
        {
            //The wave controller controls how the waves move.. how big, how fast, etc..
            //The wave controller is represented as set of points equally
            //spaced horizontally along the width of the wave.
            WaveController = new WaveController();
            WaveController.Position = ConvertUnits.ToSimUnits(0, 300);
            WaveController.Width = ConvertUnits.ToSimUnits(700);
            WaveController.Height = ConvertUnits.ToSimUnits(200);
            WaveController.NodeCount = 20; //how many vertices make up the surface of the wave
            WaveController.DampingCoefficient = .95f; //determines how quickly the wave will disipate
            WaveController.Frequency = .16f; //determines how fast the wave algorithm runs (seconds)

            //The wave generator parameters simply move an end-point of the wave up and down.
            //Think of a string attached to a wall on one end and held by a person on the other.
            //If the person moves the string up and down to make "waves" then the arm is acting
            //similar to the wave generator. The WaveGeneratorStep property controls how fast the "arm"
            //moves.
            WaveController.WaveGeneratorMax = WaveGeneratorMax;
            WaveController.WaveGeneratorMin = WaveGeneratorMin;
            WaveController.WaveGeneratorStep = WaveGeneratorStep;

            WaveController.Initialize();

            //fluid drag controller controls how things move once IN the water.
            FluidDragController = new FluidDragController();
            FluidDragController.Initialize(WaveController, 5f, 4f, 2f, physicsSimulator.Gravity);
            //init with default values.
            physicsSimulator.Add(FluidDragController);
        }
Ejemplo n.º 9
0
 public virtual bool Load(string assetName, ContentManager content, PhysicsSimulator simulator)
 {
     currentTexture = content.Load<Texture2D>(assetName);
     origin = new Vector2(currentTexture.Width / 2, currentTexture.Height / 2);
     body = BodyFactory.Instance.CreateRectangleBody(currentTexture.Height, currentTexture.Width, 1);
     geom = GeomFactory.Instance.CreateRectangleGeom(body, currentTexture.Width, currentTexture.Height);
     body.Position = position;
     body.Tag = this;
     geom.Tag = this;
     simulator.Add(body);
     simulator.Add(geom);
     return true;
 }
Ejemplo n.º 10
0
        public void Load(GraphicsDevice graphicsDevice, PhysicsSimulator physicsSimulator)
        {
            _rectangleTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, RectangleWidth, RectangleHeight,
                                                                     Color.White, Color.Black);
            int radius;
            if (AttachPoint == 0 | AttachPoint == 2)
            {
                radius = RectangleHeight;
            }
            else
            {
                radius = RectangleWidth;
            }
            _circleTexture = DrawingHelper.CreateCircleTexture(graphicsDevice, radius, Color.White, Color.Black);

            //body is created as rectangle so that it has the moment of inertia closer to the final shape of the object.
            Body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, RectangleWidth,
                                                                             RectangleHeight, 1f);

            _rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, Body,
                                                                      RectangleWidth, RectangleHeight);
            _rectangleGeom.FrictionCoefficient = .5f;
            _rectangleGeom.CollisionGroup = CollisionGroup;

            Vector2 offset = Vector2.Zero;
            switch (AttachPoint)
            {
                case 0:
                    {
                        offset = new Vector2(-RectangleWidth/2f, 0); //offset to rectangle to left
                        break;
                    }
                case 1:
                    {
                        offset = new Vector2(0, -RectangleHeight/2f); //offset to rectangle to top
                        break;
                    }
                case 2:
                    {
                        offset = new Vector2(RectangleWidth/2f, 0); //offset to rectangle to right
                        break;
                    }
                case 3:
                    {
                        offset = new Vector2(0, RectangleHeight/2f); //offset to rectangle to bottom
                        break;
                    }
            }

            Body.Position = Position - offset;

            _circleGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, Body, radius, 20,
                                                                offset, 0);
            _circleGeom.FrictionCoefficient = .5f;
            _circleGeom.CollisionGroup = CollisionGroup;

            _revoluteJoint = JointFactory.Instance.CreateFixedRevoluteJoint(physicsSimulator, Body,
                                                                            Position);
            physicsSimulator.Add(_revoluteJoint);
            SpringFactory.Instance.CreateFixedAngleSpring(physicsSimulator, Body,
                                                          SpringConstant, DampingConstant);
        }
Ejemplo n.º 11
0
 public void add(PhysicsSimulator physics)
 {
         physics.Add(body);
         physics.Add(geom);
 }
Ejemplo n.º 12
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()
        {
            phsX = new PhysicsSimulator(); //Create physX
            //phsX.Iterations = 5;

            //phsX.Gravity = new Vector2(0, (float)9.8);

            // TODO: Add your initialization logic here
            Random r = new Random();

            for (int x = 48; x < 230; x += 64)
            {
                for (int y = 48; y < 200; y += 48)
                {
                    Body rectangleBody;
                    rectangleBody = BodyFactory.Instance.CreateRectangleBody(phsX, 32, 32, 1);
                    rectangleBody.Position = new Vector2(x, y);
                    phsX.Add(GeomFactory.Instance.CreateRectangleGeom(phsX, rectangleBody, 32, 32));
                }
            }

            //Body leftCorner;
            leftCorner = BodyFactory.Instance.CreateRectangleBody(phsX, 10, 480, 100000);
            leftCorner.Position = new Vector2(5, 240);

            //Body rightCorner;
            rightCorner = BodyFactory.Instance.CreateRectangleBody(phsX, 10, 480, 100000);
            rightCorner.Position = new Vector2(267, 240);

            //Body topCorner;
            topCorner = BodyFactory.Instance.CreateRectangleBody(phsX, 272, 10, 100000);
            topCorner.Position = new Vector2(136, 5);

            //Body bottomCorner;
            bottomCorner = BodyFactory.Instance.CreateRectangleBody(phsX, 272, 10, 100000);
            bottomCorner.Position = new Vector2(136, 475);

            Geom gl = GeomFactory.Instance.CreateRectangleGeom(leftCorner, 10, 480);
            Geom gr = GeomFactory.Instance.CreateRectangleGeom(rightCorner, 10, 480);
            Geom gu = GeomFactory.Instance.CreateRectangleGeom(topCorner, 272, 10);
            Geom gb = GeomFactory.Instance.CreateRectangleGeom(bottomCorner, 272, 10);

            gl.Body.IsStatic = true;
            gr.Body.IsStatic = true;
            gu.Body.IsStatic = true;
            gb.Body.IsStatic = true;

            phsX.Add(gl);
            phsX.Add(gr);
            phsX.Add(gu);
            phsX.Add(gb);

            base.Initialize();
        }
Ejemplo n.º 13
0
        public override void Initialize()
        {
            PhysicsSimulator = new PhysicsSimulator(new Vector2(0, 150));

            // This distance has to be big enough to make sure all objects are reactivated
            // before they could collide with an currently active object
            // our biggest object is 25*25 -> 120 would be big enough
            InactivityController controller = new InactivityController(PhysicsSimulator);
            controller.ActivationDistance = 120;

            // Deactivate the object after 2 seconds of idle time
            controller.MaxIdleTime = 2000;
            controller.Enabled = true;

            PhysicsSimulator.Add(controller);
            PhysicsSimulatorView = new PhysicsSimulatorView(PhysicsSimulator);

            base.Initialize();
        }