Esempio n. 1
0
        /// <summary>
        /// Creates a new motorcycle and a driver into the given Box2D world.
        /// Creates all the parts of the motorcycle and driver and joints them together.
        /// </summary>
        /// <param name="pBikeSpeed">A pointer to the variable that describes the speed of the 
        ///                          motorcycle</param>
        /// <param name="pRotationData">RotationData to provide the information of the rotation
        ///                             of the device</param>
        /// <param name="pWorld">The Box2D world where the bike is created into</param>
        /// <param name="pCamPos">A pointer to the variable that describes the position of the
        ///                       camera</param>
        /// <param name="pContent">The used ContentManager instance</param>
        /// <param name="pSpriteBatch">The used SpriteBatch instance</param>
        public Bike(float []pBikeSpeed, RotationData pRotationData, World pWorld, float[] pCamPos,
                    ContentManager pContent)
        {
            OffTheBike = false;
            camPos = pCamPos;
            world = pWorld;
            content = pContent;
            RotationData = pRotationData;

            bikeSpeed = pBikeSpeed;

            frontWheel = CreateCirclePart("wheel", frontWheelInitPos, 35.0f, 0, 0.1f, 0.9f, 0.2f);
            frontFork = CreateBoxPart("susp_lower_long", frontForkInitPos, 20.53f, 21.33f, 0, 0.8f,
                                      1.0f, 0.2f);
            rearWheel = CreateCirclePart("rearWheel", rearWheelInitPos, 32.0f, 0, 0.4f, 1.0f,
                                         0.2f);
            rearFork = CreateBoxPart("rearFork", rearForkInitPos, 64.0f, 17.0f, rearForkInitRot,
                                     0.5f, 1.0f, 0.2f);
            bikeBody = CreateBikeBody(bikeBodyInitPos, bikeBodyInitRot, 0.5f, 1.0f, 0.2f);

            RevoluteJointDef motorDef = new RevoluteJointDef();
            motorDef.Initialize(rearWheel, rearFork, rearWheel.GetWorldCenter());
            motorDef.maxMotorTorque = 2.0f;
            motorDef.enableMotor = true;
            motor = (RevoluteJoint)world.CreateJoint(motorDef);

            RevoluteJointDef rearForkBodyDef = new RevoluteJointDef();
            Vector2 anchor = rearFork.GetWorldCenter();
            anchor.X += (32.0f / Level.FACTOR);
            anchor.Y += (13.5f / Level.FACTOR);
            rearForkBodyDef.Initialize(rearFork, bikeBody, anchor);
            rearForkBodyDef.bodyA = rearFork;
            rearForkBodyDef.bodyB = bikeBody;
            rearForkBodyDef.maxMotorTorque = 300.0f;
            world.CreateJoint(rearForkBodyDef);

            RevoluteJointDef frontWheelJointDef = new RevoluteJointDef();
            frontWheelJointDef.Initialize(frontWheel, frontFork, frontWheel.GetWorldCenter());
            frontWheelJointDef.maxMotorTorque = 300.0f;
            world.CreateJoint(frontWheelJointDef);

            DistanceJointDef frontSuspToBikeDef = new DistanceJointDef();
            frontSuspToBikeDef.Initialize(bikeBody, frontFork,
                                          frontFork.GetWorldCenter() + new Vector2(0, 0.4f),
                                          frontFork.GetWorldCenter());
            frontSuspToBikeDef.frequencyHz = 4.0f;
            frontSuspToBikeDef.dampingRatio = 0.1f;
            frontSuspToBikeDef.collideConnected = true;
            world.CreateJoint(frontSuspToBikeDef);

            DistanceJointDef rearForkDistanceDef = new DistanceJointDef();
            rearForkDistanceDef.Initialize(bikeBody, rearFork,
                                           rearFork.GetWorldCenter() + new Vector2(0, 0.4f),
                                           rearFork.GetWorldCenter());
            rearForkDistanceDef.frequencyHz = 7.0f;
            rearForkDistanceDef.dampingRatio = 0.1f;
            rearForkDistanceDef.collideConnected = true;
            world.CreateJoint(rearForkDistanceDef);

            PrismaticJointDef fSuspBikePrismaticDef = new PrismaticJointDef();
            fSuspBikePrismaticDef.Initialize(bikeBody, frontFork, bikeBody.GetWorldCenter(),
                                             new Vector2(0, 1));
            fSuspBikePrismaticDef.enableLimit = true;
            fSuspBikePrismaticDef.lowerTranslation = -0.2f;
            fSuspBikePrismaticDef.upperTranslation = 0.2f;
            fSuspBikePrismaticDef.collideConnected = true;
            world.CreateJoint(fSuspBikePrismaticDef);

            humanBody = CreateBoxPart("human", humanBodyInitPos, 17.0f, 64.0f, 0, 0.1f, 1.0f,
                                      0.2f);
            head = CreateBoxPart("head", headInitPos, 38.4f, 29.9f, headInitRot, 0.1f, 1.0f,
                                 0.2f);
            hand = CreateBoxPart("hand", handInitPos, 34.13f, 8.53f, handInitRot, 0.1f, 1.0f,
                                 0.2f);
            arm = CreateBoxPart("arm", armInitPos, 42.67f, 8.53f, armInitRot, 0.1f, 1.0f, 0.2f);

            WeldJointDef headToHumanDef = new WeldJointDef();
            headToHumanDef.Initialize(head, humanBody, head.GetWorldCenter());
            world.CreateJoint(headToHumanDef);

            RevoluteJointDef humanToBikeDef = new RevoluteJointDef();
            anchor = humanBody.GetWorldCenter();
            anchor.Y += (30.0f / Level.FACTOR);
            humanToBikeDef.Initialize(humanBody, bikeBody, anchor);
            humanToBikeDef.maxMotorTorque = 300.0f;
            humanToBike = world.CreateJoint(humanToBikeDef);

            RevoluteJointDef humanToArmDef = new RevoluteJointDef();
            anchor = arm.GetWorldPoint(new Vector2(-21.33f / Level.FACTOR, 4.26f / Level.FACTOR));
            humanToArmDef.Initialize(humanBody, arm, anchor);
            humanToArmDef.maxMotorTorque = 300.0f;
            world.CreateJoint(humanToArmDef);

            RevoluteJointDef armToHandDef = new RevoluteJointDef();
            anchor = arm.GetWorldPoint(new Vector2(21.33f / Level.FACTOR, 4.26f / Level.FACTOR));
            armToHandDef.Initialize(arm, hand, anchor);
            armToHandDef.maxMotorTorque = 300.0f;
            world.CreateJoint(armToHandDef);

            RevoluteJointDef handToBikeDef = new RevoluteJointDef();
            anchor = hand.GetWorldPoint(new Vector2(17.06f / Level.FACTOR, 4.26f / Level.FACTOR));
            handToBikeDef.Initialize(hand, bikeBody, anchor);
            handToBikeDef.maxMotorTorque = 300.0f;
            handToBike = world.CreateJoint(handToBikeDef);

            DistanceJointDef armToBikeDef = new DistanceJointDef();
            armToBikeDef.Initialize(hand, bikeBody,
                                    hand.GetWorldPoint(new Vector2(-17.00f / Level.FACTOR,
                                                       4.26f / Level.FACTOR)),
                                    bikeBody.GetWorldCenter());
            armToBikeDef.length = 40.0f / Level.FACTOR;
            armToBikeDef.frequencyHz = 10.0f;
            armToBikeDef.dampingRatio = 1.0f;
            armToBikeDef.collideConnected = true;
            armToBike = world.CreateJoint(armToBikeDef);
        }
Esempio n. 2
0
File: World.cs Progetto: Bitsits/web
        /// <summary>
        /// Call this to draw shapes and other debug draw data.
        /// </summary>
        public void DrawDebugData()
        {
            if (DebugDraw == null)
            {
                return;
            }

            DebugDrawFlags flags = DebugDraw.Flags;

            if ((flags & DebugDrawFlags.Shape) == DebugDrawFlags.Shape)
            {
                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    Transform xf;
                    b.GetTransform(out xf);
                    for (Fixture f = b.GetFixtureList(); f != null; f = f.GetNext())
                    {
                        if (b.IsActive() == false)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.5f, 0.3f));
                        }
                        else if (b.GetType() == BodyType.Static)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.9f, 0.5f));
                        }
                        else if (b.GetType() == BodyType.Kinematic)
                        {
                            DrawShape(f, xf, new Color(0.5f, 0.5f, 0.9f));
                        }
                        else if (b.IsAwake() == false)
                        {
                            DrawShape(f, xf, new Color(0.6f, 0.6f, 0.6f));
                        }
                        else
                        {
                            DrawShape(f, xf, new Color(0.9f, 0.7f, 0.7f));
                        }
                    }
                }
            }

            if ((flags & DebugDrawFlags.Joint) == DebugDrawFlags.Joint)
            {
                for (Joint j = _jointList; j != null; j = j.GetNext())
                {
                    DrawJoint(j);
                }
            }

            if ((flags & DebugDrawFlags.Pair) == DebugDrawFlags.Pair)
            {
                Color color = new Color(0.3f, 0.9f, 0.9f);
                for (Contact c = _contactManager._contactList; c != null; c = c.GetNext())
                {
                    Fixture fixtureA = c.GetFixtureA();
                    Fixture fixtureB = c.GetFixtureB();

                    AABB aabbA;
                    AABB aabbB;
                    fixtureA.GetAABB(out aabbA);
                    fixtureB.GetAABB(out aabbB);

                    Vector2 cA = aabbA.GetCenter();
                    Vector2 cB = aabbB.GetCenter();

                    DebugDraw.DrawSegment(cA, cB, color);
                }
            }

            if ((flags & DebugDrawFlags.AABB) == DebugDrawFlags.AABB)
            {
                Color      color = new Color(0.9f, 0.3f, 0.9f);
                BroadPhase bp    = _contactManager._broadPhase;

                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    if (b.IsActive() == false)
                    {
                        continue;
                    }

                    for (Fixture f = b.GetFixtureList(); f != null; f = f.GetNext())
                    {
                        AABB aabb;
                        bp.GetFatAABB(f._proxyId, out aabb);
                        FixedArray8 <Vector2> vs = new FixedArray8 <Vector2>();
                        vs[0] = new Vector2(aabb.lowerBound.X, aabb.lowerBound.Y);
                        vs[1] = new Vector2(aabb.upperBound.X, aabb.lowerBound.Y);
                        vs[2] = new Vector2(aabb.upperBound.X, aabb.upperBound.Y);
                        vs[3] = new Vector2(aabb.lowerBound.X, aabb.upperBound.Y);

                        DebugDraw.DrawPolygon(ref vs, 4, color);
                    }
                }
            }

            if ((flags & DebugDrawFlags.CenterOfMass) == DebugDrawFlags.CenterOfMass)
            {
                for (Body b = _bodyList; b != null; b = b.GetNext())
                {
                    Transform xf;
                    b.GetTransform(out xf);
                    xf.Position = b.GetWorldCenter();
                    DebugDraw.DrawTransform(ref xf);
                }
            }
        }