-
Notifications
You must be signed in to change notification settings - Fork 20
/
Shape.cs
62 lines (55 loc) · 1.8 KB
/
Shape.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using UnityEngine;
using System.Collections.Generic;
namespace Sample07
{
public class Shape
{
public Rigidbody rigidbody;
public Rect bounds;
public List<Vector2> originVertices = new List<Vector2>();
public List<Vector2> vertices = new List<Vector2>();
public Color color = Color.green;
public uint selfMask = 0xffffff;
public uint collisionMask = 0xffffff;
public bool isTrigger = false;
public Shape(Rigidbody rigidbody, Vector2[] vertices)
{
this.rigidbody = rigidbody;
if (vertices != null)
{
originVertices.AddRange(vertices);
}
}
public void updateTransform()
{
vertices.Clear();
for(int i = 0; i < originVertices.Count; ++i)
{
vertices.Add(rigidbody.matrix.transformPoint(originVertices[i]));
}
bounds = new Rect(vertices[0], Vector2.zero);
foreach(var v in vertices)
{
bounds.xMax = Mathf.Max(bounds.xMax, v.x);
bounds.xMin = Mathf.Min(bounds.xMin, v.x);
bounds.yMax = Mathf.Max(bounds.yMax, v.y);
bounds.yMin = Mathf.Min(bounds.yMin, v.y);
}
}
public Vector2 getFarthestPointInDirection(Vector2 dir, out int index)
{
float maxDistance = float.MinValue;
index = 0;
for (int i = 0; i < vertices.Count; ++i)
{
float distance = Vector2.Dot(vertices[i], dir);
if (distance > maxDistance)
{
maxDistance = distance;
index = i;
}
}
return vertices[index];
}
}
}