-
Notifications
You must be signed in to change notification settings - Fork 4
/
Box2DShapeConverter.java
86 lines (77 loc) · 2.65 KB
/
Box2DShapeConverter.java
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
package gama.extensions.physics.box2d_version;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.EdgeShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2;
import org.locationtech.jts.geom.LineString;
import gama.extensions.physics.common.IShapeConverter;
import msi.gama.common.geometry.GeometryUtils;
import msi.gama.metamodel.agent.IAgent;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.metamodel.shape.IShape;
import msi.gama.metamodel.shape.IShape.Type;
import msi.gama.runtime.IScope;
import msi.gama.util.matrix.GamaFloatMatrix;
public class Box2DShapeConverter implements IShapeConverter<Shape, Vec2>, IBox2DPhysicalEntity {
@Override
public void computeTranslation(final IAgent agent, final Type type, final float depth, final Vec2 aabbTranslation,
final Vec2 visualTranslation) {
// Normally not applicable.
}
@Override
public Shape convertShape(final IShape shape, final Type type, final float depth) {
switch (type) {
case BOX:
case PLAN:
case SQUARE:
case CUBE:
case CONE:
case PYRAMID:
PolygonShape p = new PolygonShape();
p.setAsBox(shape.getWidth().floatValue() / 2, shape.getHeight().floatValue() / 2);
return p;
case LINECYLINDER:
// oriented on the Y or on the X (default) axis
LineString line = (LineString) shape.getInnerGeometry();
EdgeShape e = new EdgeShape();
e.set(toVector((GamaPoint) line.getCoordinateN(0)), toVector((GamaPoint) line.getCoordinateN(1)));
return e;
case SPHERE:
case CIRCLE:
case POINT:
case CYLINDER:
CircleShape cc = new CircleShape();
cc.setRadius(shape.getWidth().floatValue() / 2);
return cc;
default:
GamaPoint[] points = GeometryUtils.getPointsOf(shape);
switch (points.length) {
case 0:
return null;
case 1:
return convertShape(shape, IShape.Type.POINT, depth);
case 2:
EdgeShape l = new EdgeShape();
l.set(toVector(points[0]), toVector(points[1]));
return l;
default:
PolygonShape ps = new PolygonShape();
Vec2[] vertices = new Vec2[points.length];
for (int i = 0; i < points.length; i++) {
vertices[i] = toVector(points[i]);
}
ps.set(vertices, vertices.length);
return ps;
}
}
}
@Override
public Shape convertTerrain(final IScope scope, final GamaFloatMatrix field, final Double width,
final Double height, final float depth) {
// No way to support "depth" here to build the shape :)
PolygonShape rectangle = new PolygonShape();
rectangle.setAsBox(width.floatValue(), height.floatValue());
return rectangle;
}
}