Exemplo n.º 1
0
        public Spider(World world, PhysicsGameScreen screen, Vector2 position)
        {
            //Load bodies
            _circle = BodyFactory.CreateCircle(world, SpiderBodyRadius, 0.1f, position);
            _circle.BodyType = BodyType.Dynamic;

            //Left upper leg
            _leftUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f,
                                                    _circle.Position - new Vector2(SpiderBodyRadius, 0f) -
                                                    new Vector2(_upperLegSize.X / 2f, 0f));
            _leftUpper.BodyType = BodyType.Dynamic;

            //Left lower leg
            _leftLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f,
                                                    _circle.Position - new Vector2(SpiderBodyRadius, 0f) -
                                                    new Vector2(_upperLegSize.X, 0f) -
                                                    new Vector2(_lowerLegSize.X / 2f, 0f));
            _leftLower.BodyType = BodyType.Dynamic;

            //Right upper leg
            _rightUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f,
                                                     _circle.Position + new Vector2(SpiderBodyRadius, 0f) +
                                                     new Vector2(_upperLegSize.X / 2f, 0f));
            _rightUpper.BodyType = BodyType.Dynamic;

            //Right lower leg
            _rightLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f,
                                                     _circle.Position + new Vector2(SpiderBodyRadius, 0f) +
                                                     new Vector2(_upperLegSize.X, 0f) +
                                                     new Vector2(_lowerLegSize.X / 2f, 0f));
            _rightLower.BodyType = BodyType.Dynamic;

            //Create joints
            JointFactory.CreateRevoluteJoint(world, _circle, _leftUpper, new Vector2(_upperLegSize.X / 2f, 0f));
            _leftShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _leftUpper);
            _leftShoulderAngleJoint.MaxImpulse = 3f;

            JointFactory.CreateRevoluteJoint(world, _circle, _rightUpper, new Vector2(-_upperLegSize.X / 2f, 0f));
            _rightShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _rightUpper);
            _rightShoulderAngleJoint.MaxImpulse = 3f;

            JointFactory.CreateRevoluteJoint(world, _leftUpper, _leftLower, new Vector2(_lowerLegSize.X / 2f, 0f));
            _leftKneeAngleJoint = JointFactory.CreateAngleJoint(world, _leftUpper, _leftLower);
            _leftKneeAngleJoint.MaxImpulse = 3f;

            JointFactory.CreateRevoluteJoint(world, _rightUpper, _rightLower, new Vector2(-_lowerLegSize.X / 2f, 0f));
            _rightKneeAngleJoint = JointFactory.CreateAngleJoint(world, _rightUpper, _rightLower);
            _rightKneeAngleJoint.MaxImpulse = 3;

            _screen = screen;

            //GFX
            AssetCreator creator = _screen.ScreenManager.Assets;
            _torso = new Sprite(creator.CircleTexture(SpiderBodyRadius, MaterialType.Waves, Color.Gray, 1f));
            _upperLeg = new Sprite(creator.TextureFromShape(_leftUpper.FixtureList[0].Shape,
                                                             MaterialType.Blank, Color.DimGray, 1f));
            _lowerLeg = new Sprite(creator.TextureFromShape(_leftLower.FixtureList[0].Shape,
                                                             MaterialType.Blank, Color.DarkSlateGray, 1f));
        }
Exemplo n.º 2
0
        public Ragdoll(World world, PhysicsGameScreen screen, Vector2 position)
        {
            CreateBody(world, position);
            CreateJoints(world);

            _screen = screen;
            CreateGFX();
        }
Exemplo n.º 3
0
        public Agent(World world, PhysicsGameScreen screen, Vector2 position)
        {
            _collidesWith = Category.All;
            _collisionCategories = Category.All;

            _agentBody = BodyFactory.CreateBody(world, position);
            _agentBody.BodyType = BodyType.Dynamic;

            //Center
            FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody);

            //Left arm
            FixtureFactory.AttachRectangle(1.5f, 0.4f, 1f, new Vector2(-1f, 0f), _agentBody);
            FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(-2f, 0f));

            //Right arm
            FixtureFactory.AttachRectangle(1.5f, 0.4f, 1f, new Vector2(1f, 0f), _agentBody);
            FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(2f, 0f));

            //Top arm
            FixtureFactory.AttachRectangle(0.4f, 1.5f, 1f, new Vector2(0f, 1f), _agentBody);
            FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(0f, 2f));

            //Bottom arm
            FixtureFactory.AttachRectangle(0.4f, 1.5f, 1f, new Vector2(0f, -1f), _agentBody);
            FixtureFactory.AttachCircle(0.5f, 0.5f, _agentBody, new Vector2(0f, -2f));

            _screen = screen;

            //GFX
            AssetCreator creator = _screen.ScreenManager.Assets;
            _box = new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(1.75f, 0.2f),
                                                           MaterialType.Blank, Color.White, 1f));
            _knob = new Sprite(creator.CircleTexture(0.5f, MaterialType.Blank, Color.Orange, 1f));
            _offset = ConvertUnits.ToDisplayUnits(2f);
        }
Exemplo n.º 4
0
        public TheoJansenWalker(World world, PhysicsGameScreen screen, Vector2 position)
        {
            _position = position;
            _motorSpeed = 2.0f;
            _motorOn = true;
            _screen = screen;

            _walkerJoints = new List<DistanceJoint>();

            _leftShoulders = new Body[3];
            _rightShoulders = new Body[3];
            _leftLegs = new Body[3];
            _rightLegs = new Body[3];

            Vector2 pivot = new Vector2(0f, -0.8f);

            // Chassis
            {
                PolygonShape shape = new PolygonShape(1f);
                shape.Vertices = PolygonTools.CreateRectangle(2.5f, 1.0f);

                _body = new Sprite(_screen.ScreenManager.Assets.TextureFromShape(shape, MaterialType.Blank, Color.Beige, 1f));

                _chassis = BodyFactory.CreateBody(world);
                _chassis.BodyType = BodyType.Dynamic;
                _chassis.Position = pivot + _position;

                Fixture fixture = _chassis.CreateFixture(shape);
                fixture.CollisionGroup = -1;
            }

            {
                CircleShape shape = new CircleShape(1.6f, 1f);
                _engine = new Sprite(_screen.ScreenManager.Assets.TextureFromShape(shape, MaterialType.Waves, Color.Beige * 0.8f, 1f));

                _wheel = BodyFactory.CreateBody(world);
                _wheel.BodyType = BodyType.Dynamic;
                _wheel.Position = pivot + _position;

                Fixture fixture = _wheel.CreateFixture(shape);
                fixture.CollisionGroup = -1;
            }

            {
                _motorJoint = new RevoluteJoint(_wheel, _chassis.Position, _chassis, Vector2.Zero);
                _motorJoint.CollideConnected = false;
                _motorJoint.MotorSpeed = _motorSpeed;
                _motorJoint.MaxMotorTorque = 400f;
                _motorJoint.MotorEnabled = _motorOn;
                world.AddJoint(_motorJoint);
            }

            Vector2 wheelAnchor = pivot + new Vector2(0f, 0.8f);

            CreateLegTextures();

            CreateLeg(world, -1f, wheelAnchor, 0);
            CreateLeg(world, 1f, wheelAnchor, 0);

            _leftLeg.Origin = AssetCreator.CalculateOrigin(_leftLegs[0]);
            _leftShoulder.Origin = AssetCreator.CalculateOrigin(_leftShoulders[0]);
            _rightLeg.Origin = AssetCreator.CalculateOrigin(_rightLegs[0]);
            _rightShoulder.Origin = AssetCreator.CalculateOrigin(_rightShoulders[0]);

            _wheel.SetTransform(_wheel.Position, 120f * Settings.Pi / 180f);
            CreateLeg(world, -1f, wheelAnchor, 1);
            CreateLeg(world, 1f, wheelAnchor, 1);

            _wheel.SetTransform(_wheel.Position, -120f * Settings.Pi / 180f);
            CreateLeg(world, -1f, wheelAnchor, 2);
            CreateLeg(world, 1f, wheelAnchor, 2);
        }
Exemplo n.º 5
0
        public Objects(World world, PhysicsGameScreen screen, Vector2 startPosition, Vector2 endPosition, int count,
            float radius, ObjectType type)
        {
            _bodies = new List<Body>(count);
            CollidesWith = Category.All;
            CollisionCategories = Category.All;

            for (int i = 0; i < count; ++i)
            {
                switch (type)
                {
                    case ObjectType.Circle:
                        _bodies.Add(BodyFactory.CreateCircle(world, radius, 1f));
                        break;
                    case ObjectType.Rectangle:
                        _bodies.Add(BodyFactory.CreateRectangle(world, radius, radius, 1f));
                        break;
                    case ObjectType.Star:
                        _bodies.Add(BodyFactory.CreateGear(world, radius, 10, 0f, 1f, 1f));
                        break;
                    case ObjectType.Gear:
                        _bodies.Add(BodyFactory.CreateGear(world, radius, 10, 100f, 1f, 1f));
                        break;
                }
            }

            for (int i = 0; i < _bodies.Count; ++i)
            {
                Body body = _bodies[i];
                body.BodyType = BodyType.Dynamic;
                body.Position = Vector2.Lerp(startPosition, endPosition, i / (float)(count - 1));
                body.Restitution = .7f;
                body.Friction = .2f;
                body.CollisionCategories = CollisionCategories;
                body.CollidesWith = CollidesWith;
            }

            _screen = screen;

            //GFX
            AssetCreator creator = _screen.ScreenManager.Assets;
            switch (type)
            {
                case ObjectType.Circle:
                    _object = new Sprite(creator.CircleTexture(radius, MaterialType.Dots, Color.DarkRed, 0.8f));
                    break;
                case ObjectType.Rectangle:
                    _object =
                        new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(radius / 2f, radius / 2f),
                                                                MaterialType.Dots, Color.Blue, 0.8f));
                    break;
                case ObjectType.Star:
                    _object = new Sprite(creator.TextureFromVertices(PolygonTools.CreateGear(radius, 10, 0f, 1f),
                                                                      MaterialType.Dots, Color.Yellow, 0.8f));
                    break;
                case ObjectType.Gear:
                    _object = new Sprite(creator.TextureFromVertices(PolygonTools.CreateGear(radius, 10, 100f, 1f),
                                                                      MaterialType.Dots, Color.DarkGreen, 0.8f));
                    break;
            }
        }
Exemplo n.º 6
0
        public Border(World world, PhysicsGameScreen screen, Viewport viewport)
        {
            _world = world;
            _screen = screen;

            float halfWidth = ConvertUnits.ToSimUnits(viewport.Width) / 2f - 0.75f;
            float halfHeight = ConvertUnits.ToSimUnits(viewport.Height) / 2f - 0.75f;

            Vertices borders = new Vertices(4);
            borders.Add(new Vector2(-halfWidth, halfHeight)); //Lower left
            borders.Add(new Vector2(halfWidth, halfHeight)); //Lower right
            borders.Add(new Vector2(halfWidth, -halfHeight)); //Upper right
            borders.Add(new Vector2(-halfWidth, -halfHeight)); // Upper left

            _anchor = BodyFactory.CreateLoopShape(_world, borders);
            _anchor.CollisionCategories = Category.All;
            _anchor.CollidesWith = Category.All;

            _basicEffect = new BasicEffect(_screen.ScreenManager.GraphicsDevice);
            _basicEffect.VertexColorEnabled = true;
            _basicEffect.TextureEnabled = true;
            _basicEffect.Texture = _screen.ScreenManager.Content.Load<Texture2D>("Materials/pavement");

            VertexPositionColorTexture[] vertice = new VertexPositionColorTexture[8];
            vertice[0] = new VertexPositionColorTexture(new Vector3(-halfWidth, -halfHeight, 0f),
                                                        Color.LightGray, new Vector2(-halfWidth, -halfHeight) / 5.25f);
            vertice[1] = new VertexPositionColorTexture(new Vector3(halfWidth, -halfHeight, 0f),
                                                        Color.LightGray, new Vector2(halfWidth, -halfHeight) / 5.25f);
            vertice[2] = new VertexPositionColorTexture(new Vector3(halfWidth, halfHeight, 0f),
                                                        Color.LightGray, new Vector2(halfWidth, halfHeight) / 5.25f);
            vertice[3] = new VertexPositionColorTexture(new Vector3(-halfWidth, halfHeight, 0f),
                                                        Color.LightGray, new Vector2(-halfWidth, halfHeight) / 5.25f);
            vertice[4] = new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, -halfHeight - 2f, 0f),
                                                        Color.LightGray,
                                                        new Vector2(-halfWidth - 2f, -halfHeight - 2f) / 5.25f);
            vertice[5] = new VertexPositionColorTexture(new Vector3(halfWidth + 2f, -halfHeight - 2f, 0f),
                                                        Color.LightGray,
                                                        new Vector2(halfWidth + 2f, -halfHeight - 2f) / 5.25f);
            vertice[6] = new VertexPositionColorTexture(new Vector3(halfWidth + 2f, halfHeight + 2f, 0f),
                                                        Color.LightGray,
                                                        new Vector2(halfWidth + 2f, halfHeight + 2f) / 5.25f);
            vertice[7] = new VertexPositionColorTexture(new Vector3(-halfWidth - 2f, halfHeight + 2f, 0f),
                                                        Color.LightGray,
                                                        new Vector2(-halfWidth - 2f, halfHeight + 2f) / 5.25f);

            _borderVerts = new VertexPositionColorTexture[24];
            _borderVerts[0] = vertice[0];
            _borderVerts[1] = vertice[5];
            _borderVerts[2] = vertice[4];
            _borderVerts[3] = vertice[0];
            _borderVerts[4] = vertice[1];
            _borderVerts[5] = vertice[5];
            _borderVerts[6] = vertice[1];
            _borderVerts[7] = vertice[6];
            _borderVerts[8] = vertice[5];
            _borderVerts[9] = vertice[1];
            _borderVerts[10] = vertice[2];
            _borderVerts[11] = vertice[6];
            _borderVerts[12] = vertice[2];
            _borderVerts[13] = vertice[7];
            _borderVerts[14] = vertice[6];
            _borderVerts[15] = vertice[2];
            _borderVerts[16] = vertice[3];
            _borderVerts[17] = vertice[7];
            _borderVerts[18] = vertice[3];
            _borderVerts[19] = vertice[4];
            _borderVerts[20] = vertice[7];
            _borderVerts[21] = vertice[3];
            _borderVerts[22] = vertice[0];
            _borderVerts[23] = vertice[4];
        }