internal DistanceJoint(DistanceJointDef def)
     : base(def)
 {
     _localAnchor1 = def.localAnchorA;
     _localAnchor2 = def.localAnchorB;
     _length = def.length;
     _frequencyHz = def.frequencyHz;
     _dampingRatio = def.dampingRatio;
     _impulse = 0.0f;
     _gamma = 0.0f;
     _bias = 0.0f;
 }
Пример #2
0
 internal DistanceJoint(DistanceJointDef def)
     : base(def)
 {
     _localAnchor1 = def.localAnchorA;
     _localAnchor2 = def.localAnchorB;
     _length       = def.length;
     _frequencyHz  = def.frequencyHz;
     _dampingRatio = def.dampingRatio;
     _impulse      = 0.0f;
     _gamma        = 0.0f;
     _bias         = 0.0f;
 }
Пример #3
0
        public void ApplyAttribtues(DistanceJointDef j)
        {
            j.collideConnected = collideConnected;

            if (length != 1.0f)
            {
                j.length = length;
            }

            if(frequencyHz != 0.0f)
            {
                j.frequencyHz = frequencyHz;
            }

            if (dampingRatio != 0.0f)
            {
                j.dampingRatio = dampingRatio;
            }
        }
        public void CreateBond()
        {
            if (nearestAtom == null) return;

            bondsLeft -= numberOfBonds;
            if (symbol == Symbol.X) bondsLeft = 0;

            nearestAtom.bondsLeft -= numberOfBonds;
            if (nearestAtom.symbol == Symbol.X) nearestAtom.bondsLeft = 0;

            DistanceJointDef jd = new DistanceJointDef();
            jd.bodyA = body;
            jd.bodyB = nearestAtom.body;
            jd.dampingRatio = 0.2f;
            //jd.frequencyHz = 20.0f;
            jd.collideConnected = true;

            // Mid
            jd.length = (float)(bondLength) / gameContent.b2Scale;
            jd.localAnchorA = jd.localAnchorB = Vector2.Zero;
            Joint midJoint = world.CreateJoint(jd);

            Vector2 a = body.Position * gameContent.b2Scale;
            Vector2 b = nearestAtom.body.Position * gameContent.b2Scale;
            float slope = (float)Math.Atan2((b.Y - a.Y), (b.X - a.X));

            // Short
            jd.length = (float)(bondLength - gameContent.atomRadius * 2) / gameContent.b2Scale;

            Vector2 e = new Vector2((float)Math.Cos(slope), (float)Math.Sin(slope)) * gameContent.atomRadius;

            jd.localAnchorA = body.GetLocalPoint((a + e) / gameContent.b2Scale);
            jd.localAnchorB = nearestAtom.body.GetLocalPoint((b - e) / gameContent.b2Scale);
            Joint shortJoint = world.CreateJoint(jd);

            bonds.Add(new Bond(this, nearestAtom, shortJoint, midJoint, numberOfBonds, gameContent));
            bondedAtoms.Add(nearestAtom);
            SetBondAngle();

            nearestAtom.bonds.Add(new Bond(nearestAtom, this, shortJoint, midJoint, numberOfBonds, gameContent));
            nearestAtom.bondedAtoms.Add(this);
            nearestAtom.SetBondAngle();

            gameContent.broop[gameContent.random.Next(gameContent.broop.Length)].Play();
        }
Пример #5
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);
        }
Пример #6
0
        /// <summary>
        /// Resets the bike and the driver to their initial positions
        /// Resets all angular and linear speeds
        /// Recreates the joints if they were destroyed
        /// </summary>
        public void Reset()
        {
            foreach (var part in parts)
            {
                part.SetAngularVelocity(0);
                part.SetLinearVelocity(Vector2.Zero);
            }

            frontWheel.SetTransform(frontWheelInitPos / Level.FACTOR, 0);
            frontFork.SetTransform(frontForkInitPos / Level.FACTOR, 0);
            rearWheel.SetTransform(rearWheelInitPos / Level.FACTOR, 0);
            rearFork.SetTransform(rearForkInitPos / Level.FACTOR,
                                  rearForkInitRot * Level.DEG_TO_RAD);
            bikeBody.SetTransform(bikeBodyInitPos / Level.FACTOR,
                                  bikeBodyInitRot * Level.DEG_TO_RAD);

            head.SetTransform(headInitPos / Level.FACTOR, headInitRot * Level.DEG_TO_RAD);
            humanBody.SetTransform(humanBodyInitPos / Level.FACTOR, 0);
            hand.SetTransform(handInitPos / Level.FACTOR, handInitRot * Level.DEG_TO_RAD);
            arm.SetTransform(armInitPos / Level.FACTOR, armInitRot * Level.DEG_TO_RAD);

            if (OffTheBike)
            {
                RevoluteJointDef handToBikeDef = new RevoluteJointDef();
                Vector2 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);

                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);

                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);

                OffTheBike = false;
            }

            if (!bikeBody.IsAwake())
                bikeBody.SetAwake(true);

            if (RotationData.device)
                motor._enableMotor = true;
        }
        void CreateJoint(Body other, Vector2 contact)
        {
            // cannot move from stable situation to unstable
            if (isStable && other.GetUserData() is Soldier && !((Soldier)other.GetUserData()).isStable)
                return;

            DestroyJoint();

            if (!(other.GetUserData() is Soldier) || ((Soldier)other.GetUserData()).joint == null
                || ((Soldier)other.GetUserData()).joint.GetBodyB() != body)
            {
                DistanceJointDef jd = new DistanceJointDef();

                //jd.frequencyHz = 10; jd.dampingRatio = 0f;
                jd.Initialize(body, other, body.Position, contact);
                jd.length = Size / 2 / gameContent.b2Scale;
                jd.collideConnected = true;
                joint = body.GetWorld().CreateJoint(jd);

                if (joint.GetBodyB().GetUserData() is Soldier)
                    isStable = ((Soldier)joint.GetBodyB().GetUserData()).isStable;
                else isStable = true;
            }
        }
Пример #8
0
        public static Joint AddJoint(this IJointable ithis, V2DJoint joint, float offsetX, float offsetY)
        {
            Joint result = null;
            JointDef jointDef = null;
            //Body targ0 = ithis.VScreen.bodyMap[joint.Body1];
            //Body targ1 = ithis.VScreen.bodyMap[joint.Body2];
            Body targ0 = GetBody(ithis, joint.Body1);
            Body targ1 = GetBody(ithis, joint.Body2);

            // gears need the first body static
            if (targ0 != null && targ1 != null && targ1.GetType() == BodyType.Static && targ0.GetType() != BodyType.Static)
            {
                Body temp = targ0;
                targ0 = targ1;
                targ1 = temp;
            }

            Vector2 pt0 = new Vector2(joint.X + offsetX, joint.Y + offsetY);

            string name = joint.Name;

            Vector2 anchor0 = new Vector2(pt0.X / V2DScreen.WorldScale, pt0.Y / V2DScreen.WorldScale);
            Vector2 anchor1 = new Vector2();

            switch (joint.Type)
            {
                case V2DJointKind.Distance:
                    Vector2 pt1 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                    anchor1 = new Vector2(pt1.X / V2DScreen.WorldScale, pt1.Y / V2DScreen.WorldScale);

                    DistanceJointDef dj = new DistanceJointDef();
                    dj.Initialize(targ0, targ1, anchor0, anchor1);
                    dj.collideConnected = joint.CollideConnected;
                    dj.dampingRatio = joint.DampingRatio;
                    dj.frequencyHz = joint.FrequencyHz;
                    if (joint.Length != -1)
                    {
                        dj.length = joint.Length / V2DScreen.WorldScale;
                    }

                    jointDef = dj;
                    break;

                case V2DJointKind.Revolute:
                    float rot0 = joint.Min; //(typeof(joint["min"]) == "string") ? parseFloat(joint["min"]) / 180 * Math.PI : joint["min"];
                    float rot1 = joint.Max; //(typeof(joint["max"]) == "string") ? parseFloat(joint["max"]) / 180 * Math.PI : joint["max"];

                    RevoluteJointDef rj = new RevoluteJointDef();
                    rj.Initialize(targ0, targ1, anchor0);
                    rj.lowerAngle = rot0;
                    rj.upperAngle = rot1;

                    rj.enableLimit = rot0 != 0 && rot1 != 0;
                    rj.maxMotorTorque = joint.MaxMotorTorque;
                    rj.motorSpeed = joint.MotorSpeed;
                    rj.enableMotor = joint.EnableMotor;

                    jointDef = rj;
                    break;

                case V2DJointKind.Prismatic:
                    float axisX = joint.AxisX;
                    float axisY = joint.AxisY;
                    float min = joint.Min;
                    float max = joint.Max;

                    PrismaticJointDef pj = new PrismaticJointDef();
                    Vector2 worldAxis = new Vector2(axisX, axisY);
                    pj.Initialize(targ0, targ1, anchor0, worldAxis);
                    pj.lowerTranslation = min / V2DScreen.WorldScale;
                    pj.upperTranslation = max / V2DScreen.WorldScale;

                    pj.enableLimit = joint.EnableLimit;
                    pj.maxMotorForce = joint.MaxMotorTorque;
                    pj.motorSpeed = joint.MotorSpeed;
                    pj.enableMotor = joint.EnableMotor;

                    jointDef = pj;
                    break;

                case V2DJointKind.Pully:
                    Vector2 pt2 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                    anchor1 = new Vector2(pt2.X / V2DScreen.WorldScale, pt2.Y / V2DScreen.WorldScale);

                    Vector2 groundAnchor0 = new Vector2(joint.GroundAnchor1X / V2DScreen.WorldScale, joint.GroundAnchor1Y / V2DScreen.WorldScale);

                    Vector2 groundAnchor1 = new Vector2(joint.GroundAnchor2X / V2DScreen.WorldScale, joint.GroundAnchor2Y / V2DScreen.WorldScale);

                    float max0 = joint.MaxLength1;
                    float max1 = joint.MaxLength2;

                    float rat = joint.Ratio;

                    PulleyJointDef puj = new PulleyJointDef();
                    puj.Initialize(targ0, targ1, groundAnchor0, groundAnchor1, anchor0, anchor1, rat);
                    puj.maxLengthA = (max0 + max1) / V2DScreen.WorldScale;
                    puj.maxLengthB = (max0 + max1) / V2DScreen.WorldScale;

                    puj.collideConnected = joint.CollideConnected;

                    jointDef = puj;
                    break;

                case V2DJointKind.Gear:
                    GearJointDef gj = new GearJointDef();
                    gj.bodyA = targ0;
                    gj.bodyB = targ1;
                    gj.joint1 = GetFirstGearableJoint(targ0.GetJointList());
                    gj.joint2 = GetFirstGearableJoint(targ1.GetJointList());
                    gj.ratio = joint.Ratio;
                    jointDef = gj;
                    break;
            }

            if (jointDef != null)
            {
                result = SetJointWithReflection(ithis, name, jointDef);

                if (result != null)
                {
                    Dictionary<string, string> dict = new Dictionary<string, string>();
                    dict["name"] = name;
                    result.SetUserData(dict);
                }
            }

            return result;
        }
        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));
        }