11 public enum TSCapsuleDirection2D {
19 private static FP POINT_RADIUS = 0.001f;
21 public static Physics2D.Body _testBody;
23 private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 position, Physics2D.BodySpecialSensor sensorType) {
26 Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
27 Physics2D.Fixture fixture = body.CreateFixture(shape);
29 body.BodyType = Physics2D.BodyType.Static;
31 body.CollidesWith = Physics2D.Category.All;
33 body.SpecialSensor = sensorType;
34 body.Position = position;
38 world.RemoveBody(body);
39 world.ProcessRemovedBodies();
41 if (body._specialSensorResults.Count > 0) {
42 if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce) {
46 for (
int i = 0; i < body._specialSensorResults.Count; i++) {
58 private static object _OverlapCircle(TSVector2 point, FP radius, Physics2D.BodySpecialSensor sensorType) {
59 return OverlapGeneric(
new Physics2D.CircleShape(radius, 1), point, sensorType);
69 return (
TSCollider2D) _OverlapCircle(point, radius, Physics2D.BodySpecialSensor.ActiveOnce);
79 return (
TSCollider2D[]) _OverlapCircle(point, radius, Physics2D.BodySpecialSensor.ActiveAll);
88 public static object _OverlapArea(TSVector2 pointA, TSVector2 pointB, Physics2D.BodySpecialSensor sensorType) {
90 center.x = (pointA.x + pointB.x) * FP.Half;
91 center.y = (pointA.y + pointB.y) * FP.Half;
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);
99 return OverlapGeneric(
new Physics2D.PolygonShape(vertices, 1), center, sensorType);
128 return (
TSCollider2D)_OverlapCircle(point, POINT_RADIUS, Physics2D.BodySpecialSensor.ActiveOnce);
137 return (
TSCollider2D[])_OverlapCircle(point, POINT_RADIUS, Physics2D.BodySpecialSensor.ActiveAll);
140 private static object _OverlapBox(TSVector2 point, TSVector2 size, FP angle, Physics2D.BodySpecialSensor sensorType) {
144 return OverlapGeneric(
new Physics2D.PolygonShape(Physics2D.PolygonTools.CreateRectangle(size.x, size.y, point, angle * -1), 1), point, sensorType);
155 return (
TSCollider2D) _OverlapBox(point, size, angle, Physics2D.BodySpecialSensor.ActiveOnce);
166 return (
TSCollider2D[]) _OverlapBox(point, size, angle, Physics2D.BodySpecialSensor.ActiveAll);
169 private static object _OverlapCapsule(TSVector2 point, TSVector2 size, TSCapsuleDirection2D direction, FP angle, Physics2D.BodySpecialSensor sensorType) {
170 if (direction == TSCapsuleDirection2D.HORIZONTAL) {
178 FP radius = size.x * FP.Half;
179 Physics2D.Vertices capVerts = Physics2D.PolygonTools.CreateCapsule(size.y, radius, 8, radius, 8);
181 Physics2D.PolygonTools.TransformVertices(capVerts, point, angle * FP.Deg2Rad * -1);
183 return OverlapGeneric(
new Physics2D.PolygonShape(capVerts, 1), point, sensorType);
195 return (
TSCollider2D) _OverlapCapsule(point, size, direction, angle, Physics2D.BodySpecialSensor.ActiveOnce);
207 return (
TSCollider2D[]) _OverlapCapsule(point, size, direction, angle, Physics2D.BodySpecialSensor.ActiveAll);
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;
215 direction.Normalize();
217 TSVector2 offsetToCenter = ((direction * distance) * FP.Half);
218 offsetToCenter.x = FP.Abs(offsetToCenter.x);
219 offsetToCenter.y = FP.Abs(offsetToCenter.y);
221 FP angle = TSVector2.Angle(direction, TSVector2.right);
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;
229 }
else if (direction.x >= 0 && direction.y <= 0) {
230 offsetToCenter.y = -offsetToCenter.y;
234 TSVector2 center = origin + offsetToCenter;
236 object result = _OverlapCapsule(center,
new TSVector2(distance + radius * 2, radius * 2), TSCapsuleDirection2D.HORIZONTAL, -angle, sensorType);
241 TSCollider2D[] resultAux = (TSCollider2D[]) result;
244 for (
int index = 0; index < resultHit.Length; index++) {
261 return (
TSRaycastHit2D) _CircleCast(origin, radius, direction, distance, Physics2D.BodySpecialSensor.ActiveOnce);
273 return (
TSRaycastHit2D[]) _CircleCast(origin, radius, direction, distance, Physics2D.BodySpecialSensor.ActiveAll);
static TSCollider2D OverlapArea(TSVector2 pointA, TSVector2 pointB)
Returns the first TSCollider2D within a rectangular area. Returns null if there is none...
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.
static TSCollider2D OverlapBox(TSVector2 point, TSVector2 size, FP angle)
Returns the first TSCollider2D within a box area. Returns null if there is none.
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...
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.
static TSCollider2D[] OverlapBoxAll(TSVector2 point, TSVector2 size, FP angle)
Returns all TSCollider2D within a box area. Returns null if there is none.
static object _OverlapArea(TSVector2 pointA, TSVector2 pointB, Physics2D.BodySpecialSensor sensorType)
Returns the first TSCollider2D within a rectangular area. Returns null if there is none...
static TSCollider2D[] OverlapAreaAll(TSVector2 pointA, TSVector2 pointB)
Returns all TSCollider2D within a rectangular area. Returns null if there is none.
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...
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.
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.
static TSCollider2D OverlapPoint(TSVector2 point)
Returns the first TSCollider2D within a small circular area. Returns null if there is none...
Abstract collider for 2D shapes.
static TSCollider2D OverlapCircle(TSVector2 point, FP radius)
Returns the first TSCollider2D within a circular area. Returns null if there is none.