20
20
import com .spaceproject .generation .BodyFactory ;
21
21
import com .spaceproject .generation .EntityFactory ;
22
22
import com .spaceproject .generation .TextureGenerator ;
23
- import com .spaceproject .math .MyMath ;
24
23
import com .spaceproject .utility .Mappers ;
25
24
import com .spaceproject .utility .SimpleTimer ;
26
25
27
26
public class AsteroidShatterSystem extends EntitySystem implements EntityListener {
28
27
29
28
private final DelaunayTriangulator delaunay = new DelaunayTriangulator ();
30
29
private final float minAsteroidSize = 100 ; //anything smaller than this will not create more
31
- private final float maxDriftVel = 2.0f ; //drift when shatter
32
30
private final float maxDriftAngle = 0.25f ; //angular drift when shatter
33
31
34
32
@ Override
@@ -62,21 +60,15 @@ public void entityRemoved(Entity entity) {
62
60
private void shatterAsteroid (Entity parentAsteroid , AsteroidComponent asteroid ) {
63
61
float [] vertices = asteroid .polygon .getVertices ();
64
62
65
- //debug center of mass / origin
66
- Vector2 center = new Vector2 ();
67
-
68
- GeometryUtils .polygonCentroid (vertices , 0 , vertices .length , center );
69
- if (!asteroid .centerOfMass .epsilonEquals (center )) {
70
- Gdx .app .debug (this .getClass ().getSimpleName (), "WARNING: polygonCentroid disagreement" );
71
- }
72
-
73
63
//create new polygons from vertices + center point to "sub shatter" into smaller polygon shards
74
64
int length = vertices .length ;
75
65
float [] newPoly = new float [length + 2 ];
76
66
System .arraycopy (vertices , 0 , newPoly , 0 , vertices .length );
77
- //add new point in center of triangle
78
- newPoly [length ] = asteroid .centerOfMass .x ;
79
- newPoly [length + 1 ] = asteroid .centerOfMass .y ;
67
+ //add new point in center of polygon
68
+ Vector2 center = new Vector2 ();
69
+ GeometryUtils .polygonCentroid (vertices , 0 , vertices .length , center );
70
+ newPoly [length ] = center .x ;
71
+ newPoly [length + 1 ] = center .y ;
80
72
81
73
spawnChildAsteroid (parentAsteroid , newPoly );
82
74
}
@@ -114,7 +106,7 @@ private void spawnChildAsteroid(Entity parentAsteroid, float[] vertices) {
114
106
*/
115
107
116
108
ShortArray triangles = delaunay .computeTriangles (vertices , false );
117
- // Gdx.app.debug(this. getClass().getSimpleName(), "shatter into " + triangles.size);
109
+ Gdx .app .debug (getClass ().getSimpleName (), "shatter into " + triangles .size / 3 );
118
110
119
111
//create cells for each triangle
120
112
for (int index = 0 ; index < triangles .size ; index += 3 ) {
@@ -133,23 +125,11 @@ private void spawnChildAsteroid(Entity parentAsteroid, float[] vertices) {
133
125
Vector2 center = new Vector2 ();
134
126
GeometryUtils .polygonCentroid (hull , 0 , hull .length , center );
135
127
for (int j = 0 ; j < hull .length ; j += 2 ) {
136
- hull [j ] -= center .x ;
137
- hull [j + 1 ] -= center .y ;
128
+ //todo: shift, but fix spawn origin relative to parent body
129
+ //hull[j] -= center.x;
130
+ //hull[j + 1] -= center.y;
138
131
}
139
132
140
- float triangleQuality = GeometryUtils .triangleQuality (hull [0 ], hull [1 ], hull [2 ], hull [3 ], hull [4 ], hull [5 ]);
141
- //if (triangleQuality < 2.0f) {
142
- //todo: add new vertices to break in half
143
- // because the current shatter creates long ugly slivers
144
- //}
145
- /*
146
- Gdx.app.debug(this.getClass().getSimpleName(),
147
- MyMath.round(hull[0],1) + ", " + MyMath.round(hull[1],1) + " | " +
148
- MyMath.round(hull[2],1) + ", " + MyMath.round(hull[3],1) + " | " +
149
- MyMath.round(hull[4],1) + ", " + MyMath.round(hull[5],1) +
150
- " | clockwise: " + GeometryUtils.isClockwise(hull, 0, hull.length) + " | quality: " + triangleQuality);
151
- */
152
-
153
133
//discard duplicate points
154
134
if ((hull [0 ] == hull [2 ] && hull [1 ] == hull [3 ]) || // p1 == p2 or
155
135
(hull [0 ] == hull [4 ] && hull [1 ] == hull [5 ]) || // p1 == p3 or
@@ -164,14 +144,11 @@ private void spawnChildAsteroid(Entity parentAsteroid, float[] vertices) {
164
144
165
145
//add some variation in velocity and angular so pieces drift apart
166
146
Body parentBody = Mappers .physics .get (parentAsteroid ).body ;
167
- Vector2 pos = parentBody .getPosition ();
147
+ Vector2 pos = parentBody .getPosition ();//todo: .cpy().add(center); NOT WORKING, WHY??S
168
148
Vector2 vel = parentBody .getLinearVelocity ();
169
- Vector2 driftVel = MyMath .vector (MathUtils .random (0 , MathUtils .PI2 ), maxDriftVel );
170
- vel .add (driftVel );
171
- float angularDrift = MathUtils .random (-maxDriftAngle , maxDriftAngle );
172
-
173
149
Entity childAsteroid = EntityFactory .createAsteroid ((long ) (Math .random () * Long .MAX_VALUE ),
174
150
pos .x , pos .y , vel .x , vel .y , parentBody .getAngle (), hull );
151
+ float angularDrift = MathUtils .random (-maxDriftAngle , maxDriftAngle );
175
152
Mappers .physics .get (childAsteroid ).body .setAngularVelocity (parentBody .getAngularVelocity () + angularDrift );
176
153
getEngine ().addEntity (childAsteroid );
177
154
}
0 commit comments