The world class manages all physics entities, dynamic simulation, and asynchronous queries.
Inheritance: IDisposable
        /**
         * Creates a new circle object.
         */
        public CircleObject(World world, string texturename, float density, float friction, float restitution,float myScale)
            : base(world)
        {
            textureName = texturename;

            // Initialize
            this.texture = GameEngine.TextureList[texturename];
            TextureFilename = texturename;

            Width = texture.Width * myScale;
            Height = Width;

            scale = myScale;

            // Determine dimension
            float radius = (float)texture.Width / (2 * CASSWorld.SCALE) * scale;

            // Create collision shape
            CircleDef shape = new CircleDef();
            shape.Radius = radius;
            shape.Density = density;
            shape.Friction = friction;
            shape.Restitution = restitution;
            shapes.Add(shape);
        }
        //public SensorObject(World world, Texture2D texture)
        //    : base(world, texture, 0, 0, 0,1,false)
        public SensorObject(World world, string animTexturename, string objectTextureName, int sprWidth, int sprHeight, int animInt, int myNumFrames)
            : base(world, objectTextureName, 0, 0, 0, 1, false)
        {
            TextureFilename = objectTextureName;

            animTexture = GameEngine.TextureList[animTexturename];
            animTextureName = animTexturename;
            //Texture2D objectTexture = GameEngine.TextureList[objectTextureName];

            //Texture2D texture = GameEngine.TextureList[texturename];
            //TextureFilename = texturename;
            //boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Height, (int)Width);

            shapes[0].IsSensor = true;

            animate = false;
            myGameTime = 0;
            animateTimer = 0;
            animateInterval = animInt;
            xFrame = 0;
            yFrame = 0;
            numFrames = myNumFrames;
            spriteWidth = sprWidth;
            spriteHeight = sprHeight;
            sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        }
        /**
         * Creates a new box object
         */
        /*
        public BoxObject(World world, Texture2D texture, float density, float friction, float restitution, float myScale, bool isPulley)
            : base(world)
        {

            // Initialize
            this.texture = texture;

            scale = myScale;

            if (isPulley)
                BodyDef.FixedRotation = true;

            // Determine dimensions
            float halfWidth = ((float)texture.Width / (2 * CASSWorld.SCALE)) * scale;
            float halfHeight = ((float)texture.Height / (2 * CASSWorld.SCALE)) * scale;

            // Create the collision shape
            PolygonDef shape = new PolygonDef();
            shape.SetAsBox(halfWidth, halfHeight);
            shape.Density = density;
            shape.Friction = friction;
            shape.Restitution = restitution;
            shapes.Add(shape);
        }

        */
        public BoxObject(World world, string textureName, float density, float friction, float restitution, float myScale, bool isPulley)
            : base(world)
        {
            // Initialize
            //Console.WriteLine(textureName);
            this.texture = GameEngine.TextureList[textureName];
            TextureFilename = textureName;
            Height = texture.Height * myScale;
            Width = texture.Width * myScale;
            boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height);

            scale = myScale;

            if (isPulley)
                BodyDef.FixedRotation = true;

            // Determine dimensions
            float halfWidth = (float)texture.Width / (2 * CASSWorld.SCALE) * scale ;
            float halfHeight = (float)texture.Height / (2 * CASSWorld.SCALE) * scale;

            // Create the collision shape
            PolygonDef shape = new PolygonDef();
            shape.SetAsBox(halfWidth, halfHeight);
            shape.Density = density;
            shape.Friction = friction;
            shape.Restitution = restitution;
            shape.UserData = textureName; // for DEBUGging purposes
            shapes.Add(shape);
        }
        /**
         * Creates a new drawn object
         */
        public InstasteelObject(World world, string textureName, float amountofis, float density, float friction, float restitution, float myScale, bool isPulley)
            : base(world, blobtexturename, segmenttexturename, blobs)
        {
            shapes.Clear();

            amountOfInstasteel = amountofis;

            this.texture = GameEngine.TextureList[textureName];
            TextureFilename = textureName;
            Height = texture.Height * myScale;
            Width = texture.Width * myScale;
            boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height);

            scale = myScale;

            if (isPulley)
                BodyDef.FixedRotation = true;

            // Determine dimensions
            float halfWidth = (float)texture.Width / (2 * CASSWorld.SCALE) * scale ;
            float halfHeight = (float)texture.Height / (2 * CASSWorld.SCALE) * scale;

            // Create the collision shape
            PolygonDef shape = new PolygonDef();
            shape.SetAsBox(halfWidth, halfHeight);
            shape.Density = density;
            shape.Friction = friction;
            shape.Restitution = restitution;
            shapes.Add(shape);
        }
 //, float headXPos,float headYPos)
 public PistonObject(World world, float myEndScale, float myHeadScale, float myHeadMinVal, float myHeadMaxVal, float myEndXPos, float myEndYPos, float myTrueInc, float myFalseInc, Vector2 headPos)
     : base(world, "Art\\Objects\\PistonObjects\\piston_moving", 0f, .5f, 0.0f, myHeadScale, false)
 {
     TextureFilename = "Art\\Objects\\PistonObjects\\piston_moving";
     Console.WriteLine(" in po1  {0}", headPos);
     //po = new PistonObject(world.World, .5f, .5f, 12f, 13f, 9.7f, 12.6f, .01f, .2f, gameposition);
     // private static Vector2 pistonPosition = new Vector2(12f, 13.2f);
     /*
     endXPos = myEndXPos;
     endYPos = myEndYPos;
      */
     endXPos = headPos.X - 2.3f;
     endYPos = headPos.Y - .6f;
     Console.WriteLine(" in po2  {0} {1}", endXPos, endYPos);
     //headScale = myHeadScale; needed?
     //endScale = myEndScale;
     endScale = myEndScale;
        // headMinVal = myHeadMinVal;
     //headMaxVal = myHeadMaxVal;
     headMinVal = headPos.X;
     headMaxVal = headPos.X + .6f;
     trueInc = myTrueInc;
     falseInc = myFalseInc;
     adjusted = false;
 }
Beispiel #6
0
        private Transform _xf; // the body origin transform

        #endregion Fields

        #region Constructors

        internal Body(BodyDef bd, World world)
        {
            _flags = 0;

            if (bd.IsBullet)
            {
                _flags |= BodyFlags.Bullet;
            }
            if (bd.FixedRotation)
            {
                _flags |= BodyFlags.FixedRotation;
            }
            if (bd.AllowSleep)
            {
                _flags |= BodyFlags.AllowSleep;
            }
            if (bd.IsSleeping)
            {
                _flags |= BodyFlags.Sleep;
            }

            _world = world;

            _xf.Position = bd.Position;
            _xf.R.Set(bd.Angle);

            _sweep.LocalCenter.SetZero();
            _sweep.T0 = 1.0f;
            _sweep.A0 = _sweep.A = bd.Angle;
            _sweep.C0 = _sweep.C = Math.Mul(_xf, _sweep.LocalCenter);

            _jointList = null;
            _contactList = null;
            _prev = null;
            _next = null;

            _linearVelocity = bd.LinearVelocity;
            _angularVelocity = bd.AngularVelocity;

            _linearDamping = bd.LinearDamping;
            _angularDamping = bd.AngularDamping;

            _force.Set(0.0f, 0.0f);
            _torque = 0.0f;

            _sleepTime = 0.0f;

            _mass = 0;
            _invMass = 0.0f;
            _I = 0.0f;
            _invI = 0.0f;

            _type = BodyType.Static;

            _userData = bd.UserData;

            _fixtureList = null;
            _fixtureCount = 0;
        }
Beispiel #7
0
        public PhysicsActor(World _world, Vector2 _position, float _angle = 0.0f, bool _isStatic = false)
        {
            BodyDef bodydef = new BodyDef();
            bodydef.Position = _position;
            bodydef.Angle = 0.0f;

            body = _world.CreateBody(bodydef);
        }
 public SeeSawObject(World world, string myTexturePath, float myScale, Vector2 myPosition)
     : base(world, myTexturePath, 1f, .5f, 0.0f, myScale, false)
 {
     hack = true;
     theWholeWideWorld = world;
     texturePath = myTexturePath;
     TextureFilename = texturePath;
     seeSawTexture = GameEngine.TextureList[texturePath];
 }
 /** Handles anchoring the first and last pieces of the bridge
  * to the world versus to other bridge pieces
  */
 private void CreateEndJoint(World world, bool leftEnd)
 {
     RevoluteJointDef joint = new RevoluteJointDef();
     Vector2 anchor = Position - new Vector2(width/2, 0);
     if (!leftEnd)
         anchor = Position + new Vector2(width / 2, 0);
     joint.Initialize(Body, world.GetGroundBody(), Utils.Convert(anchor));
     world.CreateJoint(joint);
 }
 public BackgroundObject(World myWorld, ScrollingWorld myScrollingWorld, string myDisplayTextureName, Vector2 myPosition)
     : base(myWorld, myDisplayTextureName, 0f, .5f, 0.0f, 1, false)
 {
     displayTexture = GameEngine.TextureList[myDisplayTextureName];
     displayTextureName = myDisplayTextureName;
     world = myScrollingWorld;
     position = myPosition;
     destroyedBody = false;
     shapes[0].IsSensor = true;
 }
Beispiel #11
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 #12
0
 public static Box Create(
         World world, float positionX, float positionY, float width, float height, float density, float friction)
 {
     var result = new Box();
     result.Width = width;
     result.Height = height;
     result.Density = density;
     result.Color = System.Drawing.Color.White;
     result.CreatePhysics(world, positionX, positionY, friction);
     return result;
 }
        public CASSWorld(float width, float height, Vector2 gravity)
        {
            // Create the world's axis-aligned bounding box
            AABB aabb = new AABB();
            aabb.LowerBound = new Vec2(-MARGIN, -MARGIN);
            aabb.UpperBound = new Vec2(width + MARGIN, height + MARGIN);

            world = new World(aabb, Utils.Convert(gravity), true);

            succeeded = failed = false;
        }
Beispiel #14
0
 public static Circle Create(
         World world, float positionX, float positionY, float radius, float density, float friction)
 {
     var result = new Circle();
     result.Radius = radius;
     result.Density = density;
     result.Color = System.Drawing.Color.White;
     result.RadiusColor = Helper.GetRandomColor();
     result.CreatePhysics(world, positionX, positionY, friction);
     return result;
 }
Beispiel #15
0
        private void CreatePhysicsWorld(PhysicsWorld2D physicsWorld)
        {
            AABB worldAABB = new AABB();

            worldAABB.LowerBound.Set(-10000, -10000);
            worldAABB.UpperBound.Set(10000, 10000);
            Vec2 gravity = physicsWorld.Gravity;

            Box2DX.Dynamics.World world = new Box2DX.Dynamics.World(worldAABB, gravity, false);
            physicsWorld.World = world;
            physicsWorld.SetBulletListner();
        }
Beispiel #16
0
 public Test()
 {
     AABB aabb = new AABB();
     aabb.UpperBound = new UnityEngine.Vector2(100,100);
     aabb.LowerBound = new UnityEngine.Vector2(-100,-100);
     World world = new World(aabb, new Vector2(0, -1), false);
     BodyDef groundBodyDef = new BodyDef();
     groundBodyDef.Position.Set(0, 0);
     PolygonShape groundBox = new PolygonShape();
     groundBox.SetAsBox(10, 1);
     Body body = new Body(groundBodyDef, world);
 }
Beispiel #17
0
        public InGameState()
        {
            //position the score display centered at the top
            ScoreDisplay.CharacterSize = 90;
            ScoreDisplay.DisplayedString = InGameState.WinCount0.ToString();
            float leftSize = ScoreDisplay.GetLocalBounds().Width;
            ScoreDisplay.DisplayedString += " : ";
            float midSize = ScoreDisplay.GetLocalBounds().Width - leftSize;
            ScoreDisplay.DisplayedString += InGameState.WinCount1.ToString();
            ScoreDisplay.Origin = new Vector2(leftSize + 0.5f * midSize, 0f);
            ScoreDisplay.Position = new Vector2(Constants.windowSizeX / 2 , Constants.windowSizeY / 14);
            ScoreDisplay.Color = new SFML.Graphics.Color(200, 255, 200);

            AABB aabb = new AABB();
            aabb.LowerBound.Set(0.0f, 0.0f);
            aabb.UpperBound.Set(800, 600/*Constants.worldSizeX * Constants.screenRatio*/);

            physicsWorld = new World(aabb, new Vec2(0.0f, -9.81f), false);

            contactManager = Physics.ContactManager.g_contactManager;
            physicsWorld.SetContactListener(contactManager);

             // Set new Players and appending dekoHands
            ResetPlayers();
            setDekoFlags();
            //0xF0A58A4
            groundPolygonAct = new Actors.PolygonActor(physicsWorld, new Vec2(0.0f, 15.0f), 0xFBA58A4, Actors.FunctionType.GradientNoise, 4);

            BackgroundBackSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundBack));
            BackgroundBackSprite.Scale = new Vector2(Constants.windowSizeX / (float)BackgroundBackSprite.TextureRect.Width, Constants.windowSizeY / (float)BackgroundBackSprite.TextureRect.Height);//0.5F * Vector2.One;
            BackgroundFrontSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.InGameBackGroundFront));
            BackgroundFrontSprite.Scale = BackgroundBackSprite.Scale;

            //left and right borders of the map
            BodyDef bodydef = new BodyDef();
            bodydef.Position = new Vector2(0,0);
            bodydef.Angle = 0.0f;
            PolygonDef box = new PolygonDef();
            box.SetAsBox(1f, Constants.worldSizeY);

            Body leftEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(leftEdge.CreateShape(box));

            bodydef.Position = new Vector2(Constants.worldSizeX-1, 0);
            Body rightEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(rightEdge.CreateShape(box));

            bodydef.Position = new Vector2(0, Constants.worldSizeY);
            box.SetAsBox(Constants.worldSizeX, 1f);
            Body topEdge = physicsWorld.CreateBody(bodydef);
            contactManager.addNonLethalShape(topEdge.CreateShape(box));
        }
Beispiel #18
0
 protected override void CreatePhysics(World world, float positionX, float positionY, float friction)
 {
     var bodyDef = new BodyDef();
     bodyDef.Position.Set(positionX, positionY);
     this.body = world.CreateBody(bodyDef);
     var shapeDef = new PolygonDef();
     shapeDef.SetAsBox(this.Width, this.Height);
     shapeDef.Density = this.Density;
     shapeDef.Friction = friction;
     shapeDef.Restitution = 0.3f;
     this.body.CreateShape(shapeDef);
     this.body.SetMassFromShapes();
 }
 public FanObject(World world, Texture2D mytexture, Texture2D objectTexture)
     : base(world, objectTexture, 0f, .5f, 0.0f, 1, false)
 {
     //animation stuff
     myGameTime = 0;
     animateTimer = 0;
     animateInterval = 20;
     animTexture = mytexture;
     xFrame = 0;
     yFrame = 0;
     spriteWidth = 200;
     spriteHeight = 200;
     sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);
     origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
 }
Beispiel #20
0
        /// <summary>
        /// ワールドを初期化
        /// </summary>
        /// <param name="worldRect">適用範囲</param>
        /// <param name="gravity">重力</param>
        public World(RectF worldRect, Vector2F gravity)
        {
            physicsCollider     = new List <PhysicsColliderNode>();
            collisionController = new CollisionController(this);
            AABB aabb = new AABB();

            aabb.LowerBound = worldRect.Position.ToB2Vector();
            aabb.UpperBound = (worldRect.Position + worldRect.Size).ToB2Vector();
            B2World         = new Box2DX.Dynamics.World(aabb, gravity.ToB2Vector(false), true);
            B2World.SetContactListener(collisionController);
            B2World.SetContactFilter(new ContactFilter());
            TimeStep           = 1.0f / 60.0f;
            VelocityItetions   = 8;
            PositionIterations = 1;
        }
Beispiel #21
0
 protected override void CreatePhysics(World world, float positionX, float positionY, float friction)
 {
     var bodyDef = new BodyDef();
     bodyDef.Position.Set(positionX, positionY);
     bodyDef.LinearDamping = 0.3f;
     bodyDef.AngularDamping = 0.6f;
     var circleDef = new CircleDef();
     circleDef.Density = this.Density;
     circleDef.Radius = this.Radius * 0.5f;
     circleDef.Friction = friction;
     circleDef.Restitution = 0.3f;
     this.body = world.CreateBody(bodyDef);
     this.body.CreateShape(circleDef);
     this.body.SetMassFromShapes();
 }
        public HackObject(World world,  int myWidth, int myHeight, int myX, int myY, float myFriction)
            : base(world)
        {
            width = myWidth;
            height = myHeight;
            x = myX;
            y = myY;

            float halfWidth = width / (2 * CASSWorld.SCALE);
            float halfHeight = height / (2 * CASSWorld.SCALE);

            PolygonDef shape = new PolygonDef();
            shape.SetAsBox(halfWidth, halfHeight);
            shape.Friction = myFriction;
            shapes.Add(shape);

            BodyDef.Position.X = x;
            BodyDef.Position.Y = y;
        }
        /**
         * Creates a new rope bridge with plank texture 'texture'
         * at height y, from x-position x1 to x-position x2, using
         * specified density, friction, and restitution coefficient.
         */
        public RopeBridge(World world, Texture2D texture, float y, float x1, float x2, float density, float friction, float restitution)
            : base(world, "DISABLED", density, friction, restitution, 1, false)
        {
            width = texture.Width / CASSWorld.SCALE;

            int nLinks = (int)((x2 - x1) / width);
            if (nLinks <= 0)
            {
                Position = new Vector2(x2, y);
                return;
            }

            spacing = (x2 - x1) - nLinks * width;
            spacing /= nLinks;

            Position = new Vector2(x1, y);

            children.Add(new RopeBridge(world, texture, y, x1 + width + spacing, x2, density, friction, restitution));
        }
        public HoleObject(World world, string animTexturename, string objectTexturename)
            : base(world, objectTexturename, 0f, .5f, 0.0f,1,false)
        {
            //thisTexture = mytexture;
            texture = GameEngine.TextureList[objectTexturename];
            TextureFilename = objectTexturename;
            animTexture = GameEngine.TextureList[animTexturename];
            animTextureName = animTexturename;

            //animation stuff
            myGameTime = 0;
            animateTimer = 0;
            animateInterval = 40;
            xFrame = 0;
            yFrame = 0;
            spriteWidth = 800;
            spriteHeight = 500;
            sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        }
        public AnimationObject(World world, string animTexturename, string objectTexturename, int sprWidth, int sprHeight, int animInt, int myNumFrames, bool hasBody, bool deathObject)
            : base(world, objectTexturename, 0f, .5f, 0.0f, 1, false)
        {
            animTextureName = animTexturename;
            //Console.WriteLine("***" + animTexturename + "***");
            animTexture = GameEngine.TextureList[animTexturename];

            myGameTime = 0;
            animateTimer = 0;
            animateInterval = animInt;
            xFrame = 0;
            yFrame = 0;
            numFrames = myNumFrames;
            spriteWidth = sprWidth;
            spriteHeight = sprHeight;
            sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);

            this.hasBody = hasBody;
            this.deathObject = deathObject;
        }
        public FailButtonObject(World world, string mytexturename, string objectTexturename, int sprWidth, int sprHeight, int animInt, int myNumFrames)
            : base(world, objectTexturename, 0f, .5f, 0.0f, .5f, false)
        {
            animTexture = GameEngine.TextureList[mytexturename];
            animTextureName = mytexturename;
            TextureFilename = objectTexturename;

            deadCosmo = false;

            myGameTime = 0;
            animateTimer = 0;
            animateInterval = animInt;
            //animTexture = mytexture;
            xFrame = 0;
            yFrame = 0;
            numFrames = myNumFrames;
            spriteWidth = sprWidth;
            spriteHeight = sprHeight;
            sourceRect = new Rectangle(xFrame * spriteWidth, yFrame * spriteHeight, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        }
Beispiel #27
0
        public static void CreateMound(World world, float x, float y, float friction, float restitution)
        {
            // ==== Mound ====
            BodyDef moundBodyDef = new BodyDef();
            moundBodyDef.Position.Set(x, y);

            // Mound body.
            Body moundBody = world.CreateBody(moundBodyDef);

            // Define shape.
            PolygonDef moundShapeDef = new PolygonDef();
            moundShapeDef.VertexCount = 3;
            moundShapeDef.Vertices[0].Set(0.20f, 0f);
            moundShapeDef.Vertices[1].Set(0f, 0.025f);
            moundShapeDef.Vertices[2].Set(-0.20f, 0f);
            moundShapeDef.Friction = friction;
            moundShapeDef.Restitution = restitution;
            moundShapeDef.Filter.CategoryBits = 0x3;

            // Add shape to body.
            moundBody.CreateShape(moundShapeDef);
        }
        /**
         * Creates a new polygon object.
         * Uses a divide-and-conquer algorithm
         * to triangulate the polygon, since triangles
         * are guaranteed to be convex, a Box2DX
         * requirement.  Colinear consecutive points
         * are okay, but the polygon must be simple -
         * i.e. no crossing edges.
         */
        public PolygonObject(World world, Vector2[] points, Texture2D texture, float density, float friction, float restitution)
            : base(world)
        {
            this.texture = texture;
            Height = texture.Height;
            Width = texture.Width;
            TextureFilename = texture.Name;

            Debug.Assert(points.Length >= 3);

            LinkedList<int> polygon = new LinkedList<int>();
            for (int i = 0; i < points.Length; i++)
                polygon.AddLast(i);

            // Triangles generated
            List<Vector2[]> triangles = new List<Vector2[]>();

            Split(polygon, points, triangles);

            CreateShapes(triangles, density, friction, restitution);
            CreateDrawable(triangles);
        }
        /**
         * Creates a new box object
         */
        public MovingObject(World world, string texturename, float density, float friction, float restitution, float myScale, bool isPulley, SwitchObject mySwitch, Vector2 myForce, float bound1, float bound2)
            : base(world)
        {
            BodyDef.FixedRotation = true;
            texture = GameEngine.TextureList[texturename];
            TextureFilename = texturename;

            Height = texture.Height * myScale;
            Width = texture.Width * myScale;
            boundingBox = new Rectangle((int)Position.X, (int)Position.Y, (int)Width, (int)Height);

            this.bound1 = bound1;
            this.bound2 = bound2;
            this.myForce = myForce;
            this.mySwitch = mySwitch;
            BodyDef.IsBullet = true;
            // Initialize
            this.texture = texture;

            scale = myScale;

            if (isPulley)
                BodyDef.FixedRotation = true;

            // Determine dimensions
            float halfWidth = ((float)texture.Width / (2 * CASSWorld.SCALE)) * scale;
            float halfHeight = ((float)texture.Height / (2 * CASSWorld.SCALE)) * scale;

            // Create the collision shape
            PolygonDef shape = new PolygonDef();
            shape.SetAsBox(halfWidth, halfHeight);
            shape.Density = density;
            shape.Friction = friction;
            shape.Restitution = restitution;
            shapes.Add(shape);

            isMoving = false;
        }
        private void SetupJointsHelper(World world)
        {
            if (children.Count == 0)
            {
                //If there are no children left, set up the final plank and stop recursing
                CreateEndJoint(world, false);
                return;
            }

            //nextBoard is the next piece of the bridge
            RopeBridge nextBoard = children[0] as RopeBridge;

            // Attach this board's body to nextBoard's body with a joint
            /////////////////////////////////////////////////

            RevoluteJointDef joint = new RevoluteJointDef();
            Vector2 anchor = Position + new Vector2(width / 2, 0);
            joint.Initialize(Body, nextBoard.Body, Utils.Convert(anchor));
            world.CreateJoint(joint);

            /////////////////////////////////////////////////

            nextBoard.SetupJointsHelper(world);
        }
Beispiel #31
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 #32
0
        public static Car Create(World world, float positionX, float positionY, float width, float height)
        {
            float poleHeightCarHeightRatio = 0.7f;

            var car = new Car();
            car.Width = width;
            car.Height = height;

            float carHeight = height * (1.0f - poleHeightCarHeightRatio);
            float bodyHeightWheelHeightRatio = 0.4f;
            float bodyHeight = carHeight * bodyHeightWheelHeightRatio;
            float wheelRadius = carHeight - bodyHeight;
            float bodyDensity = 1.5f;
            float wheelDensity = 0.4f;
            float wheelFriction = 0.9f;
            var poleDensity = 0.2f;

            float poleWidth = car.Width / 10.0f;
            float poleHeight = car.Height * poleHeightCarHeightRatio;

            car.CarBody = Box.Create(world, positionX, positionY, car.Width, bodyHeight, bodyDensity);
            car.FrontWheel = Circle.Create(
                    world,
                    positionX - car.Width + wheelRadius / 2.0f,
                    positionY - car.CarBody.Height,
                    wheelRadius,
                    wheelDensity,
                    wheelFriction);
            car.BackWheel = Circle.Create(
                    world,
                    positionX + car.Width - wheelRadius / 2.0f,
                    positionY - car.CarBody.Height,
                    wheelRadius,
                    wheelDensity,
                    wheelFriction);
            car.Pole = Box.Create(
                    world, positionX, positionY + bodyHeight*1f + poleHeight, poleWidth, poleHeight, poleDensity);

            car.BodyColor = Helper.GetRandomColor();
            car.WheelsColor = Helper.GetRandomColor();
            car.Pole.Color = Helper.GetRandomColor();
            car.CreatePhysics(world,positionX, positionY);
            return car;
        }
Beispiel #33
0
        protected override void CreatePhysics(World world, float positionX, float positionY)
        {
            var frontWheelJointDef = new RevoluteJointDef();
            frontWheelJointDef.Initialize(CarBody.Body, FrontWheel.Body, FrontWheel.Body.GetWorldCenter());
            frontWheelJoint = (RevoluteJoint)world.CreateJoint(frontWheelJointDef);

            var backWheelJointDef = new RevoluteJointDef();
            backWheelJointDef.Initialize(CarBody.Body, BackWheel.Body, BackWheel.Body.GetWorldCenter());
            backWheelJoint = (RevoluteJoint)world.CreateJoint(backWheelJointDef);

            var poleJointDef = new RevoluteJointDef();

            var anchor = this.Pole.Body.GetWorldCenter();
            anchor.Y -= Pole.Height  ;
            poleJointDef.CollideConnected = false;
            poleJointDef.EnableLimit = true;
            poleJointDef.LowerAngle = Helper.DegreesToRad(-90);
            poleJointDef.UpperAngle = Helper.DegreesToRad(90);
            poleJointDef.Initialize(CarBody.Body, Pole.Body, anchor);
            poleJoint = (RevoluteJoint)world.CreateJoint(poleJointDef);
        }
Beispiel #34
0
        /*
         * @param _funcType Style of the top line that generated
         * @param _res Size of mapunits for one linear segment
         * */
        public PolygonActor(World _world, Vector2 _position, uint _seed, FunctionType _funcType, int _res = 5)
            : base(_world, _position)
        {
            int lineCount = (int)Constants.worldSizeX / _res;

            //make sure to have an even number
            if (lineCount % 2 != 0) lineCount++;
            Vec2[] verts = new Vec2[(int)lineCount + 1 + 4];
            vertexBuffer = new VertexArray(PrimitiveType.LinesStrip);

            Vector2 posScreen = _position.toScreenCoord();

            //repeatable random sequenze
            Rand rnd = new Rand(_seed);

            //start and end have even ground
            verts[0] = new Vec2(0, 6);
            verts[1] = new Vec2(_res, 6);
            verts[2] = new Vec2(_res + _res, 6);

            verts[lineCount - 2] = new Vec2(_res * (lineCount - 2), 6);
            verts[lineCount-1] = new Vec2(_res * (lineCount-1), 6);
            verts[lineCount] = new Vec2(_res * lineCount, 6);

            vertexBuffer.Append(new Vertex(((Vector2)verts[0] + _position).toScreenCoord()));
            //create the function
            if (_funcType == FunctionType.Simple)
            {
                for (int i = 2; i <= lineCount; ++i)
                {
                    //Vector2 pos = new Vec2(i * 5, 10 + Rand.IntValue(10));
                    Vector2 pos = new Vec2(i * _res, System.Math.Max((verts[i - 1].Y + (int)rnd.next(6) - 3), 0));
                    verts[i] = pos;
                }
            }
            else if(_funcType == FunctionType.GradientNoise)
            {
                for (int i = 2; i < lineCount-3;)
                {
                    int nextGrad = i + 4;

                    if (nextGrad < lineCount - 2)
                    {
                        verts[nextGrad] = new Vec2(nextGrad * _res, rnd.next((int)maxHeight));
                    }
                    else nextGrad = lineCount - 2;

                    //interpolate between
                    float relativeA = verts[i].Y / maxHeight;
                    float relativeB = verts[nextGrad].Y / maxHeight;
                    for (int c = i + 1; c < nextGrad; ++c)
                    {
                        verts[c] = new Vec2(c * _res, maxHeight * interpolateCos(relativeA, relativeB, (float)(c - i) / 4));
                    }

                    i = nextGrad;
                }

            }

            Array.Resize<Body>(ref triangleBodys, lineCount);

            PolygonDef triangleDef = new PolygonDef();
            triangleDef.Density = 0.0f;
            triangleDef.Friction = 1.0f;
            triangleDef.VertexCount = 3;

            BodyDef bodydef = new BodyDef();
            bodydef.Position = _position;
            bodydef.Angle = 0.0f;

            //convert to triangles
            for (int i = 0; i < lineCount; ++i)
            {
                //always 3 points of the function form a triangle
                triangleDef.Vertices[0] = verts[i];
                triangleDef.Vertices[1] = verts[i] - new Vec2(0.0f, 50.0f);
                triangleDef.Vertices[2] = verts[i + 1];//.Y < verts[i+1].Y ? verts[i] : verts[i + 1]

                triangleBodys[i] = _world.CreateBody(bodydef);
                triangleBodys[i].CreateShape(triangleDef);

                vertexBuffer.Append(new Vertex(((Vector2)verts[i+1] + _position).toScreenCoord()));
            }
        }