public void ApplyAttribtues(RevoluteJointDef j) { j.collideConnected = collideConnected; if (lowerAngle != 0.0f) { j.lowerAngle = lowerAngle; } if (upperAngle != 0.0f) { j.upperAngle = upperAngle; } if (maxMotorTorque != 0.0f) { j.maxMotorTorque = maxMotorTorque; } if (motorSpeed != 0.0f) { j.motorSpeed = motorSpeed; } if (enableLimit != false) { j.enableLimit = enableLimit; } if (enableMotor != false) { j.enableMotor = enableMotor; } }
public Apple(Fixture connectedFixture, GameContent gameContent, World world) { this.world = world; this.gameContent = gameContent; texture = gameContent.apple; fallenFromAir = false; CreatBody(); State = LeafState.Grow; if (connectedFixture != null) { this.connectedFixture = connectedFixture; AABB aabb; connectedFixture.GetAABB(out aabb); body.Position = aabb.GetCenter(); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.bodyA = body; rjd.bodyB = connectedFixture.GetBody(); rjd.localAnchorA = Vector2.Zero; rjd.localAnchorB = connectedFixture.GetBody().GetLocalPoint(body.Position); revoJoint = (RevoluteJoint)world.CreateJoint(rjd); } }
internal RevoluteJoint(RevoluteJointDef def) : base(def) { _localAnchor1 = def.localAnchorA; _localAnchor2 = def.localAnchorB; _referenceAngle = def.referenceAngle; _impulse = Vector3.Zero; _motorImpulse = 0.0f; _lowerAngle = def.lowerAngle; _upperAngle = def.upperAngle; _maxMotorTorque = def.maxMotorTorque; _motorSpeed = def.motorSpeed; _enableLimit = def.enableLimit; _enableMotor = def.enableMotor; _limitState = LimitState.Inactive; }
public Branch(Path2D path, GameContent gameContent, World world, Branch nearestBranch) { this.gameContent = gameContent; dot = gameContent.dot; circle = gameContent.jointCircle; square = gameContent.jointSquare; cursor = gameContent.cursor; BodyDef bd = new BodyDef(); bd.position = path.Keys[0]; bd.type = BodyType.Dynamic; body = world.CreateBody(bd); fixtureCount = path.Keys.Count; for (int i = 0; i < path.Keys.Count; i++) CreateFixture(path.Keys[i] - path.Keys[0]); if (nearestBranch != null) { RevoluteJointDef revJd = new RevoluteJointDef(); revJd.bodyA = body; revJd.bodyB = nearestBranch.body; revJd.localAnchorA = Vector2.Zero; AABB aabb; nearestBranch.nearestFixture.GetAABB(out aabb); Vector2 p = aabb.GetCenter(); revJd.localAnchorB = nearestBranch.nearestFixture.GetBody().GetLocalPoint(p); revJd.enableMotor = true; revJd.referenceAngle = nearestBranch.nearestFixture.GetBody().Rotation; revoJoint = (RevoluteJoint)world.CreateJoint(revJd); revoJoint.SetUserData((Branch)nearestBranch); nearestBranch.nearestFixture.SetUserData((Branch)this); } newGrow = true; }
public void SetMode(bool editMode, bool isMouse) { if (!clampEnabled) isClamped = false; this.editMode = editMode; float reqMass = 10; if (isSelected) reqMass = .2f; else { body.SetActive(true); body.SetLinearDamping(0); body.SetAngularDamping(0); } for (Fixture f = body.GetFixtureList(); f != null; f = f.GetNext()) { object area = f.GetUserData(); if (area is float) f.SetDensity(reqMass / (float)area / fixtureCount); if (isSelected) f.SetFilterData(ref mouseFilter); else if (editMode) f.SetFilterData(ref equipFilter); else f.SetFilterData(ref groundFilter); } body.ResetMassData(); if (!editMode || (isSelected && !isMouse) || isClamped) { if (pin == null) // Pin { BodyDef bd = new BodyDef(); Body b = world.CreateBody(bd); RevoluteJointDef jd = new RevoluteJointDef(); jd.bodyA = b; jd.bodyB = body; jd.localAnchorA = body.Position; jd.localAnchorB = Vector2.Zero; pin = world.CreateJoint(jd); body.SetAngularVelocity(0); body.SetFixedRotation(true); } } else if (pin != null) { body.SetFixedRotation(false); world.DestroyJoint(pin); pin = null; } }
/// <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); }
/// <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 HandleEquipment(InputState input) { Vector2 m = mousePos * gameContent.scale; Point mp = new Point((int)m.X, (int)m.Y); Equipment mouseOverEq = null; if (mouseEq == null) foreach (Equipment e in equipments) { e.isMouseOver = false; if (mouseOverEq == null && !isMenuEntrySelected && (e.MoveButtonBound.Contains(mp) || e.RotationButtonBound.Contains(mp))) { mouseOverEq = e; e.isMouseOver = true; } } if (input.IsLeftClicked() && mouseOverEq != null) { if (mouseOverEq.MoveButtonBound.Contains(mp) || mouseOverEq.RotationButtonBound.Contains(mp)) { MouseJointDef mjd = new MouseJointDef(); mjd.maxForce = 500; //mjd.frequencyHz = 10f; mjd.dampingRatio = .1f; mjd.target = mousePos; mjd.bodyA = mouseGroundBody; mjd.bodyB = mouseOverEq.body; mouseJoint = (MouseJoint)world.CreateJoint(mjd); mouseEq = mouseOverEq; mouseEq.body.SetAngularDamping(20000); if (mouseOverEq.RotationButtonBound.Contains(mp)) { RevoluteJointDef rjd = new RevoluteJointDef(); rjd.bodyA = mouseGroundBody; rjd.bodyB = mouseOverEq.body; rjd.localAnchorA = mouseOverEq.body.Position; rjd.localAnchorB = Vector2.Zero; pin = world.CreateJoint(rjd); mouseEq.body.SetAngularDamping(20); } if (selectedEq != mouseOverEq) { if (selectedEq != null) { selectedEq.isSelected = false; selectedEq.SetMode(editMode, false); selectedEq = null; } selectedEq = mouseOverEq; selectedEq.isClamped = false; selectedEq.isSelected = true; } mouseEq.SetMode(editMode, true); } } else if (mouseOverEq != null && input.IsRightClicked() || input.CurrentMouseState[0].LeftButton == ButtonState.Released) { if (mouseJoint != null && mouseEq != null) { world.DestroyJoint(mouseJoint); mouseJoint = null; if (pin != null) { world.DestroyJoint(pin); pin = null; } mouseEq.SetMode(editMode, false); mouseEq = null; } if (mouseOverEq != null && input.IsRightClicked()) { selectedEq = null; mouseOverEq.Remove(); equipments.Remove(mouseOverEq); } } // Remove if (!equipmentAdded && (input.IsLeftClicked() || input.IsRightClicked()) && mouseOverEq == null && mouseEq == null && selectedEq != null) { selectedEq.isSelected = false; selectedEq.SetMode(editMode, false); selectedEq = null; } equipmentAdded = false; }
private void CreateStem() { AddBranch = true; AABB aabb = new AABB(); body.GetFixtureList().GetAABB(out aabb); Vector2 v = aabb.GetCenter() + new Vector2(0, 15); Path2D p = new Path2D(); p.AddPoint(v); p.AddPoint(v + new Vector2(0, -10)); stem = new Branch(p, gameContent, body.GetWorld(), null); RevoluteJointDef revJd = new RevoluteJointDef(); revJd.bodyA = stem.body; revJd.bodyB = ground; revJd.collideConnected = true; revJd.localAnchorA = Vector2.Zero; revJd.localAnchorB = stem.body.Position; revJd.enableMotor = true; stem.revoJoint = (RevoluteJoint)world.CreateJoint(revJd); // A Small rotation sets the body in motion stem.body.Rotation = (float)Math.PI / 360; }
void CreateWheels() { float width = gameContent.playerCar.Width, height = gameContent.playerCar.Height; Vector2 halfws = wheelSize / 2 / gameContent.Scale; PolygonShape ps = new PolygonShape(); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; FixtureDef fd = new FixtureDef(); fd.density = 0.1f; // Steer Wheel bd.position = body.Position + new Vector2(0, -(height - width / 2)) / gameContent.Scale; ps.SetAsBox(halfws.X, halfws.Y); fd.shape = ps; steerWheel = world.CreateBody(bd); steerWheel.CreateFixture(fd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body, steerWheel, steerWheel.GetWorldCenter()); rjd.enableMotor = true; rjd.maxMotorTorque = 100; steerJoint = world.CreateJoint(rjd) as RevoluteJoint; // Drive Wheel bd.position = body.Position + new Vector2(0, -halfws.Y); ps.SetAsBox(halfws.X, halfws.Y); fd.shape = ps; driveWheel = world.CreateBody(bd); driveWheel.CreateFixture(fd); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.Initialize(body, driveWheel, driveWheel.GetWorldCenter(), new Vector2(1, 0)); pjd.lowerTranslation = pjd.upperTranslation = 0; pjd.enableLimit = true; world.CreateJoint(pjd); }
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 LoadGate() { int height = 80; Vector2 jointPos = new Vector2(493, 439); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = jointPos + new Vector2(0, height / 2); gate = world.CreateBody(bd); PolygonShape pShape = new PolygonShape(); pShape.SetAsBox(0.2f, height / 2); FixtureDef fd = new FixtureDef(); fd.shape = pShape; fd.density = 0.3f; gate.CreateFixture(fd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.bodyA = gate; rjd.bodyB = ground; rjd.localAnchorA = new Vector2(0, -height / 2); rjd.localAnchorB = jointPos; world.CreateJoint(rjd); }