Example #1
0
        public Body CreateBall(World world, float ScaleFactor)
        {
            var bodyDef = new BodyDef();

            bodyDef.type = BodyType.Dynamic;

            var ballShape = new CircleShape();

            ballShape._radius = (texture.Width / 2f) * ScaleFactor;

            var ballFixture = new FixtureDef();

            ballFixture.friction = 0.0f; // no friction

            ballFixture.restitution = 1.0f; // give the ball a perfect bounce

            ballFixture.density = 1.0f;

            ballFixture.shape = ballShape;

            var ballBody = world.CreateBody(bodyDef);

            ballBody.CreateFixture(ballFixture);

            // ballBody.Position = new Vector2(((float)r.NextDouble() * 4.5f + .3f), (float)r.NextDouble() * 4.5f + .3f);

            //ballBodies.Add(ballBody);

            return ballBody;
        }
        public Soldier(Shape shape, Vector2 position, GameContent gameContent, World world)
        {
            this.gameContent = gameContent;
            this.world = world;

            idle = new Animation(gameContent.idle[(int)shape], 20f, false);
            walk = new Animation(gameContent.walk[(int)shape], 0.15f, true);
            animationPlayer.PlayAnimation(idle);

            MaxHealth = 10; health = gameContent.random.Next(2, 10);
            MaxReloadTime = gameContent.random.Next(50); reloadTime = 0;

            CircleShape cShape = new CircleShape();
            cShape._radius = (Size + 2) / 2 / gameContent.b2Scale;

            BodyDef bd = new BodyDef();
            bd.fixedRotation = true;
            bd.type = BodyType.Dynamic;
            bd.position = position / gameContent.b2Scale;
            body = world.CreateBody(bd);
            //body.SetLinearDamping(10);

            FixtureDef fd = new FixtureDef();
            fd.shape = cShape;
            fd.restitution = 0.5f;
            fd.friction = .1f;
            fd.density = .1f;

            body.CreateFixture(fd);
            body.SetUserData(this);
        }
        public Player(GameContent gameContent, World world, Vector2 position)
        {
            this.gameContent = gameContent;
            this.world = world;

            idle = new Animation(gameContent.playerIdle, 2, 2f, true, new Vector2(0.5f));
            walk = new Animation(gameContent.playerWalk, 2, 0.2f, true, new Vector2(0.5f));
            die = new Animation(gameContent.playerDie, 2, 0.2f, false, new Vector2(0.5f));

            animationPlayer.PlayAnimation(idle);

            BodyDef bd = new BodyDef();
            bd.position = position / gameContent.b2Scale;
            bd.type = BodyType.Dynamic;
            bd.linearDamping = 10;
            body = world.CreateBody(bd);

            CircleShape cs = new CircleShape();
            cs._radius = (float)(idle.FrameWidth - 1) / gameContent.b2Scale / 2;
            FixtureDef fd = new FixtureDef();
            fd.shape = cs;
            fd.filter.groupIndex = -1;

            body.CreateFixture(fd);
        }
        public Car(Vector2 position, GameContent gameContent, World world)
        {
            this.world = world;
            this.gameContent = gameContent;

            BodyDef bd = new BodyDef();
            bd.position = position / gameContent.Scale;
            bd.type = BodyType.Dynamic;
            bd.bullet = true;

            body = world.CreateBody(bd);
            body.SetLinearDamping(1f); body.SetAngularDamping(0.1f);

            float width = gameContent.playerCar.Width, height = gameContent.playerCar.Height;

            FixtureDef fd = new FixtureDef();
            fd.density = 0.1f;
            //fd.restitution = .1f;

            CircleShape cs = new CircleShape();
            cs._p = new Vector2(0, -(height - width / 2)) / gameContent.Scale;
            cs._radius = width / 2 / gameContent.Scale;
            fd.shape = cs; body.CreateFixture(fd);

            PolygonShape ps = new PolygonShape();
            ps.SetAsBox(width / 2 / gameContent.Scale, (height - width / 2) / 2 / gameContent.Scale,
                new Vector2(0, -(height - width / 2) / 2) / gameContent.Scale, 0);
            fd.shape = ps; body.CreateFixture(fd);

            CreateWheels();
        }
        public Enemy(World world, GameContent gameContent, int index, Vector2 position)
        {
            this.gameContent = gameContent;

            walk = new Animation(gameContent.enemy[index], 2, 0.15f, true, new Vector2(0.5f));
            animationPlayer.PlayAnimation(walk);

            if (index == 0) linearImpulse = 1f / 2;
            else linearImpulse = 0.75f / 2;

            BodyDef bd = new BodyDef();
            bd.position = position / gameContent.b2Scale;
            bd.type = BodyType.Dynamic;
            bd.linearDamping = 10;
            body = world.CreateBody(bd);

            CircleShape cs = new CircleShape();
            cs._radius = (float)(walk.FrameWidth - 1) / gameContent.b2Scale / 2;
            FixtureDef fd = new FixtureDef();
            fd.shape = cs;
            fd.filter.groupIndex = -1;

            body.CreateFixture(fd);
            body.SetUserData(this);
        }
        public PhysicsCircle(Texture2D sprite_texture, World physicsWorld, float radius,
            float positionX, float positionY, float rotation, float density)
            : base(sprite_texture)
        {
            this.scaleToFitTheseDimensions(radius*2.0f, radius*2.0f);
            this.position.X = positionX;
            this.position.Y = positionY;
            this.rotation = rotation;

            BodyDef dynamicBodyDef = new BodyDef();
            dynamicBodyDef.type = BodyType.Dynamic;
            dynamicBodyDef.position = new Vector2(positionX / ScreenPixelsPerMeter, positionY / ScreenPixelsPerMeter);
            dynamicBodyDef.linearDamping = 0.0f;
            Body dynamicBody = physicsWorld.CreateBody(dynamicBodyDef);

            CircleShape dynamicCircleShape = new CircleShape();
            dynamicCircleShape._radius = radius/DynamicPhysicsGameObject.ScreenPixelsPerMeter;
            //dynamicBoxShape.
            //dynamicBoxShape.SetAsBox(box_width / 2.0f, box_height / 2.0f);   //experiment with / 2.0f
            FixtureDef dynamicCircleFixtureDef = new FixtureDef();
            dynamicCircleFixtureDef.shape = dynamicCircleShape;
            dynamicCircleFixtureDef.density = density;
            dynamicCircleFixtureDef.friction = 0.3f;

            dynamicBody.CreateFixture(dynamicCircleFixtureDef);

            this.physicsBody = dynamicBody;
        }
        /// <summary>
        /// Implement Shape.
        /// </summary>
        /// <returns>Shape</returns>
        public override Shape Clone()
        {
            CircleShape shape = new CircleShape();
            shape.ShapeType = ShapeType;
            shape._radius = _radius;
            shape._p = _p;

            return shape;
        }
Example #8
0
        public void addCharacterSprite(Sprite sprite)
        {
            CircleShape cs = new CircleShape();
            cs._radius = sprite.Texture2D.Width / PTM;

            FixtureDef fd = new FixtureDef();
            fd.shape = cs;
            fd.restitution = 0.5f;
            fd.friction = 0.5f;
            fd.density = 1.5f;

            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = new Vector2(sprite.X / PTM, sprite.Y / PTM);
            bd.userData = sprite;

            Body body = world.CreateBody(bd);
            body.CreateFixture(fd);
        }
Example #9
0
    public CharacterCollision()
    {
        // Ground body
        {
            BodyDef bd = new BodyDef();
            Body ground = _world.CreateBody(bd);

            PolygonShape shape = new PolygonShape();
            shape.SetAsEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
            ground.CreateFixture(shape, 0.0f);
        }

        // Collinear edges
        {
            BodyDef bd = new BodyDef();
            Body ground = _world.CreateBody(bd);

            PolygonShape shape = new PolygonShape();
            shape.SetAsEdge(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f));
            ground.CreateFixture(shape, 0.0f);
            shape.SetAsEdge(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f));
            ground.CreateFixture(shape, 0.0f);
            shape.SetAsEdge(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f));
            ground.CreateFixture(shape, 0.0f);
        }

        // Square tiles
        {
            BodyDef bd = new BodyDef();
            Body ground = _world.CreateBody(bd);

            PolygonShape shape = new PolygonShape();
            shape.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f);
            ground.CreateFixture(shape, 0.0f);
            shape.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f);
            ground.CreateFixture(shape, 0.0f);
            shape.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f);
            ground.CreateFixture(shape, 0.0f);
        }

        // Square made from edges notice how the edges are shrunk to account
        // for the polygon radius. This makes it so the square character does
        // not get snagged. However, ray casts can now go through the cracks.
        for (int i = 0; i < 4; i++)
        {
            BodyDef bd = new BodyDef();
            Body ground = _world.CreateBody(bd);
            ground.SetTransform(new Vector2(-2f * i, 0), 0);

            Vector2[] vs = new Vector2[4];
            vs[0] = new Vector2(-1.0f, 3.0f);
            vs[1] = new Vector2(1.0f, 3.0f);
            vs[2] = new Vector2(1.0f, 5.0f);
            vs[3] = new Vector2(-1.0f, 5.0f);
            LoopShape shape = new LoopShape();
            shape._count = 4;
            shape._vertices = vs;
            ground.CreateFixture(shape, 0.0f);

            //PolygonShape shape = new PolygonShape();
            //float d = 2.0f * Settings.b2_polygonRadius;
            //shape.SetAsEdge(new Vector2(-1.0f + d, 3.0f), new Vector2(1.0f - d, 3.0f));
            //ground.CreateFixture(shape, 0.0f);
            //shape.SetAsEdge(new Vector2(1.0f, 3.0f + d), new Vector2(1.0f, 5.0f - d));
            //ground.CreateFixture(shape, 0.0f);
            //shape.SetAsEdge(new Vector2(1.0f - d, 5.0f), new Vector2(-1.0f + d, 5.0f));
            //ground.CreateFixture(shape, 0.0f);
            //shape.SetAsEdge(new Vector2(-1.0f, 5.0f - d), new Vector2(-1.0f, 3.0f + d));
            //ground.CreateFixture(shape, 0.0f);
        }

        // Square character
        {
            BodyDef bd = new BodyDef();
            bd.position = new Vector2(-3.0f, 5.0f);
            bd.type = BodyType.Dynamic;
            bd.fixedRotation = true;
            bd.allowSleep = false;

            Body body = _world.CreateBody(bd);

            PolygonShape shape = new PolygonShape();
            shape.SetAsBox(0.5f, 0.5f);

            FixtureDef fd = new FixtureDef();
            fd.shape = shape;
            fd.density = 20.0f;
            body.CreateFixture(fd);
        }

        #if false
        // Hexagon character
        {
            BodyDef bd = new BodyDef();
            bd.position = new Vector2(-5.0f, 5.0f);
            bd.type = BodyType.Dynamic;
            bd.fixedRotation = true;
            bd.allowSleep = false;

            Body body = _world.CreateBody(bd);

            float angle = 0.0f;
            float delta = (float)Math.PI / 3.0f;
            Vector2[] vertices = new Vector2[6];
            for (int i = 0; i < 6; ++i)
            {
                vertices[i] = new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle));
                angle += delta;
            }

            PolygonShape shape = new PolygonShape();
            shape.Set(vertices, 6);

            FixtureDef fd = new FixtureDef();
            fd.shape = shape;
            fd.density = 20.0f;
            body.CreateFixture(fd);
        }

        // Circle character
        {
            BodyDef bd = new BodyDef();
            bd.position = new Vector2(3.0f, 5.0f);
            bd.type = BodyType.Dynamic;
            bd.fixedRotation = true;
            bd.allowSleep = false;

            Body body = _world.CreateBody(bd);

            CircleShape shape = new CircleShape();
            shape._radius = 0.5f;

            FixtureDef fd = new FixtureDef();
            fd.shape = shape;
            fd.density = 20.0f;
            body.CreateFixture(fd);
        }
        #endif
    }
Example #10
0
 /// <summary>
 /// Creates a circle shaped part of the motocycle
 /// </summary>
 /// <param name="name">The Content name of the texture that belongs to this part</param>
 /// <param name="pos">The position of the part</param>
 /// <param name="radius">The radius of the circle shape</param>
 /// <param name="angle">The rotation angle of the part</param>
 /// <param name="density">The density of the part</param>
 /// <param name="friction">The friction of the part</param>
 /// <param name="restitution">The restitution of the part</param>
 /// <returns></returns>
 private Body CreateCirclePart(String name, Vector2 pos, float radius, float angle,
                               float density, float friction, float restitution)
 {
     CircleShape shape = new CircleShape();
     shape._radius = radius / Level.FACTOR;
     return CreatePart(shape, name, pos, angle, density, friction, restitution);
 }
Example #11
0
        // Compute contact points for edge versus circle.
        // This accounts for edge connectivity.
        public static void CollideEdgeAndCircle(ref Manifold manifold,
            EdgeShape edgeA, ref Transform xfA,
            CircleShape circleB, ref Transform xfB)
        {
            manifold._pointCount = 0;

            // Compute circle in frame of edge
            Vector2 Q = MathUtils.MultiplyT(ref xfA, MathUtils.Multiply(ref xfB, circleB._p));

            Vector2 A = edgeA._vertex1, B = edgeA._vertex2;
            Vector2 e = B - A;

            // Barycentric coordinates
            float u = Vector2.Dot(e, B - Q);
            float v = Vector2.Dot(e, Q - A);

            float radius = edgeA._radius + circleB._radius;

            ContactFeature cf;
            cf.indexB = 0;
            cf.typeB = (byte)ContactFeatureType.Vertex;

            Vector2 P, d;

            // Region A
            if (v <= 0.0f)
            {
                P = A;
                d = Q - P;
                float dd = Vector2.Dot(d, d);
                if (dd > radius * radius)
                {
                    return;
                }

                // Is there an edge connected to A?
                if (edgeA._hasVertex0)
                {
                    Vector2 A1 = edgeA._vertex0;
                    Vector2 B1 = A;
                    Vector2 e1 = B1 - A1;
                    float u1 = Vector2.Dot(e1, B1 - Q);

                    // Is the circle in Region AB of the previous edge?
                    if (u1 > 0.0f)
                    {
                        return;
                    }
                }

                cf.indexA = 0;
                cf.typeA = (byte)ContactFeatureType.Vertex;
                manifold._pointCount = 1;
                manifold._type = ManifoldType.Circles;
                manifold._localNormal = Vector2.Zero;
                manifold._localPoint = P;
                var mp = new ManifoldPoint();
                mp.Id.Key = 0;
                mp.Id.Features = cf;
                mp.LocalPoint = circleB._p;
                manifold._points[0] = mp;
                return;
            }

            // Region B
            if (u <= 0.0f)
            {
                P = B;
                d = Q - P;
                float dd = Vector2.Dot(d, d);
                if (dd > radius * radius)
                {
                    return;
                }

                // Is there an edge connected to B?
                if (edgeA._hasVertex3)
                {
                    Vector2 B2 = edgeA._vertex3;
                    Vector2 A2 = B;
                    Vector2 e2 = B2 - A2;
                    float v2 = Vector2.Dot(e2, Q - A2);

                    // Is the circle in Region AB of the next edge?
                    if (v2 > 0.0f)
                    {
                        return;
                    }
                }

                cf.indexA = 1;
                cf.typeA = (byte)ContactFeatureType.Vertex;
                manifold._pointCount = 1;
                manifold._type = ManifoldType.Circles;
                manifold._localNormal = Vector2.Zero;
                manifold._localPoint = P;
                var mp = new ManifoldPoint();
                mp.Id.Key = 0;
                mp.Id.Features = cf;
                mp.LocalPoint = circleB._p;
                manifold._points[0] = mp;
                return;
            }

            // Region AB
            float den = Vector2.Dot(e, e);
            Debug.Assert(den > 0.0f);
            P = (1.0f / den) * (u * A + v * B);
            d = Q - P;
            float dd2 = Vector2.Dot(d, d);
            if (dd2 > radius * radius)
            {
                return;
            }

            Vector2 n = new Vector2(-e.Y, e.X);
            if (Vector2.Dot(n, Q - A) < 0.0f)
            {
                n = new Vector2(-n.X, -n.Y);
            }
            n.Normalize();

            cf.indexA = 0;
            cf.typeA = (byte)ContactFeatureType.Face;
            manifold._pointCount = 1;
            manifold._type = ManifoldType.FaceA;
            manifold._localNormal = n;
            manifold._localPoint = A;
            var mp2 = new ManifoldPoint();
            mp2.Id.Key = 0;
            mp2.Id.Features = cf;
            mp2.LocalPoint = circleB._p;
            manifold._points[0] = mp2;
        }
Example #12
0
        protected void AddPoly(Body body2Body, V2DShape polygon)
        {
            Shape shape;
            if (polygon.IsCircle)
            {
                CircleShape circDef = new CircleShape();
                circDef._radius = polygon.Radius / (V2DScreen.WorldScale * State.Scale.X);
                Vector2 lp = new Vector2(polygon.CenterX / V2DScreen.WorldScale, polygon.CenterY / V2DScreen.WorldScale);
                circDef._p = lp;
                shape = circDef;
            }
            else
            {
                float[] pts = polygon.Data;
                PolygonShape polyDef = new PolygonShape();
                shape = polyDef;
                int len= (int)(pts.Length / 2);
                Vector2[] v2s = new Vector2[len];

                for (int i = 0; i < len; i++)
                {
                    float px = pts[i * 2];
                    float py = pts[i * 2 + 1];

                    v2s[i] = new Vector2(
                        px / V2DScreen.WorldScale * State.Scale.X,
                        py / V2DScreen.WorldScale * State.Scale.Y);
                }
                polyDef.Set(v2s, len);
            }

            FixtureDef fd = new FixtureDef();
            fd.shape = shape;

            if (instanceName.IndexOf("s_") == 0)
            {
                isStatic = true;
                fd.density = 0.0f;
            }
            else
            {
                fd.density = density;
            }
            fd.friction = friction;
            fd.restitution = restitution;

            if (groupIndex != 0)
            {
                fd.filter.groupIndex = groupIndex;
            }

            if (attributeProperties != null)
            {
                attributeProperties.ApplyAttribtues(fd);
            }

            body.CreateFixture(fd);
        }
Example #13
0
        /// <summary>
        /// Compute the collision manifold between a polygon and a circle.
        /// </summary>
        /// <param name="manifold"></param>
        /// <param name="polygonA"></param>
        /// <param name="xfA"></param>
        /// <param name="circleB"></param>
        /// <param name="xfB"></param>
        public static void CollidePolygonAndCircle(ref Manifold manifold,
                                                   PolygonShape polygonA, ref Transform xfA,
                                                   CircleShape circleB, ref Transform xfB)
        {
            manifold._pointCount = 0;

            // Compute circle position in the frame of the polygon.
            Vector2 c      = MathUtils.Multiply(ref xfB, circleB._p);
            Vector2 cLocal = MathUtils.MultiplyT(ref xfA, c);

            // Find the min separating edge.
            int   normalIndex = 0;
            float separation  = -Settings.b2_maxFloat;
            float radius      = polygonA._radius + circleB._radius;
            int   vertexCount = polygonA._vertexCount;

            for (int i = 0; i < vertexCount; ++i)
            {
                float s = Vector2.Dot(polygonA._normals[i], cLocal - polygonA._vertices[i]);

                if (s > radius)
                {
                    // Early out.
                    return;
                }

                if (s > separation)
                {
                    separation  = s;
                    normalIndex = i;
                }
            }

            // Vertices that subtend the incident face.
            int     vertIndex1 = normalIndex;
            int     vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0;
            Vector2 v1         = polygonA._vertices[vertIndex1];
            Vector2 v2         = polygonA._vertices[vertIndex2];

            // If the center is inside the polygon ...
            if (separation < Settings.b2_epsilon)
            {
                manifold._pointCount  = 1;
                manifold._type        = ManifoldType.FaceA;
                manifold._localNormal = polygonA._normals[normalIndex];
                manifold._localPoint  = 0.5f * (v1 + v2);

                var p0 = manifold._points[0];

                p0.LocalPoint = circleB._p;
                p0.Id.Key     = 0;

                manifold._points[0] = p0;

                return;
            }

            // Compute barycentric coordinates
            float u1 = Vector2.Dot(cLocal - v1, v2 - v1);
            float u2 = Vector2.Dot(cLocal - v2, v1 - v2);

            if (u1 <= 0.0f)
            {
                if (Vector2.DistanceSquared(cLocal, v1) > radius * radius)
                {
                    return;
                }

                manifold._pointCount  = 1;
                manifold._type        = ManifoldType.FaceA;
                manifold._localNormal = cLocal - v1;
                manifold._localNormal.Normalize();
                manifold._localPoint = v1;

                var p0b = manifold._points[0];

                p0b.LocalPoint = circleB._p;
                p0b.Id.Key     = 0;

                manifold._points[0] = p0b;
            }
            else if (u2 <= 0.0f)
            {
                if (Vector2.DistanceSquared(cLocal, v2) > radius * radius)
                {
                    return;
                }

                manifold._pointCount  = 1;
                manifold._type        = ManifoldType.FaceA;
                manifold._localNormal = cLocal - v2;
                manifold._localNormal.Normalize();
                manifold._localPoint = v2;

                var p0c = manifold._points[0];

                p0c.LocalPoint = circleB._p;
                p0c.Id.Key     = 0;

                manifold._points[0] = p0c;
            }
            else
            {
                Vector2 faceCenter  = 0.5f * (v1 + v2);
                float   separation2 = Vector2.Dot(cLocal - faceCenter, polygonA._normals[vertIndex1]);
                if (separation2 > radius)
                {
                    return;
                }

                manifold._pointCount  = 1;
                manifold._type        = ManifoldType.FaceA;
                manifold._localNormal = polygonA._normals[vertIndex1];
                manifold._localPoint  = faceCenter;

                var p0d = manifold._points[0];

                p0d.LocalPoint = circleB._p;
                p0d.Id.Key     = 0;

                manifold._points[0] = p0d;
            }
        }
Example #14
0
        public override bool init()
        {
            if (!base.init())
                return false;
            if (!base.init())
            {
                return false;
            }

            CCSize winSize = CCDirector.sharedDirector().getWinSize();
            title = CCLabelTTF.labelWithString("FootBall", "Arial", 24);
            title.position = new CCPoint(winSize.width / 2, winSize.height - 50);
            this.addChild(title, 1);
            ball = CCSprite.spriteWithFile(@"images/ball");
            ball.position = new CCPoint(100, 300);
            this.addChild(ball);
            Vector2 gravity = new Vector2(0.0f, -30.0f);
            bool doSleep = true;
            world = new World(gravity, doSleep);
            /////////////////////////
            BodyDef groundBodyDef = new BodyDef();
            groundBodyDef.position = new Vector2(0, 0);
            Body groundBody = world.CreateBody(groundBodyDef);
            PolygonShape groundBox = new PolygonShape();
            FixtureDef boxShapeDef = new FixtureDef();
            boxShapeDef.shape = groundBox;

            groundBox.SetAsEdge(new Vector2(0, 0), new Vector2((float)(winSize.width / PTM_RATIO), 0));
            groundBody.CreateFixture(boxShapeDef);
            groundBox.SetAsEdge(new Vector2(0, 0), new Vector2(0, (float)(winSize.height / PTM_RATIO)));
            groundBody.CreateFixture(boxShapeDef);
            groundBox.SetAsEdge(new Vector2(0, (float)(winSize.height / PTM_RATIO)),
                new Vector2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO)));
            groundBody.CreateFixture(boxShapeDef);
            groundBox.SetAsEdge(new Vector2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO)),
                new Vector2((float)(winSize.width / PTM_RATIO), 0));
            groundBody.CreateFixture(boxShapeDef);

            BodyDef ballBodyDef = new BodyDef();
            ballBodyDef.type = BodyType.Dynamic;
            ballBodyDef.position = new Vector2(
                (float)(100 / PTM_RATIO),
                (float)(300 / PTM_RATIO));
            ballBodyDef.userData = ball;
            body = world.CreateBody(ballBodyDef);

            CircleShape circle = new CircleShape();
            circle._radius = (float)(26.0 / PTM_RATIO);

            FixtureDef ballShapeDef = new FixtureDef();
            ballShapeDef.shape = circle;
            ballShapeDef.density = 1.0f;
            ballShapeDef.friction = 0.0f;
            ballShapeDef.restitution = 1.0f;
            body.CreateFixture(ballShapeDef);

            this.schedule(tick);
            return true;
        }
        void SetFixtures()
        {
            FixtureDef fd = new FixtureDef();
            fd.density = 1; fd.friction = .3f; fd.restitution = 0f;

            PolygonShape ps = new PolygonShape();

            float area;
            fixtureCount = 0;

            List<Vector2> v = equipmentData.ContinuousEdges;
            for (int i = 0; i < v.Count - 1; i++)
            {
                area = 0;

                if (v[i + 1].X == -1 && v[i + 1].Y == -1) { i++; continue; }

                Vector2 c = (v[i] + v[i + 1] - 2 * origin) / 2 / gameContent.b2Scale;
                float h = 8f / 2 / gameContent.b2Scale;
                float w = Vector2.Distance(v[i], v[i + 1]) / 2 / gameContent.b2Scale;
                float theta = (float)Math.Atan((v[i + 1].Y - v[i].Y) / (v[i + 1].X - v[i].X));

                ps.SetAsBox(w, h, c, theta);
                fd.shape = ps;
                area = h * w * 4;

                fd.userData = area; fixtureCount += 1; body.CreateFixture(fd);
            }

            List<Box> b = equipmentData.Boxes;
            for (int i = 0; i < b.Count; i++)
            {
                float hx = b[i].Width / 2 / gameContent.b2Scale, hy = b[i].Height / 2 / gameContent.b2Scale;
                ps.SetAsBox(hx, hy, (b[i].Position - origin) / gameContent.b2Scale,
                    b[i].RotationInDeg / 180 * (float)Math.PI);

                fd.shape = ps;
                area = hx * hy * 4;

                fd.userData = area; fixtureCount += 1; body.CreateFixture(fd);
            }

            List<Circle> circles = equipmentData.Circles;
            for (int i = 0; i < circles.Count; i++)
            {
                CircleShape cs = new CircleShape();
                cs._radius = circles[i].Diameter / 2 / gameContent.b2Scale;
                cs._p = (circles[i].Position - origin) / gameContent.b2Scale;

                fd.shape = cs;
                area = (float)Math.PI * cs._radius * cs._radius;
                fd.userData = area; fixtureCount += 1; body.CreateFixture(fd);
            }
        }
        /// <summary>
        /// Call this to initialize a Behaviour with data supplied in a file.
        /// </summary>
        /// <param name="fileName">The file to load from.</param>
        public override void LoadContent(String fileName)
        {
            base.LoadContent(fileName);

            // Load the definiton for this object.
            SimulatedPhysicsDefinition def = GameObjectManager.pInstance.pContentManager.Load<SimulatedPhysicsDefinition>(fileName);

            // Create the body representing this object in the physical world.
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(640.0f, 340.0f));
            bd.fixedRotation = true;
            bd.linearDamping = 1.0f;
            mBody = PhysicsManager.pInstance.pWorld.CreateBody(bd);

            /*
            // A bunch of code to create a capsule.  Not using it because it has all the same problems as
            // a box when it comes to catching edges and such.
            var shape = new PolygonShape();
            Vector2[] v = new Vector2[8];

            Double increment = System.Math.PI * 2.0 / (v.Length - 2);
            Double theta = 0.0;

            Vector2 center = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, 8.0f));
            Single radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f);
            for (Int32 i = 0; i < v.Length; i++)
            {
                v[i] = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));

                if (i == (v.Length / 2) - 1)
                {
                    center = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, -8.0f));
                    i++;
                    v[i] = center + radius * new Vector2((float)System.Math.Cos(theta), (float)System.Math.Sin(theta));
                }
                theta += increment;
            }

            //v = v.Reverse().ToArray();

            shape.Set(v, v.Length);

            var fd = new FixtureDef();
            fd.shape = shape;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 1.0f;
            mBody.CreateFixture(fd);
            */

            var shape = new PolygonShape();
            shape.SetAsBox(PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f), PhysicsManager.pInstance.ScreenToPhysicalWorld(8.0f));

            var fd = new FixtureDef();
            fd.shape = shape;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 1.0f;
            //mBody.CreateFixture(fd);

            var circle = new CircleShape();
            circle._radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8);
            circle._p = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, 8.0f));

            fd = new FixtureDef();
            fd.shape = circle;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 1.5f;
            mBody.CreateFixture(fd);

            circle = new CircleShape();
            circle._radius = PhysicsManager.pInstance.ScreenToPhysicalWorld(8);
            circle._p = PhysicsManager.pInstance.ScreenToPhysicalWorld(new Vector2(0.0f, -8.0f));

            fd = new FixtureDef();
            fd.shape = circle;
            fd.restitution = 0.0f;
            fd.friction = 0.5f;
            fd.density = 1.5f;
            mBody.CreateFixture(fd);
        }
Example #17
0
 public Fixture AttachCircle(Vector2 center, float radius,
     float density, float friction, float restitution)
 {
     var shape = new CircleShape();
     shape._p = World.B2Value(center);
     shape._radius = World.B2Value(radius);
     var fixtureDef = new FixtureDef();
     fixtureDef.shape = shape;
     fixtureDef.density = density;
     fixtureDef.friction = friction;
     fixtureDef.restitution = restitution;
     return _body.CreateFixture(fixtureDef);
 }
        private void CreatBody()
        {
            State = LeafState.Drop;

            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            CircleShape cShape = new CircleShape();
            cShape._radius = texture.Width / 2 - 3;
            cShape._p = new Vector2(0, cShape._radius + 1);
            FixtureDef fd = new FixtureDef();
            fd.density = 0.001f;
            fd.friction = 0.5f;
            fd.restitution = 0.0f;
            fd.shape = cShape;
            //fd.filter.groupIndex = -1;
            //fd.filter.categoryBits = 2;
            //fd.filter.maskBits = 4;

            body = world.CreateBody(bd);
            body.CreateFixture(fd);

            scale = 0;
        }
        private void CreateFixture(Vector2 localPosition)
        {
            CircleShape cShape = new CircleShape();

            cShape._radius = .5f;
            cShape._p = localPosition;

            FixtureDef fd = new FixtureDef();
            fd.shape = cShape;
            fd.density = 10.1f;
            //fd.filter.groupIndex = -1;
            //fd.filter.categoryBits = 4;
            //fd.filter.maskBits = 2;

            body.CreateFixture(fd);
        }
Example #20
0
    public EdgeTest()
    {
        {
            BodyDef bd = new BodyDef();
            Body ground = _world.CreateBody(bd);

            Vector2 v1 = new Vector2(-10.0f, 0.0f);
            Vector2 v2 = new Vector2(-7.0f, -1.0f);
            Vector2 v3 = new Vector2(-4.0f, 0.0f);
            Vector2 v4 = new Vector2(0.0f, 0.0f);
            Vector2 v5 = new Vector2(4.0f, 0.0f);
            Vector2 v6 = new Vector2(7.0f, 1.0f);
            Vector2 v7 = new Vector2(10.0f, 0.0f);

            EdgeShape shape = new EdgeShape();

            shape.Set(v1, v2);
            //shape._index1 = 0;
            //shape._index2 = 1;
            shape._hasVertex3 = true;
            shape._vertex3 = v3;
            ground.CreateFixture(shape, 0.0f);

            shape.Set(v2, v3);
            //shape._index1 = 1;
            //shape._index2 = 2;
            shape._hasVertex0 = true;
            shape._hasVertex3 = true;
            shape._vertex0 = v1;
            shape._vertex3 = v4;
            ground.CreateFixture(shape, 0.0f);

            shape.Set(v3, v4);
            //shape._index1 = 2;
            //shape._index2 = 3;
            shape._hasVertex0 = true;
            shape._hasVertex3 = true;
            shape._vertex0 = v2;
            shape._vertex3 = v5;
            ground.CreateFixture(shape, 0.0f);

            shape.Set(v4, v5);
            //shape._index1 = 3;
            //shape._index2 = 4;
            shape._hasVertex0 = true;
            shape._hasVertex3 = true;
            shape._vertex0 = v3;
            shape._vertex3 = v6;
            ground.CreateFixture(shape, 0.0f);

            shape.Set(v5, v6);
            //shape._index1 = 4;
            //shape._index2 = 5;
            shape._hasVertex0 = true;
            shape._hasVertex3 = true;
            shape._vertex0 = v4;
            shape._vertex3 = v7;
            ground.CreateFixture(shape, 0.0f);

            shape.Set(v6, v7);
            //shape._index1 = 5;
            //shape._index2 = 6;
            shape._hasVertex0 = true;
            shape._vertex0 = v5;
            ground.CreateFixture(shape, 0.0f);
        }

        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = new Vector2(-0.5f, 0.5f);
            bd.allowSleep = false;
            Body body = _world.CreateBody(bd);

            CircleShape shape = new CircleShape();
            shape._radius = 0.5f;

            body.CreateFixture(shape, 1.0f);
        }

        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = new Vector2(0.5f, 0.5f);
            bd.allowSleep = false;
            Body body = _world.CreateBody(bd);

            PolygonShape shape = new PolygonShape();
            shape.SetAsBox(0.5f, 0.5f);

            body.CreateFixture(shape, 1.0f);
        }
    }
        /// <summary>
        /// Compute the collision manifold between two circles.
        /// </summary>
        /// <param name="manifold"></param>
        /// <param name="circleA"></param>
        /// <param name="xfA"></param>
        /// <param name="circleB"></param>
        /// <param name="xfB"></param>
        public static void CollideCircles(ref Manifold manifold,
            CircleShape circleA, ref Transform xfA,
            CircleShape circleB, ref Transform xfB)
        {
            manifold._pointCount = 0;

            Vector2 pA = MathUtils.Multiply(ref xfA, circleA._p);
            Vector2 pB = MathUtils.Multiply(ref xfB, circleB._p);

            Vector2 d = pB - pA;
            float distSqr = Vector2.Dot(d, d);
            float rA = circleA._radius;
            float rB = circleB._radius;
            float radius = rA + rB;
            if (distSqr > radius * radius)
            {
                return;
            }

            manifold._type = ManifoldType.Circles;
            manifold._localPoint = circleA._p;
            manifold._localNormal = Vector2.Zero;
            manifold._pointCount = 1;

            var p0 = manifold._points[0];

            p0.LocalPoint = circleB._p;
            p0.Id.Key = 0;

            manifold._points[0] = p0;
        }
        public Atom(Symbol symbol, Vector2 position, GameContent gameContent, World world)
        {
            this.gameContent = gameContent;
            this.world = world;

            if (symbol == Symbol.Ra) eye = EyeState.Angry;

            this.symbol = symbol;
            this.symbolStr = symbol.ToString();
            symbolCenter = gameContent.symbolFont.MeasureString(this.symbolStr);
            symbolCenter.X *= 0.5f;
            symbolCenter.Y *= 0.92f;

            bondsLeft = (int)symbol;

            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            bd.position = this.position = position / gameContent.b2Scale;
            bd.bullet = true;
            body = world.CreateBody(bd);
            body.SetUserData(this);

            CircleShape cs = new CircleShape();
            cs._radius = gameContent.atomRadius / gameContent.b2Scale;

            FixtureDef fd = new FixtureDef();
            fd.shape = cs;
            fd.restitution = 0.2f;
            fd.friction = 0.5f;

            fixture = body.CreateFixture(fd);

            electroShockAnimation = new Animation(gameContent.electroShock, 3, 0.1f, true, new Vector2(0.5f, 0.5f));

            radiationSmoke = new RadiationSmoke(gameContent, this);

            // Collide only with Ground but not with itself and bonded Filter
            mouseFilter = new Filter();
            mouseFilter.categoryBits = 0x0002; mouseFilter.maskBits = 0x0001; mouseFilter.groupIndex = -2;

            // Collide with every thing
            atomFilter = new Filter();
            atomFilter.categoryBits = 0x0001; atomFilter.maskBits = 0x0001; atomFilter.groupIndex = 1;

            fixture.SetFilterData(ref atomFilter);

            SetMode(false, false);
        }
Example #23
0
        public override void Initialize(ConfigSection section)
        {
            base.Initialize(section);

            textureName = section["texture"];
            circlePos = section["position"].AsVector2();
            Radius = section["radius"];
            MaxTorque = section["torque"];

            var bodyDef = new BodyDef();
            bodyDef.type = BodyType.Dynamic;

            bodyDef.position = Game.level.ConvertToBox2D(circlePos);
            bodyDef.inertiaScale = section["inertiaScale"];
            bodyDef.linearDamping = section["linearDamping"];
            bodyDef.angularDamping = section["angularDamping"];

            bodyDef.userData = this;

            Body = Game.level.World.CreateBody(bodyDef);

            var shape = new CircleShape();
            shape._radius = Game.level.ConvertToBox2D(Radius);

            var fixture = new FixtureDef();
            fixture.restitution = section["restitution"];
            fixture.density = section["density"];
            fixture.shape = shape;
            fixture.friction = section["friction"];
            Body.CreateFixture(fixture);
        }
        /// <summary>
        /// Compute the collision manifold between a polygon and a circle.
        /// </summary>
        /// <param name="manifold"></param>
        /// <param name="polygonA"></param>
        /// <param name="xfA"></param>
        /// <param name="circleB"></param>
        /// <param name="xfB"></param>
        public static void CollidePolygonAndCircle(ref Manifold manifold,
            PolygonShape polygonA, ref Transform xfA,
            CircleShape circleB, ref Transform xfB)
        {
            manifold._pointCount = 0;

            // Compute circle position in the frame of the polygon.
            Vector2 c = MathUtils.Multiply(ref xfB, circleB._p);
            Vector2 cLocal = MathUtils.MultiplyT(ref xfA, c);

            // Find the min separating edge.
            int normalIndex = 0;
            float separation = -Settings.b2_maxFloat;
            float radius = polygonA._radius + circleB._radius;
            int vertexCount = polygonA._vertexCount;

            for (int i = 0; i < vertexCount; ++i)
            {
                float s = Vector2.Dot(polygonA._normals[i], cLocal - polygonA._vertices[i]);

                if (s > radius)
                {
                    // Early out.
                    return;
                }

                if (s > separation)
                {
                    separation = s;
                    normalIndex = i;
                }
            }

            // Vertices that subtend the incident face.
            int vertIndex1 = normalIndex;
            int vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0;
            Vector2 v1 = polygonA._vertices[vertIndex1];
            Vector2 v2 = polygonA._vertices[vertIndex2];

            // If the center is inside the polygon ...
            if (separation < Settings.b2_epsilon)
            {
                manifold._pointCount = 1;
                manifold._type = ManifoldType.FaceA;
                manifold._localNormal = polygonA._normals[normalIndex];
                manifold._localPoint = 0.5f * (v1 + v2);

                var p0 = manifold._points[0];

                p0.LocalPoint = circleB._p;
                p0.Id.Key = 0;

                manifold._points[0] = p0;

                return;
            }

            // Compute barycentric coordinates
            float u1 = Vector2.Dot(cLocal - v1, v2 - v1);
            float u2 = Vector2.Dot(cLocal - v2, v1 - v2);
            if (u1 <= 0.0f)
            {
                if (Vector2.DistanceSquared(cLocal, v1) > radius * radius)
                {
                    return;
                }

                manifold._pointCount = 1;
                manifold._type = ManifoldType.FaceA;
                manifold._localNormal = cLocal - v1;
                manifold._localNormal.Normalize();
                manifold._localPoint = v1;

                var p0b = manifold._points[0];

                p0b.LocalPoint = circleB._p;
                p0b.Id.Key = 0;

                manifold._points[0] = p0b;

            }
            else if (u2 <= 0.0f)
            {
                if (Vector2.DistanceSquared(cLocal, v2) > radius * radius)
                {
                    return;
                }

                manifold._pointCount = 1;
                manifold._type = ManifoldType.FaceA;
                manifold._localNormal = cLocal - v2;
                manifold._localNormal.Normalize();
                manifold._localPoint = v2;

                var p0c = manifold._points[0];

                p0c.LocalPoint = circleB._p;
                p0c.Id.Key = 0;

                manifold._points[0] = p0c;
            }
            else
            {
                Vector2 faceCenter = 0.5f * (v1 + v2);
                float separation2 = Vector2.Dot(cLocal - faceCenter, polygonA._normals[vertIndex1]);
                if (separation2 > radius)
                {
                    return;
                }

                manifold._pointCount = 1;
                manifold._type = ManifoldType.FaceA;
                manifold._localNormal = polygonA._normals[vertIndex1];
                manifold._localPoint = faceCenter;

                var p0d = manifold._points[0];

                p0d.LocalPoint = circleB._p;
                p0d.Id.Key = 0;

                manifold._points[0] = p0d;
            }
        }
        private void LoadAsteroid(Vector2 positon)
        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Dynamic;
            //bd.angularVelocity = -2.0f;
            bd.position = positon;

            Body asteroid = world.CreateBody(bd);

            CircleShape pShape = new CircleShape();
            pShape._radius = GameContent.asteroid[0].Width / 2;

            FixtureDef fd = new FixtureDef();
            fd.shape = pShape;
            fd.density = 0.05f;
            fd.friction = 0.3f;

            asteroid.CreateFixture(fd);

            DistanceJointDef jd = new DistanceJointDef();
            jd.bodyA = ground;
            jd.bodyB = asteroid;
            jd.localAnchorA = asteroid.Position;
            jd.localAnchorB = Vector2.Zero;
            jd.frequencyHz = 0.18f;
            jd.dampingRatio = 0.95f;
            jd.length = 0;
            jd.collideConnected = false;

            world.CreateJoint(jd);

            asteroids.Add(new Asteroid(GameContent, random.Next(16), asteroid));
        }