private bool GetColliderMeasurements(bool hasFsBody, ref List <FVector2> shapePoints) { // TODO: Add support for capsule colliders var box = GetComponent <BoxCollider>(); if (box == null) { return(false); } Vector3 scale = transform.lossyScale; // Added support for collider center offset (has to convert to local first) var center = transform.InverseTransformPoint(box.bounds.center); // Top left to Top Right points, counter-clockwise var v00l = new Vector3(-box.size.x * 0.5f, -box.size.y * 0.5f) + center; var v01l = new Vector3(box.size.x * 0.5f, -box.size.y * 0.5f) + center; var v02l = new Vector3(box.size.x * 0.5f, box.size.y * 0.5f) + center; var v03l = new Vector3(-box.size.x * 0.5f, box.size.y * 0.5f) + center; v00l.Scale(scale); v01l.Scale(scale); v02l.Scale(scale); v03l.Scale(scale); Vector3 v00 = hasFsBody ? v00l : FSHelper.LocalTranslatedVec3(v00l, transform); Vector3 v01 = hasFsBody ? v01l : FSHelper.LocalTranslatedVec3(v01l, transform); Vector3 v02 = hasFsBody ? v02l : FSHelper.LocalTranslatedVec3(v02l, transform); Vector3 v03 = hasFsBody ? v03l : FSHelper.LocalTranslatedVec3(v03l, transform); shapePoints.Add(FSHelper.Vector3ToFVector2(v00)); shapePoints.Add(FSHelper.Vector3ToFVector2(v01)); shapePoints.Add(FSHelper.Vector3ToFVector2(v02)); shapePoints.Add(FSHelper.Vector3ToFVector2(v03)); return(true); }
protected PolygonShape GetPolyShape() { // if this GameObject contains the FSBodyComponent // then there is no need to adjust the colliders! bool isBody = GetComponent <FSBodyComponent>() != null; List <FVector2> vs = new List <FVector2>(); if (UseUnityCollider) { BoxCollider bc = this.GetComponent <BoxCollider>(); if (bc == null) { return(null); } Vector3 scale = transform.lossyScale; //Debug.Log("SCALE: " + scale); Vector3 v00l = new Vector3(-bc.size.x * 0.5f, -bc.size.y * 0.5f); Vector3 v01l = new Vector3(bc.size.x * 0.5f, -bc.size.y * 0.5f); Vector3 v02l = new Vector3(bc.size.x * 0.5f, bc.size.y * 0.5f); Vector3 v03l = new Vector3(-bc.size.x * 0.5f, bc.size.y * 0.5f); v00l.Scale(scale); v01l.Scale(scale); v02l.Scale(scale); v03l.Scale(scale); Vector3 v00 = isBody ? v00l : FSHelper.LocalTranslatedVec3(v00l, this.transform); //v00.Scale(scale); Vector3 v01 = isBody ? v01l : FSHelper.LocalTranslatedVec3(v01l, this.transform); //v01.Scale(scale); Vector3 v02 = isBody ? v02l : FSHelper.LocalTranslatedVec3(v02l, this.transform); //v02.Scale(scale); Vector3 v03 = isBody ? v03l : FSHelper.LocalTranslatedVec3(v03l, this.transform); //v03.Scale(scale); vs.Add(FSHelper.Vector3ToFVector2(v00)); vs.Add(FSHelper.Vector3ToFVector2(v01)); vs.Add(FSHelper.Vector3ToFVector2(v02)); vs.Add(FSHelper.Vector3ToFVector2(v03)); } else { if (PolygonPoints.Length < 3) { return(null); } //vs = new Vertices(); for (int i = 0; i < PolygonPoints.Length; i++) { if (!isBody) { // updated! // now the points can be anywhere Vector3 localp = transform.parent.InverseTransformPoint(PolygonPoints[i].position); vs.Add(FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3(localp, this.transform.parent))); // not doing this way anymore because this way points couldn't be shared betweeen shapes //vs.Add(FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3(PolygonPoints[i].localPosition, this.transform))); } else { vs.Add(FSHelper.Vector3ToFVector2(PolygonPoints[i].localPosition)); } } } return(new PolygonShape(new Vertices(vs.ToArray()), Density)); }
private PolygonShape GetPolyShape() { // if this GameObject contains the FSBodyComponent there is no need to adjust the colliders bool hasFsBody = GetComponent <FSBodyComponent>() != null; var shapePoints = new List <FVector2>(); if (UseUnityCollider) { if (!GetColliderMeasurements(hasFsBody, ref shapePoints)) { return(null); } } else { if (UseTransforms) { if (PolygonTransforms.Length < 3) { return(null); } foreach (Transform t in PolygonTransforms) { shapePoints.Add(hasFsBody ? FSHelper.Vector3ToFVector2(t.localPosition) : FSHelper.Vector3ToFVector2(FSHelper.LocalTranslatedVec3( transform.parent.InverseTransformPoint(t.position), transform.parent))); } } else { if (PolygonCoordinates.Length < 3) { return(null); } foreach (Vector2 t in PolygonCoordinates) { shapePoints.Add(hasFsBody ? FSHelper.Vector3ToFVector2(t) : FSHelper.Vector3ToFVector2(t)); } } } return(new PolygonShape(new Vertices(shapePoints.ToArray()), Density)); }