/// <summary> /// Attaches the bodies with revolute joints. /// </summary> /// <param name="world">The world.</param> /// <param name="bodies">The bodies.</param> /// <param name="localAnchorA">The local anchor A.</param> /// <param name="localAnchorB">The local anchor B.</param> /// <param name="connectFirstAndLast">if set to <c>true</c> [connect first and last].</param> /// <param name="collideConnected">if set to <c>true</c> [collide connected].</param> public static List<RevoluteJoint> AttachBodiesWithRevoluteJoint(World world, List<Body> bodies, Vector2 localAnchorA, Vector2 localAnchorB, bool connectFirstAndLast, bool collideConnected) { List<RevoluteJoint> joints = new List<RevoluteJoint>(bodies.Count + 1); for (int i = 1; i < bodies.Count; i++) { RevoluteJoint joint = new RevoluteJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB); joint.CollideConnected = collideConnected; world.AddJoint(joint); joints.Add(joint); } if (connectFirstAndLast) { RevoluteJoint lastjoint = new RevoluteJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB); lastjoint.CollideConnected = collideConnected; world.AddJoint(lastjoint); joints.Add(lastjoint); } return joints; }
protected override void CreateExtraJoints(World world) { rightHandSpring = new FixedMouseJoint(_lowerRightArm, _lowerRightArm.Position); rightHandSpring.MaxForce = 1000.0f * _lowerRightArm.Mass; world.AddJoint(rightHandSpring); leftHandSpring = new FixedMouseJoint(_lowerLeftArm, _lowerLeftArm.Position); leftHandSpring.MaxForce = 1000.0f * _lowerLeftArm.Mass; world.AddJoint(leftHandSpring); }
public override Object PlacePhysicsObject(Microsoft.Xna.Framework.Vector2 position, FarseerPhysics.Dynamics.World world) { List <Fixture> list = world.TestPointAll(position); if (pin.Checked && list.Count > 0) { FixedRevoluteJoint j = new FixedRevoluteJoint(list[0].Body, list[0].Body.GetLocalPoint(position), position); if (motorEnabled.Checked) { j.MotorEnabled = true; float speed; float maxTorque; if (float.TryParse(motorSpeed.Text, out speed)) { j.MotorSpeed = speed; } if (float.TryParse(motorTorque.Text, out maxTorque)) { j.MaxMotorTorque = maxTorque; } } world.AddJoint(j); return(j); } if (list.Count > 1) { RevoluteJoint j = new RevoluteJoint(list[0].Body, list[1].Body, list[0].Body.GetLocalPoint(position), list[1].Body.GetLocalPoint(position)); if (motorEnabled.Checked) { j.MotorEnabled = true; float speed; float maxTorque; if (float.TryParse(motorSpeed.Text, out speed)) { j.MotorSpeed = speed; } if (float.TryParse(motorTorque.Text, out maxTorque)) { j.MaxMotorTorque = maxTorque; } } world.AddJoint(j); return(j); } return(base.PlacePhysicsObject(position, world)); }
public static FixedDistanceJoint CreateFixedDistanceJoint(World world, Body body, Vector2 localAnchor, Vector2 worldAnchor) { FixedDistanceJoint distanceJoint = new FixedDistanceJoint(body, localAnchor, worldAnchor); world.AddJoint(distanceJoint); return distanceJoint; }
/// <summary> /// Creates a fixed angle joint. /// </summary> /// <param name="world">The world.</param> /// <param name="body">The body.</param> /// <returns></returns> public static FixedAngleJoint CreateFixedAngleJoint(World world, Body body) { FixedAngleJoint angleJoint = new FixedAngleJoint(body); world.AddJoint(angleJoint); return angleJoint; }
public static DistanceJoint CreateDistanceJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB) { DistanceJoint distanceJoint = new DistanceJoint(bodyA, bodyB, anchorA, anchorB); world.AddJoint(distanceJoint); return distanceJoint; }
/// <summary> /// Creates an angle joint. /// </summary> /// <param name="world">The world.</param> /// <param name="bodyA">The first body.</param> /// <param name="bodyB">The second body.</param> /// <returns></returns> public static AngleJoint CreateAngleJoint(World world, Body bodyA, Body bodyB) { AngleJoint angleJoint = new AngleJoint(bodyA, bodyB); world.AddJoint(angleJoint); return angleJoint; }
public static PulleyJoint CreatePulleyJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, Vector2 worldAnchorA, Vector2 worldAnchorB, float ratio, bool useWorldCoordinates = false) { PulleyJoint pulleyJoint = new PulleyJoint(bodyA, bodyB, anchorA, anchorB, worldAnchorA, worldAnchorB, ratio, useWorldCoordinates); world.AddJoint(pulleyJoint); return pulleyJoint; }
public static FrictionJoint CreateFrictionJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB) { FrictionJoint frictionJoint = new FrictionJoint(bodyA, bodyB, anchorA, anchorB); world.AddJoint(frictionJoint); return frictionJoint; }
public Turret(Vector2 farseerLoc, World w, RagdollManager r, Fixture f) { DebugMaterial gray = new DebugMaterial(MaterialType.Blank) { Color = Color.DarkGray }; body = new Body(w); pivot = FixtureFactory.AttachCircle(.9f, 1, body, gray); FixtureFactory.AttachRectangle(barrelLength, .5f, 1, new Vector2(barrelLength / 2, 0), body, gray); body.Position = farseerLoc; body.BodyType = BodyType.Dynamic; //b.CollidesWith = Category.None; if (f == null) { motor = JointFactory.CreateFixedRevoluteJoint(w, body, Vector2.Zero, farseerLoc); } else { motor = new RevoluteJoint(body, f.Body, Vector2.Zero, f.Body.GetLocalPoint(farseerLoc)); w.AddJoint(motor); } motor.MotorEnabled = true; motor.MaxMotorTorque = 5000; Init(w, r); }
/// <summary> /// Creates the fixed revolute joint. /// </summary> /// <param name="world">The world.</param> /// <param name="body">The body.</param> /// <param name="bodyAnchor">The body anchor.</param> /// <param name="worldAnchor">The world anchor.</param> /// <returns></returns> public static FixedRevoluteJoint CreateFixedRevoluteJoint(World world, Body body, Vector2 bodyAnchor, Vector2 worldAnchor) { FixedRevoluteJoint fixedRevoluteJoint = new FixedRevoluteJoint(body, bodyAnchor, worldAnchor); world.AddJoint(fixedRevoluteJoint); return fixedRevoluteJoint; }
public static FixedPrismaticJoint CreateFixedPrismaticJoint(World world, Body body, Vector2 worldAnchor, Vector2 axis) { FixedPrismaticJoint joint = new FixedPrismaticJoint(body, worldAnchor, axis); world.AddJoint(joint); return joint; }
/// <summary> /// Creates a revolute joint and adds it to the world /// </summary> /// <param name="world"></param> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="anchorB"></param> /// <returns></returns> public static RevoluteJoint CreateRevoluteJoint(World world, Body bodyA, Body bodyB, Vector2 anchorB) { Vector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(anchorB)); RevoluteJoint joint = new RevoluteJoint(bodyA, bodyB, localanchorA, anchorB); world.AddJoint(joint); return joint; }
/// <summary> /// Attaches the bodies with revolute joints. /// </summary> /// <param name="world">The world.</param> /// <param name="bodies">The bodies.</param> /// <param name="localAnchorA">The local anchor A.</param> /// <param name="localAnchorB">The local anchor B.</param> /// <param name="connectFirstAndLast">if set to <c>true</c> [connect first and last].</param> /// <param name="collideConnected">if set to <c>true</c> [collide connected].</param> public static void AttachBodiesWithSliderJoint(World world, List<Body> bodies, Vector2 localAnchorA, Vector2 localAnchorB, bool connectFirstAndLast, bool collideConnected, float minLength, float maxLength) { for (int i = 1; i < bodies.Count; i++) { SliderJoint joint = new SliderJoint(bodies[i], bodies[i - 1], localAnchorA, localAnchorB, minLength, maxLength); joint.CollideConnected = collideConnected; world.AddJoint(joint); } if (connectFirstAndLast) { SliderJoint lastjoint = new SliderJoint(bodies[0], bodies[bodies.Count - 1], localAnchorA, localAnchorB, minLength, maxLength); lastjoint.CollideConnected = collideConnected; world.AddJoint(lastjoint); } }
public override void LoadContent(ContentManager content, World physics) { base.LoadContent(content, physics); this.physics.Position = ConvertUnits.ToSimUnits(50, 50); this.physics.CollisionCategories = Category.Cat1; this.physics.CollidesWith = Category.Cat1; fixture = new FixedMouseJoint(this.physics, ConvertUnits.ToSimUnits(new Vector2(50))); fixture.MaxForce = float.MaxValue; physics.AddJoint(fixture); }
public void Update(GameTime gameTime, CameraOperator cameraOperator, World world) { _sprite.Body.Center = Input.Mouse.Position; if (_mouseJoint != null) { if (Input.Mouse.IsButtonDown(Input.Mouse.MouseButton.Left)) { _mouseJoint.WorldAnchorB = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position); } else { world.RemoveJoint(_mouseJoint); _mouseJoint = null; } } else { Vector2 cursorWorldPosition = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position); if (Input.Mouse.IsButtonClicked(Input.Mouse.MouseButton.Left)) { Fixture fixture; float radiusMeters = _sprite.Body.Projection.BoundingBox.Dimensions.X.ToMeters(); if ((fixture = world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters))) != null || (fixture = world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters))) != null || (fixture = world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters, -radiusMeters))) != null || (fixture = world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters, -radiusMeters))) != null) { if (fixture.UserData is Coin) { _mouseJoint = new FixedMouseJoint(fixture.Body, cursorWorldPosition) { MaxForce = 200*fixture.Body.Mass }; world.AddJoint(_mouseJoint); } } } } }
public SwingPlatform(World world, SwingPlatformData swingPlatformData) { width = swingPlatformData.Width; height = swingPlatformData.Height; body = BodyFactory.CreateRectangle(world, width, height, 1.0f); body.Position = swingPlatformData.Center; body.BodyType = BodyType.Dynamic; anchor = BodyFactory.CreateCircle(world, 0.1f, 1.0f); anchor.Position = swingPlatformData.Center; anchor.BodyType = BodyType.Static; RevoluteJoint revoluteJoint = new RevoluteJoint(body, anchor, new Vector2(0.0f, 0.0f), new Vector2(0.0f, 0.0f)); revoluteJoint.LowerLimit = -swingPlatformData.MaxAngle; revoluteJoint.UpperLimit = swingPlatformData.MaxAngle; revoluteJoint.LimitEnabled = true; revoluteJoint.MaxMotorTorque = 10.0f; revoluteJoint.MotorSpeed = 0.0f; revoluteJoint.MotorEnabled = true; world.AddJoint(revoluteJoint); }
public void PopulateWorld(World w) { foreach (Body b in bodyList) { b.setWorld(w); foreach (Fixture f in b.FixtureList) { f.Shape.ComputeProperties(); w.FixtureAdded(f); } float mass = b.Mass; //b.ResetMassData(); b.Mass = mass; b.Enabled = true; b.Awake = true; w.AddBody(b); //w.BodyList.Add(b); } w.ProcessChanges(); foreach (Joint j in jointList) { w.AddJoint(j); } w.ProcessChanges(); }
public void Deserialize(World world, Stream stream) { world.Clear(); XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream); if (root.Name.ToLower() != "world") throw new Exception(); foreach (XMLFragmentElement main in root.Elements) { if (main.Name.ToLower() == "gravity") { world.Gravity = ReadVector(main); } } foreach (XMLFragmentElement shapeElement in root.Elements) { if (shapeElement.Name.ToLower() == "shapes") { foreach (XMLFragmentElement n in shapeElement.Elements) { if (n.Name.ToLower() != "shape") throw new Exception(); ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true); switch (type) { case ShapeType.Circle: { CircleShape shape = new CircleShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "radius": shape.Radius = float.Parse(sn.Value); break; case "position": shape.Position = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(shape); } break; case ShapeType.Polygon: { PolygonShape shape = new PolygonShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "vertices": { List<Vector2> verts = new List<Vector2>(); foreach (XMLFragmentElement vert in sn.Elements) verts.Add(ReadVector(vert)); shape.Set(new Vertices(verts.ToArray())); } break; case "centroid": shape.MassData.Centroid = ReadVector(sn); break; } } _shapes.Add(shape); } break; case ShapeType.Edge: { EdgeShape shape = new EdgeShape(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "hasvertex0": shape.HasVertex0 = bool.Parse(sn.Value); break; case "hasvertex3": shape.HasVertex0 = bool.Parse(sn.Value); break; case "vertex0": shape.Vertex0 = ReadVector(sn); break; case "vertex1": shape.Vertex1 = ReadVector(sn); break; case "vertex2": shape.Vertex2 = ReadVector(sn); break; case "vertex3": shape.Vertex3 = ReadVector(sn); break; default: throw new Exception(); } } _shapes.Add(shape); } break; } } } } foreach (XMLFragmentElement fixtureElement in root.Elements) { if (fixtureElement.Name.ToLower() == "fixtures") { foreach (XMLFragmentElement n in fixtureElement.Elements) { Fixture fixture = new Fixture(); if (n.Name.ToLower() != "fixture") throw new Exception(); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "shape": fixture.Shape = _shapes[int.Parse(sn.Value)]; break; case "density": fixture.Shape.Density = float.Parse(sn.Value); break; case "filterdata": foreach (XMLFragmentElement ssn in sn.Elements) { switch (ssn.Name.ToLower()) { case "categorybits": fixture._collisionCategories = (Category)int.Parse(ssn.Value); break; case "maskbits": fixture._collidesWith = (Category)int.Parse(ssn.Value); break; case "groupindex": fixture._collisionGroup = short.Parse(ssn.Value); break; } } break; case "friction": fixture.Friction = float.Parse(sn.Value); break; case "issensor": fixture.IsSensor = bool.Parse(sn.Value); break; case "restitution": fixture.Restitution = float.Parse(sn.Value); break; case "userdata": fixture.UserData = ReadSimpleType(sn, null, false); break; } } _fixtures.Add(fixture); } } } foreach (XMLFragmentElement bodyElement in root.Elements) { if (bodyElement.Name.ToLower() == "bodies") { foreach (XMLFragmentElement n in bodyElement.Elements) { Body body = new Body(world); if (n.Name.ToLower() != "body") throw new Exception(); body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true); foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "active": if (bool.Parse(sn.Value)) body.Flags |= BodyFlags.Enabled; else body.Flags &= ~BodyFlags.Enabled; break; case "allowsleep": body.SleepingAllowed = bool.Parse(sn.Value); break; case "angle": { Vector2 position = body.Position; body.SetTransformIgnoreContacts(ref position, float.Parse(sn.Value)); } break; case "angulardamping": body.AngularDamping = float.Parse(sn.Value); break; case "angularvelocity": body.AngularVelocity = float.Parse(sn.Value); break; case "awake": body.Awake = bool.Parse(sn.Value); break; case "bullet": body.IsBullet = bool.Parse(sn.Value); break; case "fixedrotation": body.FixedRotation = bool.Parse(sn.Value); break; case "lineardamping": body.LinearDamping = float.Parse(sn.Value); break; case "linearvelocity": body.LinearVelocity = ReadVector(sn); break; case "position": { float rotation = body.Rotation; Vector2 position = ReadVector(sn); body.SetTransformIgnoreContacts(ref position, rotation); } break; case "userdata": body.UserData = ReadSimpleType(sn, null, false); break; case "fixtures": { foreach (XMLFragmentElement v in sn.Elements) { Fixture blueprint = _fixtures[int.Parse(v.Value)]; Fixture f = new Fixture(body, blueprint.Shape, blueprint.CollisionCategories); f.Restitution = blueprint.Restitution; f.UserData = blueprint.UserData; f.Friction = blueprint.Friction; f.CollidesWith = blueprint.CollidesWith; f.CollisionGroup = blueprint.CollisionGroup; } break; } } } _bodies.Add(body); } } } foreach (XMLFragmentElement jointElement in root.Elements) { if (jointElement.Name.ToLower() == "joints") { foreach (XMLFragmentElement n in jointElement.Elements) { Joint joint; if (n.Name.ToLower() != "joint") throw new Exception(); JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true); int bodyAIndex = -1, bodyBIndex = -1; bool collideConnected = false; object userData = null; foreach (XMLFragmentElement sn in n.Elements) { switch (sn.Name.ToLower()) { case "bodya": bodyAIndex = int.Parse(sn.Value); break; case "bodyb": bodyBIndex = int.Parse(sn.Value); break; case "collideconnected": collideConnected = bool.Parse(sn.Value); break; case "userdata": userData = ReadSimpleType(sn, null, false); break; } } Body bodyA = _bodies[bodyAIndex]; Body bodyB = _bodies[bodyBIndex]; switch (type) { case JointType.Distance: joint = new DistanceJoint(); break; case JointType.Friction: joint = new FrictionJoint(); break; case JointType.Line: joint = new LineJoint(); break; case JointType.Prismatic: joint = new PrismaticJoint(); break; case JointType.Pulley: joint = new PulleyJoint(); break; case JointType.Revolute: joint = new RevoluteJoint(); break; case JointType.Weld: joint = new WeldJoint(); break; case JointType.Rope: joint = new RopeJoint(); break; case JointType.Angle: joint = new AngleJoint(); break; case JointType.Slider: joint = new SliderJoint(); break; case JointType.Gear: throw new Exception("GearJoint is not supported."); default: throw new Exception("Invalid or unsupported joint."); } joint.CollideConnected = collideConnected; joint.UserData = userData; joint.BodyA = bodyA; joint.BodyB = bodyB; _joints.Add(joint); world.AddJoint(joint); foreach (XMLFragmentElement sn in n.Elements) { // check for specific nodes switch (type) { case JointType.Distance: { switch (sn.Name.ToLower()) { case "dampingratio": ((DistanceJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((DistanceJoint)joint).Frequency = float.Parse(sn.Value); break; case "length": ((DistanceJoint)joint).Length = float.Parse(sn.Value); break; case "localanchora": ((DistanceJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((DistanceJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Friction: { switch (sn.Name.ToLower()) { case "localanchora": ((FrictionJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((FrictionJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxforce": ((FrictionJoint)joint).MaxForce = float.Parse(sn.Value); break; case "maxtorque": ((FrictionJoint)joint).MaxTorque = float.Parse(sn.Value); break; } } break; case JointType.Line: { switch (sn.Name.ToLower()) { case "enablemotor": ((LineJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((LineJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((LineJoint)joint).LocalAnchorB = ReadVector(sn); break; case "motorspeed": ((LineJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "dampingratio": ((LineJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "maxmotortorque": ((LineJoint)joint).MaxMotorTorque = float.Parse(sn.Value); break; case "frequencyhz": ((LineJoint)joint).Frequency = float.Parse(sn.Value); break; case "localxaxis": ((LineJoint)joint).LocalXAxis = ReadVector(sn); break; } } break; case JointType.Prismatic: { switch (sn.Name.ToLower()) { case "enablelimit": ((PrismaticJoint)joint).LimitEnabled = bool.Parse(sn.Value); break; case "enablemotor": ((PrismaticJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((PrismaticJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PrismaticJoint)joint).LocalAnchorB = ReadVector(sn); break; case "local1axis1": ((PrismaticJoint)joint).LocalXAxis1 = ReadVector(sn); break; case "maxmotorforce": ((PrismaticJoint)joint).MaxMotorForce = float.Parse(sn.Value); break; case "motorspeed": ((PrismaticJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "lowertranslation": ((PrismaticJoint)joint).LowerLimit = float.Parse(sn.Value); break; case "uppertranslation": ((PrismaticJoint)joint).UpperLimit = float.Parse(sn.Value); break; case "referenceangle": ((PrismaticJoint)joint).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Pulley: { switch (sn.Name.ToLower()) { case "groundanchora": ((PulleyJoint)joint).GroundAnchorA = ReadVector(sn); break; case "groundanchorb": ((PulleyJoint)joint).GroundAnchorB = ReadVector(sn); break; case "lengtha": ((PulleyJoint)joint).LengthA = float.Parse(sn.Value); break; case "lengthb": ((PulleyJoint)joint).LengthB = float.Parse(sn.Value); break; case "localanchora": ((PulleyJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((PulleyJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxlengtha": ((PulleyJoint)joint).MaxLengthA = float.Parse(sn.Value); break; case "maxlengthb": ((PulleyJoint)joint).MaxLengthB = float.Parse(sn.Value); break; case "ratio": ((PulleyJoint)joint).Ratio = float.Parse(sn.Value); break; } } break; case JointType.Revolute: { switch (sn.Name.ToLower()) { case "enablelimit": ((RevoluteJoint)joint).LimitEnabled = bool.Parse(sn.Value); break; case "enablemotor": ((RevoluteJoint)joint).MotorEnabled = bool.Parse(sn.Value); break; case "localanchora": ((RevoluteJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RevoluteJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxmotortorque": ((RevoluteJoint)joint).MaxMotorTorque = float.Parse(sn.Value); break; case "motorspeed": ((RevoluteJoint)joint).MotorSpeed = float.Parse(sn.Value); break; case "lowerangle": ((RevoluteJoint)joint).LowerLimit = float.Parse(sn.Value); break; case "upperangle": ((RevoluteJoint)joint).UpperLimit = float.Parse(sn.Value); break; case "referenceangle": ((RevoluteJoint)joint).ReferenceAngle = float.Parse(sn.Value); break; } } break; case JointType.Weld: { switch (sn.Name.ToLower()) { case "localanchora": ((WeldJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((WeldJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; case JointType.Rope: { switch (sn.Name.ToLower()) { case "localanchora": ((RopeJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((RopeJoint)joint).LocalAnchorB = ReadVector(sn); break; case "maxlength": ((RopeJoint)joint).MaxLength = float.Parse(sn.Value); break; } } break; case JointType.Gear: throw new Exception("Gear joint is unsupported"); case JointType.Angle: { switch (sn.Name.ToLower()) { case "biasfactor": ((AngleJoint)joint).BiasFactor = float.Parse(sn.Value); break; case "maximpulse": ((AngleJoint)joint).MaxImpulse = float.Parse(sn.Value); break; case "softness": ((AngleJoint)joint).Softness = float.Parse(sn.Value); break; case "targetangle": ((AngleJoint)joint).TargetAngle = float.Parse(sn.Value); break; } } break; case JointType.Slider: { switch (sn.Name.ToLower()) { case "dampingratio": ((SliderJoint)joint).DampingRatio = float.Parse(sn.Value); break; case "frequencyhz": ((SliderJoint)joint).Frequency = float.Parse(sn.Value); break; case "maxlength": ((SliderJoint)joint).MaxLength = float.Parse(sn.Value); break; case "minlength": ((SliderJoint)joint).MinLength = float.Parse(sn.Value); break; case "localanchora": ((SliderJoint)joint).LocalAnchorA = ReadVector(sn); break; case "localanchorb": ((SliderJoint)joint).LocalAnchorB = ReadVector(sn); break; } } break; } } } } } }
private void CreateJoints(World world) { const float dampingRatio = 1; const float frequency = 25; //head -> body DistanceJoint jHeadBody = new DistanceJoint(_head.Body, _body[0].Body, new Vector2(0, -1), new Vector2(0, 2)); jHeadBody.CollideConnected = true; jHeadBody.DampingRatio = dampingRatio; jHeadBody.Frequency = frequency; jHeadBody.Length = 0.025f; world.AddJoint(jHeadBody); //lowerLeftArm -> upperLeftArm DistanceJoint jLeftArm = new DistanceJoint(_lowerLeftArm[0].Body, _upperLeftArm[0].Body, new Vector2(0, 1), new Vector2(0, -1)); jLeftArm.CollideConnected = true; jLeftArm.DampingRatio = dampingRatio; jLeftArm.Frequency = frequency; jLeftArm.Length = 0.02f; world.AddJoint(jLeftArm); //upperLeftArm -> body DistanceJoint jLeftArmBody = new DistanceJoint(_upperLeftArm[0].Body, _body[0].Body, new Vector2(0, 1), new Vector2(-1, 1.5f)); jLeftArmBody.CollideConnected = true; jLeftArmBody.DampingRatio = dampingRatio; jLeftArmBody.Frequency = frequency; jLeftArmBody.Length = 0.02f; world.AddJoint(jLeftArmBody); //lowerRightArm -> upperRightArm DistanceJoint jRightArm = new DistanceJoint(_lowerRightArm[0].Body, _upperRightArm[0].Body, new Vector2(0, 1), new Vector2(0, -1)); jRightArm.CollideConnected = true; jRightArm.DampingRatio = dampingRatio; jRightArm.Frequency = frequency; jRightArm.Length = 0.02f; world.AddJoint(jRightArm); //upperRightArm -> body DistanceJoint jRightArmBody = new DistanceJoint(_upperRightArm[0].Body, _body[0].Body, new Vector2(0, 1), new Vector2(1, 1.5f)); jRightArmBody.CollideConnected = true; jRightArmBody.DampingRatio = dampingRatio; jRightArmBody.Frequency = 25; jRightArmBody.Length = 0.02f; world.AddJoint(jRightArmBody); //lowerLeftLeg -> upperLeftLeg DistanceJoint jLeftLeg = new DistanceJoint(_lowerLeftLeg[0].Body, _upperLeftLeg[0].Body, new Vector2(0, 1.1f), new Vector2(0, -1)); jLeftLeg.CollideConnected = true; jLeftLeg.DampingRatio = dampingRatio; jLeftLeg.Frequency = frequency; jLeftLeg.Length = 0.05f; world.AddJoint(jLeftLeg); //upperLeftLeg -> body DistanceJoint jLeftLegBody = new DistanceJoint(_upperLeftLeg[0].Body, _body[0].Body, new Vector2(0, 1.1f), new Vector2(-0.8f, -1.9f)); jLeftLegBody.CollideConnected = true; jLeftLegBody.DampingRatio = dampingRatio; jLeftLegBody.Frequency = frequency; jLeftLegBody.Length = 0.02f; world.AddJoint(jLeftLegBody); //lowerRightleg -> upperRightleg DistanceJoint jRightLeg = new DistanceJoint(_lowerRightLeg[0].Body, _upperRightLeg[0].Body, new Vector2(0, 1.1f), new Vector2(0, -1)); jRightLeg.CollideConnected = true; jRightLeg.DampingRatio = dampingRatio; jRightLeg.Frequency = frequency; jRightLeg.Length = 0.05f; world.AddJoint(jRightLeg); //upperRightleg -> body DistanceJoint jRightLegBody = new DistanceJoint(_upperRightLeg[0].Body, _body[0].Body, new Vector2(0, 1.1f), new Vector2(0.8f, -1.9f)); jRightLegBody.CollideConnected = true; jRightLegBody.DampingRatio = dampingRatio; jRightLegBody.Frequency = frequency; jRightLegBody.Length = 0.02f; world.AddJoint(jRightLegBody); }
public WebOfGoo(World world, Vector2 position, float radius, int rings, int sides) { _ringBodys = new List<List<Body>>(rings); _ringJoints = new List<DistanceJoint>(); for (int i = 1; i < rings; i++) { Vertices vertices = PolygonTools.CreateCircle(i * 2.9f, sides); vertices.Translate(ref position); List<Body> bodies = new List<Body>(sides); //Create the first goo Body previous = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[0]); previous.BodyType = BodyType.Dynamic; bodies.Add(previous); //Connect the first goo to the next for (int j = 1; j < vertices.Count; j++) { Body current = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[j]); current.BodyType = BodyType.Dynamic; DistanceJoint joint = new DistanceJoint(previous, current, Vector2.Zero, Vector2.Zero); joint.Frequency = 4.0f; joint.DampingRatio = 0.5f; joint.Breakpoint = Breakpoint; world.AddJoint(joint); _ringJoints.Add(joint); previous = current; bodies.Add(current); } //Connect the first and the last goo DistanceJoint jointClose = new DistanceJoint(bodies[0], bodies[bodies.Count - 1], Vector2.Zero, Vector2.Zero); jointClose.Frequency = 4.0f; jointClose.DampingRatio = 0.5f; jointClose.Breakpoint = Breakpoint; world.AddJoint(jointClose); _ringJoints.Add(jointClose); _ringBodys.Add(bodies); } //Create an outer ring Vertices frame = PolygonTools.CreateCircle(rings * 2.9f - 0.9f, sides); frame.Translate(ref position); Body anchor = new Body(world, position); anchor.BodyType = BodyType.Static; //Attach the outer ring to the anchor for (int i = 0; i < _ringBodys[rings - 2].Count; i++) { DistanceJoint joint = new DistanceJoint(anchor, _ringBodys[rings - 2][i], frame[i], _ringBodys[rings - 2][i].Position, true); joint.Frequency = 8.0f; joint.DampingRatio = 0.5f; joint.Breakpoint = Breakpoint; world.AddJoint(joint); _ringJoints.Add(joint); } //Interconnect the rings for (int i = 1; i < _ringBodys.Count; i++) { for (int j = 0; j < sides; j++) { DistanceJoint joint = new DistanceJoint(_ringBodys[i - 1][j], _ringBodys[i][j], Vector2.Zero, Vector2.Zero); joint.Frequency = 4.0f; joint.DampingRatio = 0.5f; joint.Breakpoint = Breakpoint; world.AddJoint(joint); _ringJoints.Add(joint); } } _link = new Sprite(ContentWrapper.GetTexture("Link")); _goo = new Sprite(ContentWrapper.GetTexture("Goo")); }
private void CreateChassis(World world, Vector2 pivot, AssetCreator assets) { { PolygonShape shape = new PolygonShape(1f); shape.Vertices = PolygonTools.CreateRectangle(2.5f, 1.0f); _body = new Sprite(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(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, _wheel.GetLocalPoint(_chassis.Position), Vector2.Zero); _motorJoint.CollideConnected = false; _motorJoint.MotorSpeed = _motorSpeed; _motorJoint.MaxMotorTorque = 400f; _motorJoint.MotorEnabled = true; world.AddJoint(_motorJoint); } }
private void CreateLeg(World world, float direction, Vector2 wheelAnchor, int index) { Vector2 p1 = new Vector2(5.4f * direction, 6.1f); Vector2 p2 = new Vector2(7.2f * direction, 1.2f); Vector2 p3 = new Vector2(4.3f * direction, 1.9f); Vector2 p4 = new Vector2(3.1f * direction, -0.8f); Vector2 p5 = new Vector2(6.0f * direction, -1.5f); Vector2 p6 = new Vector2(2.5f * direction, -3.7f); PolygonShape shoulderPolygon; PolygonShape legPolygon; Vertices vertices = new Vertices(3); if (direction < 0f) { vertices.Add(p1); vertices.Add(p2); vertices.Add(p3); shoulderPolygon = new PolygonShape(vertices, 1); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; legPolygon = new PolygonShape(vertices, 2); } else { vertices.Add(p1); vertices.Add(p3); vertices.Add(p2); shoulderPolygon = new PolygonShape(vertices, 1); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; legPolygon = new PolygonShape(vertices, 2); } Body leg = BodyFactory.CreateBody(world); leg.BodyType = BodyType.Dynamic; leg.Position = _position; leg.AngularDamping = 10f; if (direction < 0f) _leftLegs[index] = leg; else _rightLegs[index] = leg; Body shoulder = BodyFactory.CreateBody(world); shoulder.BodyType = BodyType.Dynamic; shoulder.Position = p4 + _position; shoulder.AngularDamping = 10f; if (direction < 0f) _leftShoulders[index] = shoulder; else _rightShoulders[index] = shoulder; Fixture f1 = leg.CreateFixture(shoulderPolygon); f1.CollisionGroup = -1; Fixture f2 = shoulder.CreateFixture(legPolygon); f2.CollisionGroup = -1; // Using a soft distanceraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. DistanceJoint djd = new DistanceJoint(leg, shoulder, leg.GetLocalPoint(p2 + _position), shoulder.GetLocalPoint(p5 + _position)); djd.DampingRatio = 0.5f; djd.Frequency = 10f; world.AddJoint(djd); _walkerJoints.Add(djd); DistanceJoint djd2 = new DistanceJoint(leg, shoulder, leg.GetLocalPoint(p3 + _position), shoulder.GetLocalPoint(p4 + _position)); djd2.DampingRatio = 0.5f; djd2.Frequency = 10f; world.AddJoint(djd2); _walkerJoints.Add(djd2); DistanceJoint djd3 = new DistanceJoint(leg, _wheel, leg.GetLocalPoint(p3 + _position), _wheel.GetLocalPoint(wheelAnchor + _position)); djd3.DampingRatio = 0.5f; djd3.Frequency = 10f; world.AddJoint(djd3); _walkerJoints.Add(djd3); DistanceJoint djd4 = new DistanceJoint(shoulder, _wheel, shoulder.GetLocalPoint(p6 + _position), _wheel.GetLocalPoint(wheelAnchor + _position)); djd4.DampingRatio = 0.5f; djd4.Frequency = 10f; world.AddJoint(djd4); _walkerJoints.Add(djd4); Vector2 anchor = p4 - new Vector2(0f, -0.8f); RevoluteJoint rjd = new RevoluteJoint(shoulder, _chassis, shoulder.GetLocalPoint(_chassis.GetWorldPoint(anchor)), anchor); world.AddJoint(rjd); }
Joint j2b2Joint(World world, JObject jointValue) { Joint joint = null; int bodyIndexA = (int)jointValue["bodyA"]; int bodyIndexB = (int)jointValue["bodyB"]; if (bodyIndexA >= m_bodies.Count || bodyIndexB >= m_bodies.Count) return null; // set features common to all joints var bodyA = m_bodies[bodyIndexA]; var bodyB = m_bodies[bodyIndexB]; var collideConnected = jointValue["collideConnected"] == null ? false : (bool)jointValue["collideConnected"]; // keep these in scope after the if/else below RevoluteJoint revoluteDef; PrismaticJoint prismaticDef; DistanceJoint distanceDef; PulleyJoint pulleyDef; FixedMouseJoint mouseDef; GearJoint gearDef; WheelJoint wheelDef; WeldJoint weldDef; FrictionJoint frictionDef; RopeJoint ropeDef; MotorJoint motorDef; Vector2 mouseJointTarget = new Vector2(0, 0); string type = jointValue["type"].ToString() == null ? "" : jointValue["type"].ToString(); if (type == "revolute") { joint = revoluteDef = JointFactory.CreateRevoluteJoint(world, bodyA, bodyB, jsonToVec("anchorB", jointValue)); revoluteDef.LocalAnchorA = jsonToVec("anchorA", jointValue); revoluteDef.LocalAnchorB = jsonToVec("anchorB", jointValue); revoluteDef.ReferenceAngle = jsonToFloat("refAngle", jointValue); revoluteDef.LimitEnabled = jointValue["enableLimit"] == null ? false : (bool)jointValue["enableLimit"]; revoluteDef.LowerLimit = jsonToFloat("lowerLimit", jointValue); revoluteDef.UpperLimit = jsonToFloat("upperLimit", jointValue); revoluteDef.MotorEnabled = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; revoluteDef.MotorSpeed = jsonToFloat("motorSpeed", jointValue); revoluteDef.MaxMotorTorque = jsonToFloat("maxMotorTorque", jointValue); } else if (type == "prismatic") { var localAxis = new Vector2(); var localAnchorA = jsonToVec("anchorA", jointValue); var localAnchorB = jsonToVec("anchorB", jointValue); if (jointValue["localAxisA"] != null) localAxis = jsonToVec("localAxisA", jointValue); else localAxis = jsonToVec("localAxis1", jointValue); joint = prismaticDef = JointFactory.CreatePrismaticJoint(world, bodyA, bodyB, localAnchorB, localAxis); prismaticDef.LocalAnchorA = localAnchorA; prismaticDef.ReferenceAngle = jsonToFloat("refAngle", jointValue); prismaticDef.LimitEnabled = (bool)jointValue["enableLimit"]; prismaticDef.LowerLimit = jsonToFloat("lowerLimit", jointValue); prismaticDef.UpperLimit = jsonToFloat("upperLimit", jointValue); prismaticDef.MotorEnabled = (bool)jointValue["enableMotor"]; prismaticDef.MotorSpeed = jsonToFloat("motorSpeed", jointValue); prismaticDef.MaxMotorForce = jsonToFloat("maxMotorForce", jointValue); } else if (type == "distance") { joint = distanceDef = JointFactory.CreateDistanceJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue)); distanceDef.LocalAnchorA = (jsonToVec("anchorA", jointValue)); distanceDef.LocalAnchorB = (jsonToVec("anchorB", jointValue)); distanceDef.Length = jsonToFloat("length", jointValue); distanceDef.Frequency = jsonToFloat("frequency", jointValue); distanceDef.DampingRatio = jsonToFloat("dampingRatio", jointValue); } else if (type == "pulley") { joint = pulleyDef = JointFactory.CreatePulleyJoint(world, bodyA, bodyB, jsonToVec("groundAnchorA", jointValue), jsonToVec("groundAnchorB", jointValue), jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue), jsonToFloat("ratio", jointValue)); pulleyDef.LengthA = jsonToFloat("lengthA", jointValue); pulleyDef.LengthB = jsonToFloat("lengthB", jointValue); //pulleyDef.Ratio = jsonToFloat("ratio", jointValue); } else if (type == "mouse") { joint = mouseDef = JointFactory.CreateFixedMouseJoint(world, bodyA, jsonToVec("target", jointValue)); //mouseJointTarget = jsonToVec("target", jointValue); //mouseDef.target.set(jsonToVec("anchorB", jointValue));// alter after creating joint mouseDef.MaxForce = jsonToFloat("maxForce", jointValue); mouseDef.Frequency = jsonToFloat("frequency", jointValue); mouseDef.DampingRatio = jsonToFloat("dampingRatio", jointValue); } // Gear joints are apparently not implemented in JBox2D yet, but // when they are, commenting out the following section should work. else if (type == "gear") { int jointIndex1 = (int)jointValue["joint1"]; int jointIndex2 = (int)jointValue["joint2"]; var joint1 = m_joints[jointIndex1]; var joint2 = m_joints[jointIndex2]; var ratio = jsonToFloat("ratio", jointValue); joint = gearDef = JointFactory.CreateGearJoint(world, joint1, joint2, ratio); } // Wheel joints are apparently not implemented in JBox2D yet, but // when they are, commenting out the following section should work. else if (type == "wheel") { var localAnchorA = jsonToVec("anchorA", jointValue); var localAnchorB = (jsonToVec("anchorB", jointValue)); var localAxisA = (jsonToVec("localAxisA", jointValue)); var enableMotor = jointValue["enableMotor"] == null ? false : (bool)jointValue["enableMotor"]; var motorSpeed = jsonToFloat("motorSpeed", jointValue); var maxMotorTorque = jsonToFloat("maxMotorTorque", jointValue); var frequencyHz = jsonToFloat("springFrequency", jointValue); var dampingRatio = jsonToFloat("springDampingRatio", jointValue); joint = wheelDef = JointFactory.CreateWheelJoint(world, bodyA, bodyB, localAnchorB, localAxisA); wheelDef.LocalAnchorA = localAnchorA; wheelDef.LocalAnchorB = localAnchorB; wheelDef.MotorEnabled = enableMotor; wheelDef.MotorSpeed = motorSpeed; wheelDef.SpringFrequencyHz = frequencyHz; wheelDef.MaxMotorTorque = maxMotorTorque; wheelDef.SpringDampingRatio = dampingRatio; } else if (type == "weld") { joint = weldDef = JointFactory.CreateWeldJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue)); weldDef.LocalAnchorA = jsonToVec("anchorA", jointValue); weldDef.LocalAnchorB = jsonToVec("anchorB", jointValue); weldDef.FrequencyHz = jsonToFloat("frequency", jointValue); weldDef.DampingRatio = jsonToFloat("dampingRatio", jointValue); } else if (type == "friction") { joint = frictionDef = JointFactory.CreateFrictionJoint(world, bodyA, bodyB, jsonToVec("anchorA", jointValue)); frictionDef.LocalAnchorB = jsonToVec("anchorB", jointValue); frictionDef.MaxForce = jsonToFloat("maxForce", jointValue); frictionDef.MaxTorque = jsonToFloat("maxTorque", jointValue); } else if (type == "rope") { joint = ropeDef = new RopeJoint(bodyA, bodyB, jsonToVec("anchorA", jointValue), jsonToVec("anchorB", jointValue)); world.AddJoint(joint); ropeDef.MaxLength = jsonToFloat("maxLength", jointValue); } else if (type == "motor") { var maxForce = jsonToFloat("maxForce", jointValue); var maxMotorTorque = jsonToFloat("maxTorque", jointValue); var angularOffset = jsonToFloat("refAngle", jointValue); joint = motorDef = new MotorJoint(bodyA, bodyB); world.AddJoint(joint); motorDef.LinearOffset = jsonToVec("anchorA", jointValue); motorDef.MaxForce = maxForce; motorDef.MaxTorque = maxMotorTorque; motorDef.AngularOffset = angularOffset; } if (null != joint) { // set features common to all joints /*joint.BodyA = bodyA; joint.BodyB = bodyB;*/ joint.CollideConnected = collideConnected; string jointName = jointValue["name"] == null ? "" : jointValue["name"].ToString(); if (jointName != "") { SetJointName(joint, jointName); } } return joint; }
public static RopeJoint CreateRopeJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, bool useWorldCoordinates = false) { RopeJoint ropeJoint = new RopeJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates); world.AddJoint(ropeJoint); return ropeJoint; }
public static RevoluteJoint CreateRevoluteJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, bool useWorldCoordinates = false) { RevoluteJoint joint = new RevoluteJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates); world.AddJoint(joint); return joint; }
public static PrismaticJoint CreatePrismaticJoint(World world, Body bodyA, Body bodyB, Vector2 anchor, Vector2 axis, bool useWorldCoordinates = false) { PrismaticJoint joint = new PrismaticJoint(bodyA, bodyB, anchor, axis, useWorldCoordinates); world.AddJoint(joint); return joint; }
public static MotorJoint CreateMotorJoint(World world, Body bodyA, Body bodyB, bool useWorldCoordinates = false) { MotorJoint joint = new MotorJoint(bodyA, bodyB, useWorldCoordinates); world.AddJoint(joint); return joint; }
public static GearJoint CreateGearJoint(World world, Body bodyA, Body bodyB, Joint jointA, Joint jointB, float ratio) { GearJoint gearJoint = new GearJoint(bodyA, bodyB, jointA, jointB, ratio); world.AddJoint(gearJoint); return gearJoint; }
public static FrictionJoint CreateFrictionJoint(World world, Body bodyA, Body bodyB, Vector2 anchor, bool useWorldCoordinates = false) { FrictionJoint frictionJoint = new FrictionJoint(bodyA, bodyB, anchor, useWorldCoordinates); world.AddJoint(frictionJoint); return frictionJoint; }
public static FixedMouseJoint CreateFixedMouseJoint(World world, Body body, Vector2 worldAnchor) { FixedMouseJoint joint = new FixedMouseJoint(body, worldAnchor); world.AddJoint(joint); return joint; }