TrueSync
TSPhysics2D.cs
1 using System;
2 
3 namespace TrueSync
4 {
5 
9  public class TSPhysics2D {
10 
11  public enum TSCapsuleDirection2D {
12 
13  VERTICAL,
14 
15  HORIZONTAL
16 
17  }
18 
19  private static FP POINT_RADIUS = 0.001f;
20 
21  public static Physics2D.Body _testBody;
22 
23  private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 position, Physics2D.BodySpecialSensor sensorType) {
24  Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld();
25 
26  Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
27  Physics2D.Fixture fixture = body.CreateFixture(shape);
28 
29  body.BodyType = Physics2D.BodyType.Static;
30  body.IsSensor = true;
31  body.CollidesWith = Physics2D.Category.All;
32 
33  body.SpecialSensor = sensorType;
34  body.Position = position;
35 
36  _testBody = body;
37 
38  world.RemoveBody(body);
39  world.ProcessRemovedBodies();
40 
41  if (body._specialSensorResults.Count > 0) {
42  if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce) {
43  return Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent<TSCollider2D>();
44  } else {
45  TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count];
46  for (int i = 0; i < body._specialSensorResults.Count; i++) {
47  result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent<TSCollider2D>();
48  }
49 
50  return result;
51  }
52 
53  }
54 
55  return null;
56  }
57 
58  private static object _OverlapCircle(TSVector2 point, FP radius, Physics2D.BodySpecialSensor sensorType) {
59  return OverlapGeneric(new Physics2D.CircleShape(radius, 1), point, sensorType);
60  }
61 
68  public static TSCollider2D OverlapCircle(TSVector2 point, FP radius) {
69  return (TSCollider2D) _OverlapCircle(point, radius, Physics2D.BodySpecialSensor.ActiveOnce);
70  }
71 
78  public static TSCollider2D[] OverlapCircleAll(TSVector2 point, FP radius) {
79  return (TSCollider2D[]) _OverlapCircle(point, radius, Physics2D.BodySpecialSensor.ActiveAll);
80  }
81 
88  public static object _OverlapArea(TSVector2 pointA, TSVector2 pointB, Physics2D.BodySpecialSensor sensorType) {
89  TSVector2 center;
90  center.x = (pointA.x + pointB.x) * FP.Half;
91  center.y = (pointA.y + pointB.y) * FP.Half;
92 
93  Physics2D.Vertices vertices = new Physics2D.Vertices(4);
94  vertices.Add(new TSVector2(pointA.x, pointA.y) - center);
95  vertices.Add(new TSVector2(pointB.x, pointA.y) - center);
96  vertices.Add(new TSVector2(pointB.x, pointB.y) - center);
97  vertices.Add(new TSVector2(pointA.x, pointB.y) - center);
98 
99  return OverlapGeneric(new Physics2D.PolygonShape(vertices, 1), center, sensorType);
100  }
101 
108  public static TSCollider2D OverlapArea(TSVector2 pointA, TSVector2 pointB) {
109  return (TSCollider2D) _OverlapArea(pointA, pointB, Physics2D.BodySpecialSensor.ActiveOnce);
110  }
111 
118  public static TSCollider2D[] OverlapAreaAll(TSVector2 pointA, TSVector2 pointB) {
119  return (TSCollider2D[]) _OverlapArea(pointA, pointB, Physics2D.BodySpecialSensor.ActiveAll);
120  }
121 
127  public static TSCollider2D OverlapPoint(TSVector2 point) {
128  return (TSCollider2D)_OverlapCircle(point, POINT_RADIUS, Physics2D.BodySpecialSensor.ActiveOnce);
129  }
130 
136  public static TSCollider2D[] OverlapPointAll(TSVector2 point) {
137  return (TSCollider2D[])_OverlapCircle(point, POINT_RADIUS, Physics2D.BodySpecialSensor.ActiveAll);
138  }
139 
140  private static object _OverlapBox(TSVector2 point, TSVector2 size, FP angle, Physics2D.BodySpecialSensor sensorType) {
141  size *= FP.Half;
142  angle *= FP.Deg2Rad;
143 
144  return OverlapGeneric(new Physics2D.PolygonShape(Physics2D.PolygonTools.CreateRectangle(size.x, size.y, point, angle * -1), 1), point, sensorType);
145  }
146 
154  public static TSCollider2D OverlapBox(TSVector2 point, TSVector2 size, FP angle) {
155  return (TSCollider2D) _OverlapBox(point, size, angle, Physics2D.BodySpecialSensor.ActiveOnce);
156  }
157 
165  public static TSCollider2D[] OverlapBoxAll(TSVector2 point, TSVector2 size, FP angle) {
166  return (TSCollider2D[]) _OverlapBox(point, size, angle, Physics2D.BodySpecialSensor.ActiveAll);
167  }
168 
169  private static object _OverlapCapsule(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle, Physics2D.BodySpecialSensor sensorType) {
170  if (direction == TSCapsuleDirection2D.HORIZONTAL) {
171  FP aux = size.y;
172  size.y = size.x;
173  size.x = aux;
174 
175  angle += 90;
176  }
177 
178  FP radius = size.x * FP.Half;
179  Physics2D.Vertices capVerts = Physics2D.PolygonTools.CreateCapsule(size.y, radius, 8, radius, 8);
180 
181  Physics2D.PolygonTools.TransformVertices(capVerts, point, angle * FP.Deg2Rad * -1);
182 
183  return OverlapGeneric(new Physics2D.PolygonShape(capVerts, 1), point, sensorType);
184  }
185 
194  public static TSCollider2D OverlapCapsule(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle) {
195  return (TSCollider2D) _OverlapCapsule(point, size, direction, angle, Physics2D.BodySpecialSensor.ActiveOnce);
196  }
197 
206  public static TSCollider2D[] OverlapCapsuleAll(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle) {
207  return (TSCollider2D[]) _OverlapCapsule(point, size, direction, angle, Physics2D.BodySpecialSensor.ActiveAll);
208  }
209 
210  public static object _CircleCast(TSVector2 origin, FP radius, TSVector2 direction, FP distance, Physics2D.BodySpecialSensor sensorType) {
211  if (distance + radius > FP.MaxValue) {
212  distance = FP.MaxValue - radius;
213  }
214 
215  direction.Normalize();
216 
217  TSVector2 offsetToCenter = ((direction * distance) * FP.Half);
218  offsetToCenter.x = FP.Abs(offsetToCenter.x);
219  offsetToCenter.y = FP.Abs(offsetToCenter.y);
220 
221  FP angle = TSVector2.Angle(direction, TSVector2.right);
222 
223  if (direction.x <= 0 && direction.y >= 0) {
224  offsetToCenter.x = -offsetToCenter.x;
225  } else if (direction.x <= 0 && direction.y <= 0) {
226  offsetToCenter.x = -offsetToCenter.x;
227  offsetToCenter.y = -offsetToCenter.y;
228  angle = -angle;
229  } else if (direction.x >= 0 && direction.y <= 0) {
230  offsetToCenter.y = -offsetToCenter.y;
231  angle = -angle;
232  }
233 
234  TSVector2 center = origin + offsetToCenter;
235 
236  object result = _OverlapCapsule(center, new TSVector2(distance + radius * 2, radius * 2), TSCapsuleDirection2D.HORIZONTAL, -angle, sensorType);
237 
238  if (result is TSCollider2D) {
239  return new TSRaycastHit2D((TSCollider2D) result);
240  } else {
241  TSCollider2D[] resultAux = (TSCollider2D[]) result;
242  TSRaycastHit2D[] resultHit = new TSRaycastHit2D[resultAux.Length];
243 
244  for (int index = 0; index < resultHit.Length; index++) {
245  resultHit[index] = new TSRaycastHit2D(resultAux[index]);
246  }
247 
248  return resultHit;
249  }
250  }
251 
260  public static TSRaycastHit2D CircleCast(TSVector2 origin, FP radius, TSVector2 direction, FP distance) {
261  return (TSRaycastHit2D) _CircleCast(origin, radius, direction, distance, Physics2D.BodySpecialSensor.ActiveOnce);
262  }
263 
272  public static TSRaycastHit2D[] CircleCastAll(TSVector2 origin, FP radius, TSVector2 direction, FP distance) {
273  return (TSRaycastHit2D[]) _CircleCast(origin, radius, direction, distance, Physics2D.BodySpecialSensor.ActiveAll);
274  }
275 
276  }
277 
278 }
static TSCollider2D OverlapArea(TSVector2 pointA, TSVector2 pointB)
Returns the first TSCollider2D within a rectangular area. Returns null if there is none...
Definition: TSPhysics2D.cs:108
static TSCollider2D OverlapCapsule(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle)
Returns the first TSCollider2D within a capsule area. Returns null if there is none.
Definition: TSPhysics2D.cs:194
static TSCollider2D OverlapBox(TSVector2 point, TSVector2 size, FP angle)
Returns the first TSCollider2D within a box area. Returns null if there is none.
Definition: TSPhysics2D.cs:154
static TSRaycastHit2D[] CircleCastAll(TSVector2 origin, FP radius, TSVector2 direction, FP distance)
Cast a circle and returns an array TSRaycastHit2D with information about all TSCollider2D found...
Definition: TSPhysics2D.cs:272
static Physics2DWorldManager instance
Public access to a manager instance.
static TSCollider2D[] OverlapCapsuleAll(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle)
Returns all TSCollider2D within a capsule area. Returns null if there is none.
Definition: TSPhysics2D.cs:206
static TSCollider2D[] OverlapBoxAll(TSVector2 point, TSVector2 size, FP angle)
Returns all TSCollider2D within a box area. Returns null if there is none.
Definition: TSPhysics2D.cs:165
static object _OverlapArea(TSVector2 pointA, TSVector2 pointB, Physics2D.BodySpecialSensor sensorType)
Returns the first TSCollider2D within a rectangular area. Returns null if there is none...
Definition: TSPhysics2D.cs:88
static TSCollider2D[] OverlapAreaAll(TSVector2 pointA, TSVector2 pointB)
Returns all TSCollider2D within a rectangular area. Returns null if there is none.
Definition: TSPhysics2D.cs:118
static TSRaycastHit2D CircleCast(TSVector2 origin, FP radius, TSVector2 direction, FP distance)
Cast a circle and returns a TSRaycastHit2D with information about the first TSCollider2D found...
Definition: TSPhysics2D.cs:260
Information about a 2D cast hit.
static TSCollider2D[] OverlapPointAll(TSVector2 point)
Returns all TSCollider2D within a small circular area. Returns null if there is none.
Definition: TSPhysics2D.cs:136
Manages the 2D physics simulation.
static TSCollider2D[] OverlapCircleAll(TSVector2 point, FP radius)
Returns all TSCollider2D within a circular area. Returns null if there is none.
Definition: TSPhysics2D.cs:78
static TSCollider2D OverlapPoint(TSVector2 point)
Returns the first TSCollider2D within a small circular area. Returns null if there is none...
Definition: TSPhysics2D.cs:127
Helpers for 2D physics.
Definition: TSPhysics2D.cs:9
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11
static TSCollider2D OverlapCircle(TSVector2 point, FP radius)
Returns the first TSCollider2D within a circular area. Returns null if there is none.
Definition: TSPhysics2D.cs:68