TrueSync: TSPolygonCollider2D.cs Source File

TrueSync

TSPolygonCollider2D.cs
1 using System.Collections.Generic;
2 using UnityEngine;
3 
4 namespace TrueSync {
5 
9  [AddComponentMenu("TrueSync/Physics/PolygonCollider2D", 0)]
11 
12  [SerializeField]
13  private TSVector2[] _points;
14 
15  public TSVector2[] points {
16  get {
17  return _points;
18  }
19 
20  set {
21  if (_body == null) {
22  _points = value;
23  }
24  }
25  }
26 
30  public override TrueSync.Physics2D.Shape[] CreateShapes() {
31  if (_points == null || _points.Length == 0) {
32  return null;
33  }
34 
35 
36  TSVector2 lossy2D = new TSVector2(lossyScale.x, lossyScale.y);
37  TrueSync.Physics2D.Vertices v = new Physics2D.Vertices();
38  for (int index = 0, length = _points.Length; index < length; index++) {
39  v.Add(TSVector2.Scale(_points[index], lossy2D));
40  }
41 
42  List<TrueSync.Physics2D.Vertices> convexShapeVs = TrueSync.Physics2D.BayazitDecomposer.ConvexPartition(v);
43  TrueSync.Physics2D.Shape[] result = new Physics2D.Shape[convexShapeVs.Count];
44  for (int index = 0, length = result.Length; index < length; index++) {
45  result[index] = new TrueSync.Physics2D.PolygonShape(convexShapeVs[index], 1);
46  }
47 
48  return result;
49  }
50 
51  protected override void DrawGizmos() {
52  DrawPolygon(_points);
53  }
54 
55  private void DrawPolygon(TSVector2[] allPoints) {
56  if (allPoints == null || allPoints.Length == 0) {
57  return;
58  }
59 
60  for (int index = 0, length = allPoints.Length - 1; index < length; index++) {
61  Gizmos.DrawLine(allPoints[index].ToVector(), allPoints[index+1].ToVector());
62  }
63 
64  Gizmos.DrawLine(allPoints[allPoints.Length - 1].ToVector(), allPoints[0].ToVector());
65  }
66 
67  protected override Vector3 GetGizmosSize() {
68  return lossyScale.ToVector();
69  }
70 
71  }
72 
73 }
override TrueSync.Physics2D.Shape[] CreateShapes()
Create the internal shape used to represent a PolygonCollider.
Collider with a polygon 2D shape.
override void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
override Vector3 GetGizmosSize()
Returns the gizmos size.
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:12
Generated by   doxygen 1.8.11