-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsPolygon.java
More file actions
114 lines (94 loc) · 3.23 KB
/
Copy pathPhysicsPolygon.java
File metadata and controls
114 lines (94 loc) · 3.23 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.ArrayList;
import java.util.List;
public class PhysicsPolygon {
private List<Vector2> points;
private Double mass;
private Double density;
private Vector2 centerOfMass;
private Double inertia;
public PhysicsPolygon(Vector2[] points, double mass) {
List<Vector2> copyList = new ArrayList<>();
for (Vector2 point : points) {
copyList.add(new Vector2(point));
}
this.points.addAll(copyList);
this.mass = mass;
updateCenterOfMass();
updateInertia(new Vector2(0.0,0.0));
}
public PhysicsPolygon(List<Vector2> points, double mass) {
this.points = points;
this.mass = mass;
updateCenterOfMass();
updateInertia(new Vector2(0.0,0.0));
}
public PhysicsPolygon(PhysicsPolygon that) {
this.points = new ArrayList<>();
for (Vector2 point : that.points) {
this.points.add(new Vector2(point));
}
this.mass = that.mass;
this.density = that.density;
this.centerOfMass = new Vector2(that.centerOfMass);
this.inertia = that.inertia;
}
public List<Vector2> getPoints() {
return points;
}
/*
* https://en.wikipedia.org/wiki/Polygon#Area
*
* https://en.wikipedia.org/wiki/Centroid#Of_a_polygon
* https://math.stackexchange.com/questions/3177/why-doesnt-a-simple-mean-give-the-position-of-a-centroid-in-a-polygon
*
* https://en.wikipedia.org/wiki/Second_moment_of_area#Any_polygon
*/
public void updateInertia(Vector2 pivot) {
this.inertia = 0.0;
double area = 0.0;
Vector2 curr;
Vector2 next = points.get(0);
for (int i = 0; i < points.size(); i++) {
curr = next;
next = points.get((i+1)%points.size());
double trapizodArea = curr.cross(next);
area += trapizodArea;
centerOfMass = centerOfMass.add(curr.add(next).scale(trapizodArea));
Vector2 dcurr = curr.minus(pivot);
Vector2 dnext = next.minus(pivot);
inertia += trapizodArea * (dcurr.x*dnext.y + 2*dcurr.x*dcurr.y + 2*dnext.x*dnext.y + dnext.x*dcurr.y);
}
area /= 2;
this.density = mass/area;
inertia *= density/12;
}
public Vector2 updateCenterOfMass() {
double area = 0.0;
this.centerOfMass = new Vector2();
Vector2 curr;
Vector2 next = points.get(0);
for (int i = 0; i < points.size(); i++) {
curr = next;
next = points.get((i+1)%points.size());
double trapizodArea = curr.cross(next);
area += trapizodArea;
centerOfMass = centerOfMass.add(curr.add(next).scale(trapizodArea));
}
area /= 2;
centerOfMass = centerOfMass.scale(1/(6*area));
return centerOfMass;
}
public Vector2 centerToCenterOfMass() {
Vector2 oldCenterOfMass = updateCenterOfMass();
for (int i = 0; i < points.size(); i++) {
points.set(i, points.get(i).minus(oldCenterOfMass));
}
return oldCenterOfMass;
}
public Double getMass() {
return mass;
}
public Double getInertia() {
return inertia;
}
}