public override Joint createJoint() { var joint = new WeldJoint( bodyA, bodyB, ownerBodyAnchor * FSConvert.displayToSim, otherBodyAnchor * FSConvert.displayToSim ); joint.collideConnected = collideConnected; joint.dampingRatio = dampingRatio; joint.frequencyHz = frequencyHz; return joint; }
private CantileverTest() { Body ground; { ground = BodyFactory.CreateBody(World); EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape); } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 15.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-4.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); } prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(5.5f + 1.0f * i, 10.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); } prevBody = body; } } //Triangels Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); for (int i = 0; i < 2; ++i) { PolygonShape shape = new PolygonShape(vertices, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-8.0f + 8.0f * i, 12.0f); body.CreateFixture(shape); } //Circles for (int i = 0; i < 2; ++i) { CircleShape shape = new CircleShape(0.5f, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-6.0f + 6.0f * i, 10.0f); body.CreateFixture(shape); } }
private bool LoadJoint() { var otherObject = GameObject.Scene.GameObjects.FirstOrDefault(g => g.Id == OtherObjectId) as GameObject; if (otherObject == null) return false; var otherRigidBody = otherObject.Components.FirstOrDefault(c => c is RigidBodyFarseerComponent) as RigidBodyFarseerComponent; if (otherRigidBody == null) return false; if (!otherObject.IsLoaded) otherObject.Load(); if (!otherRigidBody.IsLoaded) return false; var myRigidBody = GameObject.Components.FirstOrDefault(c => c is RigidBodyFarseerComponent) as RigidBodyFarseerComponent; if (!myRigidBody.IsLoaded) myRigidBody.Load(); if (!myRigidBody.IsLoaded) return false; var world = GameObject.Scene.Physics.GetWorld() as World; if (world == null) throw new InvalidOperationException("Not a farseer physics world."); _joint = JointFactory.CreateWeldJoint(world, myRigidBody.RigidBody, otherRigidBody.RigidBody, Vector2.Zero); //world.AddJoint(_joint); return true; }
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 CantileverTest() { Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0)); { PolygonShape shape = new PolygonShape(20); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 0.125f); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, anchor, anchor, true); World.AddJoint(jd); prevBody = body; } } { PolygonShape shape = new PolygonShape(20f); shape.Vertices = PolygonTools.CreateRectangle(1f, 0.125f); Body prevBody = ground; for (int i = 0; i < 3; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.0f + 2.0f * i, 15.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 2.0f * i, 15.0f); WeldJoint jd = new WeldJoint(prevBody, body, anchor, anchor, true); jd.FrequencyHz = 5.0f; jd.DampingRatio = 0.7f; World.AddJoint(jd); prevBody = body; } } { PolygonShape shape = new PolygonShape(20f); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 0.125f); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-4.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, anchor, anchor, true); World.AddJoint(jd); } prevBody = body; } } { PolygonShape shape = new PolygonShape(20f); shape.Vertices = PolygonTools.CreateRectangle(0.5f, 0.125f); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(5.5f + 1.0f * i, 10.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); WeldJoint jd = new WeldJoint(prevBody, body, anchor, anchor, true); jd.FrequencyHz = 8.0f; jd.DampingRatio = 0.7f; World.AddJoint(jd); } prevBody = body; } } //Triangels Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); for (int i = 0; i < 2; ++i) { PolygonShape shape = new PolygonShape(vertices, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-8.0f + 8.0f * i, 12.0f); body.CreateFixture(shape); } //Circles for (int i = 0; i < 2; ++i) { CircleShape shape = new CircleShape(0.5f, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-6.0f + 6.0f * i, 10.0f); body.CreateFixture(shape); } }
public static WeldJoint CreateWeldJoint(World world, Body bodyA, Body bodyB, Vector2 anchorA, Vector2 anchorB, bool useWorldCoordinates = false) { WeldJoint weldJoint = new WeldJoint(bodyA, bodyB, anchorA, anchorB, useWorldCoordinates); world.AddJoint(weldJoint); return weldJoint; }
/// <summary> /// Creates a weld joint /// </summary> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="localAnchor"></param> /// <returns></returns> public static WeldJoint CreateWeldJoint(Body bodyA, Body bodyB, Vector2 localAnchor) { WeldJoint joint = new WeldJoint(bodyA, bodyB, bodyA.GetLocalPoint(localAnchor), bodyB.GetLocalPoint(localAnchor)); return joint; }
public static WeldJoint CreateWeldJoint(World world, Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB) { WeldJoint weldJoint = new WeldJoint(bodyA, bodyB, localAnchorA, localAnchorB); world.AddJoint(weldJoint); return weldJoint; }
bool OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact) { if (fixtureB.UserData as string == "Pattack") { attacked = true; fixtureB.Body.Friction = (Points / 100f); } if (fixtureB.UserData as string == "Player") { if (!weldedbool) { Vector2 weldA = new Vector2((float)ConvertUnits.ToSimUnits((float)Math.Cos(rotation - ((float)Math.PI / 2f))), (float)ConvertUnits.ToSimUnits((float)Math.Sin(rotation - ((float)Math.PI / 2f)))); Vector2 weldB = new Vector2((float)ConvertUnits.ToSimUnits((float)Math.Cos(fixtureB.Body.Rotation - ((float)Math.PI / 2f))), (float)ConvertUnits.ToSimUnits((float)Math.Sin(fixtureB.Body.Rotation - ((float)Math.PI / 2f)))); welded = new WeldJoint(fixtureA.Body, fixtureB.Body, weldA * 20f, //_bodyA.Body.LocalCenter weldB * 20f); //_bodyB.Body.LocalCenter level.AddJoint(welded); weldedbool = true; } return false; } return true; }
/// <summary> /// Creates a weld joint /// </summary> /// <param name="world"></param> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="localanchorB"></param> /// <returns></returns> public static WeldJoint CreateWeldJoint(Body bodyA, Body bodyB, Vector2 localanchorB) { Vector2 localanchorA = bodyA.GetLocalPoint(bodyB.GetWorldPoint(localanchorB)); WeldJoint joint = new WeldJoint(bodyA, bodyB, localanchorA, localanchorB); return joint; }
/// <summary> /// Adds a Physics Joint to the simulation /// </summary> /// <param name="joint"></param> public void AddPhysicsJoint(PhysicsJoint joint) { string bodyOne = joint.BodyOne; string bodyTwo = joint.BodyTwo; short collisionGroup = Convert.ToInt16(joint.CollisionGroup); bool isAngleSpringEnabled = joint.AngleSpringEnabled; float springConstant = (float)joint.AngleSpringConstant; float angleLowerLimit = (float)joint.AngleLowerLimit; float angleUpperLimit = (float)joint.AngleUpperLimit; Point center = joint.GetCenter(); xna.Vector2 ptCollisionCenter = new xna.Vector2((float)center.X, (float)center.Y); if (!PhysicsObjects.ContainsKey(bodyOne)) throw new Exception("Cannot add joint for an invalid BodyOne value of '" + bodyOne + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); if (!PhysicsObjects.ContainsKey(bodyTwo)) throw new Exception("Cannot add joint for an invalid BodyTwo value of '" + bodyTwo + "'. If using Behaviors, did you forgot to add a PhysicsObjectBehavior?"); Body body1 = PhysicsObjects[bodyOne].BodyObject; Body body2 = PhysicsObjects[bodyTwo].BodyObject; xna.Vector2 ptCollisionCenterA = _boundaryHelper.ScreenToWorld(ptCollisionCenter); xna.Vector2 ptCollisionCenterB = _boundaryHelper.ScreenToWorld(ptCollisionCenter); // account for offset within body ptCollisionCenterA -= body1.Position; ptCollisionCenterB -= body2.Position; // DEMO: (5) Create Joints if (joint.IsWeldJoint) { WeldJoint weldJoint = new WeldJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB); Simulator.AddJoint(weldJoint); } else if (joint.IsDistanceJoint) { DistanceJoint distanceJoint = new DistanceJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB); Simulator.AddJoint(distanceJoint); } else { RevoluteJoint revoluteJoint = new RevoluteJoint(body1, body2, ptCollisionCenterA, ptCollisionCenterB); Simulator.AddJoint(revoluteJoint); if (isAngleSpringEnabled) { AngleJoint aj = new AngleJoint(body1, body2); aj.TargetAngle = 0; aj.Softness = springConstant; Simulator.AddJoint(aj); } if (angleUpperLimit != -1 && angleLowerLimit != -1) { float upperAngle = (float)PhysicsUtilities.DegreesToRadians(angleUpperLimit); float lowerAngle = (float)PhysicsUtilities.DegreesToRadians(angleLowerLimit); revoluteJoint.LimitEnabled = true; revoluteJoint.LowerLimit = lowerAngle; revoluteJoint.UpperLimit = upperAngle; } } if (collisionGroup > 0) { foreach (Fixture f in PhysicsObjects[bodyOne].BodyObject.FixtureList) { f.CollisionGroup = collisionGroup; } foreach (Fixture f in PhysicsObjects[bodyTwo].BodyObject.FixtureList) { f.CollisionGroup = collisionGroup; } } // get rid of the UI representation of the joint joint.Visibility = Visibility.Collapsed; }
private bool ChainBallOnCollision(Fixture fixturea, Fixture fixtureb, Contact contact) { var crateBody = fixtureb.Body; var crate = crateBody.UserData as Crate; if (crate != null && !crate.IsStuck) { // The ball hit a crate // We make them stick together using a weld joint var positionDiff = _chainEndBall.Position - crateBody.Position; var joint = new WeldJoint(_chainEndBall, crateBody, Vector2.Zero, positionDiff); World.AddJoint(joint); crate.MagnetJoint = joint; } return true; }
public static Joint CopyJoint(Joint joint, Body bodyA, Body bodyB, World world) { Joint newJoint = null; switch (joint.JointType) { case JointType.Angle: newJoint = JointFactory.CreateAngleJoint(world, bodyA, bodyB); break; case JointType.Distance: newJoint = new DistanceJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.FixedAngle: newJoint = new FixedAngleJoint(bodyA); break; case JointType.FixedDistance: newJoint = new FixedDistanceJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.FixedFriction: newJoint = new FixedFrictionJoint(bodyA, bodyA.WorldCenter); break; case JointType.FixedPrismatic: var fpJoint = joint as FixedPrismaticJoint; var fpAxis = fpJoint.LocalXAxis1; newJoint = new FixedPrismaticJoint(bodyA, bodyA.WorldCenter, fpAxis); break; case JointType.FixedRevolute: newJoint = new FixedRevoluteJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.Friction: newJoint = new FrictionJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Line: var lineJoint = joint as LineJoint; var axis = lineJoint.LocalXAxis; newJoint = new LineJoint(bodyA, bodyB, bodyA.WorldCenter, axis); break; case JointType.Prismatic: var pJoint = joint as PrismaticJoint; var pAxis = pJoint.LocalXAxis1; newJoint = new PrismaticJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, pAxis); ((PrismaticJoint)newJoint).LimitEnabled = pJoint.LimitEnabled; ((PrismaticJoint)newJoint).MotorEnabled = pJoint.MotorEnabled; ((PrismaticJoint)newJoint).MaxMotorForce = pJoint.MaxMotorForce; ((PrismaticJoint)newJoint).MotorSpeed = pJoint.MotorSpeed; ((PrismaticJoint)newJoint).LowerLimit = pJoint.LowerLimit; ((PrismaticJoint)newJoint).UpperLimit = pJoint.UpperLimit; ((PrismaticJoint)newJoint).ReferenceAngle = pJoint.ReferenceAngle; ((PrismaticJoint)newJoint).LocalXAxis1 = pJoint.LocalXAxis1; break; case JointType.Pulley: var pulleyJoint = joint as PulleyJoint; var ratio = pulleyJoint.Ratio; newJoint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, bodyA.WorldCenter, bodyB.WorldCenter, ratio); break; case JointType.Revolute: newJoint = new RevoluteJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Slider: var sliderJoint = joint as SliderJoint; var minLength = sliderJoint.MinLength; var maxLength = sliderJoint.MaxLength; newJoint = new SliderJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, minLength, maxLength); break; case JointType.Weld: newJoint = new WeldJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; } var data = new FarseerJointUserData(); data.BodyAName = ((FarseerJointUserData) joint.UserData).BodyAName; data.BodyBName = ((FarseerJointUserData) joint.UserData).BodyBName; joint.UserData = data; return newJoint; }