protected void OnMove(IGameObject sender) { //IGameObjects use a position located in the top-left corner of the object //and use world units, not sim units, so we need to convert both. if (Bodies != null) { Vector2 position = ConvertUnits.ToSimUnits(sender.Position); foreach (Body body in Bodies.Values) { if (!body.IsStatic) { FarseerUserData userData = (FarseerUserData)body.UserData; Vector2 myPosition = position - userData.TopLeftOffset + userData.Offset; //If the position is different from the current position of the PhysicsBody then we need to //change the actual position of the PhysicsBody. if (Math.Abs(myPosition.X - body.Position.X) > 0.0001f || Math.Abs(myPosition.Y - body.Position.Y) > 0.0001f) { //Change the position of the PhysicsBody... body.Position = myPosition; } } } } }
protected void OnBodyRemoved(Body body) { var userData = new FarseerUserData(); if (body.UserData != null) userData = (FarseerUserData) body.UserData; if (bodyDictionary.ContainsKey(userData.Name)) bodyDictionary.Remove(userData.Name); }
protected void OnBodyRemoved(Body body) { var userData = new FarseerUserData(); if (body.UserData != null) { userData = (FarseerUserData)body.UserData; } if (bodyDictionary.ContainsKey(userData.Name)) { bodyDictionary.Remove(userData.Name); } }
public override void Update(GameTime gameTime) { if (Paused) { return; } //Update the position of our Parent to the current position of the PhysicsBody. //WTo do this we'll track down the first 'Primary' body that we can find. Body primary = null; if (Bodies != null) { primary = GetPrimaryBody(); if (primary != null && (oldPosition.X != primary.Position.X || oldPosition.Y != primary.Position.Y)) { Vector2 position = primary.Position; FarseerUserData userData = (FarseerUserData)primary.UserData; position += userData.TopLeftOffset; position = ConvertUnits.ToDisplayUnits(position); Parent.Position = position; oldPosition = primary.Position; Vector2 newVelocity = primary.LinearVelocity; if (Math.Abs(primary.LinearVelocity.X) > MaxVelocity) { newVelocity.X = MaxVelocity; } if (Math.Abs(primary.LinearVelocity.Y) > MaxVelocity) { newVelocity.Y = MaxVelocity; } primary.LinearVelocity = newVelocity; } } base.Update(gameTime); }
public static Body DeserializeBody(XElement element, IPhysicsManager physicsManager, IPhysicsComponent parent) { Body body = null; FarseerUserData userData = new FarseerUserData(); //Read the shape used for this body. BodyType bodyType = BodyType.Static; if (element.Attribute("bodyType") != null) { if (element.Attribute("bodyType").Value.StartsWith("d", StringComparison.InvariantCultureIgnoreCase)) { bodyType = BodyType.Dynamic; } else if (element.Attribute("bodyType").Value.StartsWith("s", StringComparison.InvariantCultureIgnoreCase)) { bodyType = BodyType.Static; } else if (element.Attribute("bodyType").Value.StartsWith("k", StringComparison.InvariantCultureIgnoreCase)) { bodyType = BodyType.Kinematic; } } bool primary = false; if (element.Attribute("primary") != null) { bool.TryParse(element.Attribute("primary").Value, out primary); } Shape shape = null; XElement shapeElement = element.Element("shape"); if (shapeElement != null && shapeElement.Attribute("type") != null) { if (shapeElement.Attribute("type").Value.StartsWith("b", StringComparison.InvariantCultureIgnoreCase)) { shape = DeserializeBoxShape(shapeElement); } else if (shapeElement.Attribute("type").Value.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) { shape = DeserializeCircleShape(shapeElement); } else if (shapeElement.Attribute("type").Value.StartsWith("p", StringComparison.InvariantCultureIgnoreCase)) { shape = DeserializePolygonShape(shapeElement); } } if (shape != null) { string name; body = BodyFactory.CreateBody(physicsManager.PhysicsWorld); body.BodyType = bodyType; Vector2 offset = Vector2.Zero; float friction = 0.5f, linearDamping = 5f, restitution = 0f, mass = 1f; bool fixedRotation = false; if (element.Element("friction") != null) { float.TryParse(element.Element("friction").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out friction); } if (element.Element("linearDamping") != null) { float.TryParse(element.Element("linearDamping").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out linearDamping); } if (element.Element("restitution") != null) { float.TryParse(element.Element("restitution").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out restitution); } if (element.Element("mass") != null) { float.TryParse(element.Element("mass").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out mass); } if (element.Element("fixedRotation") != null) { bool.TryParse(element.Element("fixedRotation").Value, out fixedRotation); } if (element.Element("offset") != null) { offset = offset.DeserializeOffset(element.Element("offset")); } body.CreateFixture(shape); body.Friction = friction; body.LinearDamping = linearDamping; body.Restitution = restitution; body.Mass = mass; body.FixedRotation = fixedRotation; name = element.Attribute("name") != null?element.Attribute("name").Value : body.BodyId.ToString(); userData.TopLeftOffset = ConvertUnits.GetTopLeftCorner(body.FixtureList[0], 0); userData.Name = name; userData.Primary = primary; userData.Owner = parent; userData.Offset = offset; body.UserData = userData; } return(body); }
public static Body DeserializeBody(XElement element, IPhysicsManager physicsManager, IPhysicsComponent parent) { Body body = null; FarseerUserData userData = new FarseerUserData(); //Read the shape used for this body. BodyType bodyType = BodyType.Static; if (element.Attribute("bodyType") != null) { if (element.Attribute("bodyType").Value.StartsWith("d", StringComparison.InvariantCultureIgnoreCase)) bodyType = BodyType.Dynamic; else if (element.Attribute("bodyType").Value.StartsWith("s", StringComparison.InvariantCultureIgnoreCase)) bodyType = BodyType.Static; else if (element.Attribute("bodyType").Value.StartsWith("k", StringComparison.InvariantCultureIgnoreCase)) bodyType = BodyType.Kinematic; } bool primary = false; if (element.Attribute("primary") != null) bool.TryParse(element.Attribute("primary").Value, out primary); Shape shape = null; XElement shapeElement = element.Element("shape"); if (shapeElement != null && shapeElement.Attribute("type") != null) { if (shapeElement.Attribute("type").Value.StartsWith("b", StringComparison.InvariantCultureIgnoreCase)) shape = DeserializeBoxShape(shapeElement); else if (shapeElement.Attribute("type").Value.StartsWith("c", StringComparison.InvariantCultureIgnoreCase)) shape = DeserializeCircleShape(shapeElement); else if (shapeElement.Attribute("type").Value.StartsWith("p", StringComparison.InvariantCultureIgnoreCase)) shape = DeserializePolygonShape(shapeElement); } if (shape != null) { string name; body = BodyFactory.CreateBody(physicsManager.PhysicsWorld); body.BodyType = bodyType; Vector2 offset = Vector2.Zero; float friction = 0.5f, linearDamping = 5f, restitution = 0f, mass = 1f; bool fixedRotation = false; if (element.Element("friction") != null) float.TryParse(element.Element("friction").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out friction); if (element.Element("linearDamping") != null) float.TryParse(element.Element("linearDamping").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out linearDamping); if (element.Element("restitution") != null) float.TryParse(element.Element("restitution").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out restitution); if (element.Element("mass") != null) float.TryParse(element.Element("mass").Value, NumberStyles.Float, CultureInfo.InvariantCulture, out mass); if (element.Element("fixedRotation") != null) bool.TryParse(element.Element("fixedRotation").Value, out fixedRotation); if (element.Element("offset") != null) offset = offset.DeserializeOffset(element.Element("offset")); body.CreateFixture(shape); body.Friction = friction; body.LinearDamping = linearDamping; body.Restitution = restitution; body.Mass = mass; body.FixedRotation = fixedRotation; name = element.Attribute("name") != null ? element.Attribute("name").Value : body.BodyId.ToString(); userData.TopLeftOffset = ConvertUnits.GetTopLeftCorner(body.FixtureList[0], 0); userData.Name = name; userData.Primary = primary; userData.Owner = parent; userData.Offset = offset; body.UserData = userData; } return body; }