public override void Initialize() { Parent.OnMove += OnMove; physicsManager = GameServiceManager.GetService <IPhysicsManager>(); foreach (var body in Bodies.Values) { body.Position = ConvertUnits.ToSimUnits(Parent.Position + ((FarseerUserData)body.UserData).Offset) - ((FarseerUserData)body.UserData).TopLeftOffset; body.Enabled = true; } physicsManager.PhysicsWorld.Step(0.000001f); foreach (var joint in copyJoints) { var data = joint.Value.UserData as FarseerJointUserData; var bodyA = !string.IsNullOrWhiteSpace(data.BodyAName) && Bodies.ContainsKey(data.BodyAName) ? Bodies[data.BodyAName] : null; var bodyB = !string.IsNullOrWhiteSpace(data.BodyBName) && Bodies.ContainsKey(data.BodyBName) ? Bodies[data.BodyBName] : null; var newJoint = FarseerDeserialization.CopyJoint(joint.Value, bodyA, bodyB, physicsManager.PhysicsWorld); physicsManager.PhysicsWorld.AddJoint(newJoint); Joints.Add(joint.Key, newJoint); } copyJoints.Clear(); base.Initialize(); }
public override void Deserialize(XElement element) { IPhysicsManager physicsManager = GameServiceManager.GetService <IPhysicsManager>(); if (physicsManager != null && element != null) { //Read in the max velocity value. float maxVelocity = float.PositiveInfinity; if (element.Element("maxVelocity") != null) { float.TryParse(element.Element("maxVelocity").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out maxVelocity); } MaxVelocity = ConvertUnits.ToSimUnits(maxVelocity); if (element.Element("bodies") != null) { Bodies = FarseerDeserialization.DeserializeBodies(element.Element("bodies"), physicsManager, this); } //Record the primary body primaryBody = GetPrimaryBody(); foreach (var body in Bodies.Values) { body.Enabled = false; body.Position = new Vector2(-10f, -10f); } //A single-body physics component only supports joints that require only a single body (for obvious reasons...) //But we'll expect the FarseerDeserialization class to handle any user error with this. if (element.Element("joints") != null) { this.Joints = FarseerDeserialization.DeserializeJoints(Parent, element.Element("joints"), physicsManager, Bodies); } } }