The world class manages all physics entities, dynamic simulation, and asynchronous queries.
        /// <summary>
        /// Attaches the bodies with revolute joints.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="bodies">The bodies.</param>
        /// <param name="localAnchorA">The local anchor A.</param>
        /// <param name="localAnchorB">The local anchor B.</param>
        /// <param name="connectFirstAndLast">if set to <c>true</c> [connect first and last].</param>
        /// <param name="collideConnected">if set to <c>true</c> [collide connected].</param>
        public static List<RevoluteJoint> AttachBodiesWithRevoluteJoint(World world, List<Body> bodies,
            Vector2 localAnchorA,
            Vector2 localAnchorB, bool connectFirstAndLast,
            bool collideConnected)
        {
            List<RevoluteJoint> joints = new List<RevoluteJoint>(bodies.Count + 1);

            for (int i = 1; i < bodies.Count; i++)
            {
                RevoluteJoint joint = new RevoluteJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB);
                joint.CollideConnected = collideConnected;
                world.AddJoint(joint);
                joints.Add(joint);
            }

            if (connectFirstAndLast)
            {
                RevoluteJoint lastjoint = new RevoluteJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA,
                                                            localAnchorB);
                lastjoint.CollideConnected = collideConnected;
                world.AddJoint(lastjoint);
                joints.Add(lastjoint);
            }

            return joints;
        }
예제 #2
0
        /// <summary>
        /// Creates a fixed angle joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="body">The body.</param>
        /// <returns></returns>
        public static FixedAngleJoint CreateFixedAngleJoint(World world, Body body)
        {
            FixedAngleJoint angleJoint = new FixedAngleJoint(body);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
예제 #3
0
 public static FixedDistanceJoint CreateFixedDistanceJoint(World world, Body body, Vector2 localAnchor,
                                                           Vector2 worldAnchor)
 {
     FixedDistanceJoint distanceJoint = new FixedDistanceJoint(body, localAnchor, worldAnchor);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
예제 #4
0
        /// <summary>
        /// Creates an angle joint.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="bodyA">The first body.</param>
        /// <param name="bodyB">The second body.</param>
        /// <returns></returns>
        public static AngleJoint CreateAngleJoint(World world, Body bodyA, Body bodyB)
        {
            AngleJoint angleJoint = new AngleJoint(bodyA, bodyB);
            world.AddJoint(angleJoint);

            return angleJoint;
        }
예제 #5
0
 public static DistanceJoint CreateDistanceJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA,
                                                 Vector2 anchorB)
 {
     DistanceJoint distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB);
     world.AddJoint(distanceJoint);
     return distanceJoint;
 }
예제 #6
0
        public MasslessItem(Texture2D t, PhysicsShape shape, Vector2 position, World w)
            : base(position, w, t.Width, t.Height)
        {
            texture = t;
            body.BodyType = BodyType.Static;
            body.Position = position;

            switch (shape)
            {
                case PhysicsShape.Rectangle:
                    fixture = FixtureFactory.CreateRectangle(
                        texture.Width,
                        texture.Height,
                        0.0f,
                        Vector2.Zero,
                        body);
                    break;
            }

            //fixture.CollisionFilter.IgnoreCollisionWith();

            fixture.OnCollision += new OnCollisionEventHandler(this.OnCollision);
            w.AddBody(body);

            //drawOrigin = new Vector2(texture.Width / 2, texture.Height / 2);
            //drawRectangle = new Rectangle(0, 0, texture.Width, texture.Height);
        }
예제 #7
0
        public Battle(SPRWorld sprWorld, int botHalfWidth, World world, int currentLevel)
        {
            this.sprWorld = sprWorld;
            this.botHalfWidth = botHalfWidth;
            this.world = world;

            xmlDoc = new XmlDocument();

            if (nextAllies == "")
            {
                xmlDoc.Load("Games/SuperPowerRobots/Storage/Allies.xml");
            }
            else
            {
                xmlDoc.LoadXml(nextAllies);
            }

            //xmlDoc.

            XmlNodeList nodes = xmlDoc.GetElementsByTagName("Bot");

            Vector2[] edges = { new Vector2(-botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, botHalfWidth) * Settings.MetersPerPixel, new Vector2(-botHalfWidth, botHalfWidth) * Settings.MetersPerPixel };

            CreateBots(nodes, edges);

            xmlDoc.Load("Games/SuperPowerRobots/Storage/Battles.xml");

            nodes = xmlDoc.GetElementsByTagName("Level");

            nodes = nodes[currentLevel].ChildNodes;

            CreateBots(nodes, edges);
        }
예제 #8
0
        /// <summary>
        /// Creates a breakable body. You would want to remove collinear points before using this.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static BreakableBody CreateBreakableBody(World world, Vertices vertices, float density, Vector2 position,
                                                        Object userData)
        {
            List<Vertices> triangles = EarclipDecomposer.ConvexPartition(vertices);

            BreakableBody breakableBody = new BreakableBody(triangles, world, density, userData);
            breakableBody.MainBody.Position = position;
            world.AddBreakableBody(breakableBody);

            return breakableBody;
        }
예제 #9
0
파일: Bomb.cs 프로젝트: scastle/Solitude
        public Bomb(Vector2 position, World world, Vector2 velocity)
            : base(position, world, TextureStatic.Get("bomb").Width, TextureStatic.Get("bomb").Height)
        {
            body.BodyType = BodyType.Dynamic;
            texture = TextureStatic.Get("bomb");
            fixture = FixtureFactory.CreateCircle(TextureStatic.Get("bomb").Width / 2, 1, body);

            laidTime = GameClock.Now;
            world.AddBody(body);
            body.LinearVelocity = velocity;
        }
예제 #10
0
 public Explosion(Vector2 position, World world, float radius, int power)
     : base(position, world, TextureStatic.Get("solitudeExplosion").Width, TextureStatic.Get("solitudeExplosion").Height)
 {
     body.BodyType = BodyType.Kinematic;
     body.Position = position;
     this.radius = 1f;
     maxRadius = radius;
     this.power = power;
     fixture = FixtureFactory.CreateCircle(this.maxRadius, 0f, body);
     fixture.CollisionFilter.CollidesWith = Category.None;
     texture = TextureStatic.Get("solitudeExplosion");
 }
예제 #11
0
        public SolitudeObject(Vector2 position, World world, Shape shape, float width, float height)
        {
            body = BodyFactory.CreateBody(world, position);

            //currently in wall class:
            fixture = new Fixture(body, shape);
            fixture.OnCollision += new OnCollisionEventHandler(OnCollision);
            //###################

            drawOrigin = new Vector2(width, height);
            drawRectangle = new Rectangle(0, 0, (int)width, (int)height);
        }
예제 #12
0
파일: Mauler.cs 프로젝트: scastle/Solitude
 public Mauler(Vector2 position, World w)
     : base(position, w, TextureStatic.Get("solitudeMauler").Width, TextureStatic.Get("solitudeMauler").Height)
 {
     health = Settings.maulerHealth;
     world = w;
     body.BodyType = BodyType.Dynamic;
     body.Position = position;
     texture = TextureStatic.Get("solitudeMauler");
     fixture = FixtureFactory.CreateCircle(texture.Width/2, 0.25f, body, Vector2.Zero);
     fixture.OnCollision += new OnCollisionEventHandler(OnCollision);
     world.AddBody(body);
     targetPoint = body.Position;
 }
        public BreakableBody(IEnumerable<Vertices> vertices, World world, float density, Object userData)
        {
            _world = world;
            _world.ContactManager.PostSolve += PostSolve;
            MainBody = new Body(_world);
            MainBody.BodyType = BodyType.Dynamic;

            foreach (Vertices part in vertices)
            {
                PolygonShape polygonShape = new PolygonShape(part, density);
                Fixture fixture = MainBody.CreateFixture(polygonShape, userData);
                Parts.Add(fixture);
            }
        }
예제 #14
0
파일: Fighter.cs 프로젝트: scastle/Solitude
 public Fighter(Vector2 position, World w)
     : base(position, w, TextureStatic.Get("fighter").Width, TextureStatic.Get("fighter").Height)
 {
     this.health = Settings.fighterHealth;
     world = w;
     lastShot = GameClock.Now;
     world = w;
     body.BodyType = BodyType.Dynamic;
     body.Position = position;
     texture = TextureStatic.Get("fighter");
     fixture = FixtureFactory.CreateRectangle(texture.Width, texture.Height, 0.25f, Vector2.Zero, body);
     world.AddBody(body);
     targetPoint = body.Position;
 }
예제 #15
0
        public TyTaylor(World w, Vector2 position)
            : base(position, w, TextureStatic.Get("ty").Width, TextureStatic.Get("ty").Height)
        {
            health = Settings.TyHealth;
            world = w;
            lastShot = GameClock.Now;
            body.BodyType = BodyType.Dynamic;
            body.Position = position;
            texture = TextureStatic.Get("ty");
            fixture = FixtureFactory.CreateRectangle(texture.Width, texture.Height, 0.25f, Vector2.Zero, body);
            world.AddBody(body);
            targetPoint = body.Position;

            fixture.OnCollision += new OnCollisionEventHandler(this.OnCollision);
        }
        // </DEBUG!!>
        /// <summary>
        /// Initializes a new instance of the <see cref="StupidGameScreen"/> class.
        /// </summary>
        /// <param name="scoreboardIndex">The game-specific index into the scoreboard.</param>
        public SPRGameScreen(int scoreboardIndex)
            : base(scoreboardIndex)
        {
            this.m_scoreboardIndex = scoreboardIndex;

            currentLevel = 0;
            scoreKeeper = new ScoreKeeper(true);

            previousGameTime = GameClock.Now;
            fantastica = new World(Vector2.Zero);
            Physics.Settings.MaxPolygonVertices = 30; // Defaults to 8? What are we, running on a TI-83 or something?
            Physics.Settings.EnableDiagnostics = false;
            this.sprWorld = new SPRWorld(fantastica, currentLevel);

            // Always call reset at the end of the constructor!
            this.Reset();
        }
예제 #17
0
        /// <summary>
        /// Creates a capsule.
        /// Note: Automatically decomposes the capsule if it contains too many vertices (controlled by Settings.MaxPolygonVertices)
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="height">The height.</param>
        /// <param name="topRadius">The top radius.</param>
        /// <param name="topEdges">The top edges.</param>
        /// <param name="bottomRadius">The bottom radius.</param>
        /// <param name="bottomEdges">The bottom edges.</param>
        /// <param name="density">The density.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static List<Fixture> CreateCapsule(World world, float height, float topRadius, int topEdges,
                                                  float bottomRadius,
                                                  int bottomEdges, float density, Vector2 position, Object userData)
        {
            Vertices verts = PolygonTools.CreateCapsule(height, topRadius, topEdges, bottomRadius, bottomEdges);

            //There are too many vertices in the capsule. We decompose it.
            if (verts.Count >= Settings.MaxPolygonVertices)
            {
                List<Vertices> vertList = EarclipDecomposer.ConvexPartition(verts);
                List<Fixture> fixtureList = CreateCompoundPolygon(world, vertList, density, userData);
                fixtureList[0].Body.Position = position;

                return fixtureList;
            }

            return new List<Fixture> {CreatePolygon(world, verts, density, userData)};
        }
예제 #18
0
파일: Door.cs 프로젝트: scastle/Solitude
 public Door(Vector2 position, World world, float width, float height, float density, WallType type, Direction d)
     : base(position, world, width, height, density, type, d)
 {
     this.type = type;
     textureString = "solitudeWallDoor";
     if (type == WallType.Grip)
     {
         textureString = "solitudeGripDoor";
     }
     direction = d;
     switch (d)
     {
         case Direction.Up: body.Rotation = (float)(3 * Math.PI / 2); break;
         case Direction.Down: body.Rotation = (float)(Math.PI / 2); break;
         case Direction.Left: body.Rotation = (float)Math.PI; break;
         case Direction.Right: body.Rotation = 0; break;
     }
     //body.Rotation = (float)((int)d * (Math.PI / 2));
 }
        // Anything that is a fixture must have an object type for collision logic purposes
        /*public enum ObjectTypes
        {
            Bot = 1,
            Weapon = 2,  // Since weapons will be fixtures, they too need an object type.
            Bullet = 3,
            Wall = 4
        }*/
        public SPRWorld(World world, int currentLevel)
        {
            int botHalfWidth = 31; // Half the bot's width (e.g. the distance from the centroid to the edge)
            m_World = world;
            this.m_Entities = new SortedDictionary<ulong,Entity>();

            // Make polygons out of weapons and anything that needs collision.
            // NOTE: Stores the convex hull for each item, since collision detection
            //          relies upon these verticies being convex polygons.
            String[] toPreload = new String[] { "Gun", "Axe", "Shield" };

            foreach (String texture in toPreload) {
                Texture2D a = TextureStatic.Get(texture);
                uint[] data = new uint[a.Width * a.Height];
                a.GetData<uint>(data);
                Vertices v = Melkman.GetConvexHull(PolygonTools.CreatePolygon(data, a.Width, a.Height));
                Vector2 scale = new Vector2(Settings.MetersPerPixel, Settings.MetersPerPixel);
                v.Scale(ref scale);
                if (!computedSpritePolygons.ContainsKey(texture))
                {
                    computedSpritePolygons.Add(texture, v);
                }
            }

            //walls
            Vector2[] outer = { new Vector2(0, 0) * Settings.MetersPerPixel, new Vector2(0, 1920) * Settings.MetersPerPixel, new Vector2(1080, 1920) * Settings.MetersPerPixel, new Vector2(1080, 0) * Settings.MetersPerPixel };
            Vector2[] inner = { new Vector2(200, 200) * Settings.MetersPerPixel, new Vector2(200, 1720) * Settings.MetersPerPixel, new Vector2(880, 1720) * Settings.MetersPerPixel, new Vector2(880, 200) * Settings.MetersPerPixel };

            FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 100) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(100, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 1920 * Settings.MetersPerPixel, 200 * Settings.MetersPerPixel, 1f, new Vector2(960, 980) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static;
            FixtureFactory.CreateRectangle(world, 200 * Settings.MetersPerPixel, 1080 * Settings.MetersPerPixel, 1f, new Vector2(1820, 540) * Settings.MetersPerPixel, "Wall").Body.BodyType = BodyType.Static;

            Vector2[] edges = { new Vector2(-botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, -botHalfWidth) * Settings.MetersPerPixel, new Vector2(botHalfWidth, botHalfWidth) * Settings.MetersPerPixel, new Vector2(-botHalfWidth, botHalfWidth) * Settings.MetersPerPixel };

            this.battle = new Battle(this, botHalfWidth, world, currentLevel);
            GameWorld.audio.SongPlay("sprbattle");
        }
예제 #20
0
        public Bullet(Vector2 velocity, Vector2 position, World w, Color color, Fixture sender)
            : base(position, w, 15f, 15f)
        {
            senderFixture = sender;
            world = w;
            BulletColor = color;
            body.BodyType = BodyType.Dynamic;
            body.IsBullet = true;
            body.LinearVelocity = velocity;
            body.Position = position;
            texture = TextureStatic.Get("bullet");
            fixture = FixtureFactory.CreateCircle(7f, 1, body);
            body.Mass = 0;
            fixture.Body.UserData = "bullet";
            body.Inertia = 0;
            body.Torque = 0;
            fixture.CollisionFilter.IgnoreCollisionWith(senderFixture);

            fixture.BeforeCollision += new BeforeCollisionEventHandler(BeforeCollision);
            fixture.OnCollision += new OnCollisionEventHandler(OnCollision);

            w.AddBody(body);
        }
        /// <summary>
        /// Creates a chain.
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        /// <param name="linkWidth">The width.</param>
        /// <param name="linkHeight">The height.</param>
        /// <param name="fixStart">if set to <c>true</c> [fix start].</param>
        /// <param name="fixEnd">if set to <c>true</c> [fix end].</param>
        /// <param name="numberOfLinks">The number of links.</param>
        /// <param name="linkDensity">The link density.</param>
        /// <returns></returns>
        public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight,
            bool fixStart, bool fixEnd, int numberOfLinks, float linkDensity)
        {
            //Chain start / end
            Path path = new Path();
            path.Add(start);
            path.Add(end);

            //A single chainlink
            PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight), linkDensity);

            //Use PathManager to create all the chainlinks based on the chainlink created before.
            List<Body> chainLinks = PathManager.EvenlyDistributeShapesAlongPath(world, path, shape, BodyType.Dynamic,
                                                                                numberOfLinks);

            if (fixStart)
            {
                //Fix the first chainlink to the world
                JointFactory.CreateFixedRevoluteJoint(world, chainLinks[0], new Vector2(0, -(linkHeight/2)),
                                                      chainLinks[0].Position);
            }

            if (fixEnd)
            {
                //Fix the last chainlink to the world
                JointFactory.CreateFixedRevoluteJoint(world, chainLinks[chainLinks.Count - 1],
                                                      new Vector2(0, (linkHeight/2)),
                                                      chainLinks[chainLinks.Count - 1].Position);
            }

            //Attach all the chainlinks together with a revolute joint
            PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight),
                                                      new Vector2(0, linkHeight),
                                                      false, false);

            return (path);
        }
예제 #22
0
파일: Wall.cs 프로젝트: scastle/Solitude
        public Wall(Vector2 position, World world, float width, float height, float density, WallType t, Direction d)
            : base(position, world, width, height)
        {
            body.Rotation = 0;

            if (d == Direction.Up || d == Direction.Down) // Up or down
            {
                body.Rotation = (float)Math.PI / 2;
            }

            body.BodyType = BodyType.Static;
            //world.AddBody(body);
            fixture = FixtureFactory.CreateRectangle(width, height, density, Vector2.Zero, body, null);
            fixture.OnCollision += new OnCollisionEventHandler(OnCollision);
            type = t;
            this.width = width;
            this.height = height;

            drawRectangle = new Rectangle(0, 0, (int)width, (int)height);
            switch (type){
                case WallType.Smooth:
                    textureString = /*TextureStatic.Get(*/"solitudeWallSmooth";      break;
                case WallType.HandHold:
                    textureString = /*TextureStatic.Get(*/"solitudeWallHandHold";    break;
                case WallType.Grip:
                    textureString = /*TextureStatic.Get(*/"solitudeWallGrip";        break;
                case WallType.Metal:
                    textureString = /*TextureStatic.Get(*/"solitudeWallMetal";       break;
                case WallType.Hot:
                    textureString = /*TextureStatic.Get(*/"solitudeWallHot";         break;
                case WallType.Cold:
                    textureString = /*TextureStatic.Get(*/"solitudeWallCold";        break;
                case WallType.Spike:
                    textureString = /*TextureStatic.Get(*/"solitudeWallSpike";       break;
            }
        }
예제 #23
0
 public Terminal(Vector2 position, World w, string t)
     : base(TextureStatic.Get("terminal"), PhysicsShape.Rectangle, position, w)
 {
     fixture.OnCollision += new OnCollisionEventHandler(OnCollision);
     fixture.OnSeparation += new OnSeparationEventHandler(OnSeparation);
     text = t;
 }
예제 #24
0
파일: Enemy.cs 프로젝트: scastle/Solitude
 public Enemy(Vector2 position, World world, float width, float height)
     : base(position, world, width, height)
 {
 }
예제 #25
0
 public SolitudeObject(Vector2 position, World world, float width, float height)
 {
     body = BodyFactory.CreateBody(world, position);
     drawOrigin = new Vector2(width / 2f, height / 2f);
     drawRectangle = new Rectangle(0, 0, (int)width, (int)height);
 }
        /// <summary>
        /// Resets this instance. This will be called when a new game is played. Reset all of
        /// your objects here so that you do not need to reallocate the memory for them.
        /// </summary>
        internal override void Reset()
        {
            base.Reset();
            currentLevel = 0;
            scoreKeeper = new ScoreKeeper(true);
            previousGameTime = GameClock.Now;
            fantastica = new World(Vector2.Zero);
            Physics.Settings.MaxPolygonVertices = 30; // Defaults to 8? What are we, running on a TI-83 or something?
            Physics.Settings.EnableDiagnostics = false;
            Battle.nextAllies = "";
            this.sprWorld = new SPRWorld(fantastica, currentLevel);

            this.gameOverTime = GameClock.Now + 10000000 * 10; // 10 Seconds.
        }
 public void NextLevel()
 {
     currentLevel += 1;
     fantastica = new World(Vector2.Zero);
     Physics.Settings.MaxPolygonVertices = 30; // Defaults to 8? What are we, running on a TI-83 or something?
     Physics.Settings.EnableDiagnostics = false;
     addedEndScreen = false;
     this.sprWorld = new SPRWorld(fantastica, currentLevel);
 }
예제 #28
0
 public Sentinel(Vector2 position, Vector2 velocity, World w, int rate)
     : base(position, w, TextureStatic.Get("sentinel").Width, TextureStatic.Get("sentinel").Height)
 {
     health = Settings.sentinelHealth;
     patrolRate = rate;
     lastShot = GameClock.Now;
     lastTurn = lastShot;
     world = w;
     body.BodyType = BodyType.Dynamic;
     speed = velocity;
     body.LinearVelocity = velocity;
     //body.Position = position;
     texture = TextureStatic.Get("sentinel");
     fixture = FixtureFactory.CreateRectangle(texture.Width, texture.Height, 0.25f, Vector2.Zero, body);
     world.AddBody(body);
 }
예제 #29
0
파일: Body.cs 프로젝트: scastle/Solitude
        public Body(World world, Object userData)
        {
            FixtureList = new List<Fixture>(32);

            World = world;
            UserData = userData;

            FixedRotation = false;
            IsBullet = false;
            SleepingAllowed = true;
            Awake = true;
            BodyType = BodyType.Static;
            Enabled = true;

            Xf.R.Set(0);

            world.AddBody(this);
        }
예제 #30
0
파일: Body.cs 프로젝트: scastle/Solitude
 public Body(World world)
     : this(world, null)
 {
 }