public static Vector2 CalculateOrigin(Body body) { Vector2 origin = Vector2.Zero; if (body != null) { Vector2 leftBound = new Vector2(float.MaxValue); Transform trans; body.GetTransform(out trans); for (int i = 0; i < body.FixtureList.Count; ++i) { for (int j = 0; j < body.FixtureList[i].Shape.ChildCount; ++j) { AABB bounds; body.FixtureList[i].Shape.ComputeAABB(out bounds, ref trans, j); Vector2.Min(ref leftBound, ref bounds.LowerBound, out leftBound); } } // calculate body offset from its center and add a 1 pixel border // because we generate the textures a little bigger than the actual body's fixtures origin = ConvertUnits.ToDisplayUnits(body.Position - leftBound) + new Vector2(1f); } return origin; }
public void ScaleToBody(Body body) { Transform t; body.GetTransform(out t); AABB aabb; aabb.LowerBound = new Vector2(Settings.MaxFloat, Settings.MaxFloat); aabb.UpperBound = new Vector2(-Settings.MaxFloat, -Settings.MaxFloat); AABB tempAABB; int fixtureCount = body.FixtureList.Count; for (int i = 0; i < fixtureCount; ++i) { body.FixtureList[i].Shape.ComputeAABB(out tempAABB, ref t, 0); aabb.Combine(ref tempAABB); } Vector2 scale = new Vector2( ConvertUnits.ToDisplayUnits(aabb.Width) / sourceRect.Width, ConvertUnits.ToDisplayUnits(aabb.Height) / sourceRect.Height ); this.scale = scale; }
/// <summary> /// Cast a ray against this Shape. /// </summary> /// <param name="output">The ray-cast results.</param> /// <param name="input">The ray-cast input parameters.</param> /// <param name="childIndex">Index of the child.</param> /// <returns></returns> public bool RayCast(out RayCastOutput output, ref RayCastInput input, int childIndex) { Transform xf; Body.GetTransform(out xf); return(Shape.RayCast(out output, ref input, ref xf, childIndex)); }
/// <summary> /// Test a point for containment in this fixture. /// </summary> /// <param name="point">A point in world coordinates.</param> /// <returns></returns> public bool TestPoint(ref Vector2 point) { Transform xf; Body.GetTransform(out xf); return(Shape.TestPoint(ref xf, ref point)); }
public static AABB GetAABBFromBody(Body body) { Transform t; body.GetTransform(out t); AABB aabb; aabb.LowerBound = new Vector2(Settings.MaxFloat, Settings.MaxFloat); aabb.UpperBound = new Vector2(-Settings.MaxFloat, -Settings.MaxFloat); AABB tempAABB; int fixtureCount = body.FixtureList.Count; for (int i = 0; i < fixtureCount; ++i) { body.FixtureList[i].Shape.ComputeAABB(out tempAABB, ref t, 0); aabb.Combine(ref tempAABB); } return aabb; }
/// <summary> /// Draws a single body /// </summary> /// <param name="body">the body to draw</param> public void DrawBody(Body body) { Transform xf; body.GetTransform(out xf); foreach (Fixture f in body.FixtureList) { PolygonShape poly = (PolygonShape)f.Shape; int vertexCount = poly.Vertices.Count; if (vertexCount > MAX_VERTICES) throw new Exception("Num vertices in polygon exceeds maximum number allowed"); for (int i = 0; i < vertexCount; i++) tempVertices[i] = MathUtils.Multiply(ref xf, poly.Vertices[i]); DrawPolygon(tempVertices, vertexCount, Color.SaddleBrown); } }