BowlerKernel
CSGPhysicsManager.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.physics;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import javax.vecmath.Matrix4f;
7 import javax.vecmath.Quat4f;
8 import javax.vecmath.Vector3f;
9 
10 import com.bulletphysics.collision.shapes.*;
11 import com.bulletphysics.dynamics.RigidBody;
12 import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
13 import com.bulletphysics.dynamics.constraintsolver.HingeConstraint;
14 import com.bulletphysics.dynamics.constraintsolver.TypedConstraint;
15 import com.bulletphysics.linearmath.DefaultMotionState;
16 import com.bulletphysics.linearmath.Transform;
17 import com.bulletphysics.util.ObjectArrayList;
18 //import com.neuronrobotics.sdk.addons.kinematics.gui.TransformFactory;
19 import com.neuronrobotics.sdk.addons.kinematics.math.TransformNR;
20 
21 import eu.mihosoft.vrl.v3d.CSG;
22 import eu.mihosoft.vrl.v3d.Cube;
23 import eu.mihosoft.vrl.v3d.Polygon;
24 import eu.mihosoft.vrl.v3d.Sphere;
25 import eu.mihosoft.vrl.v3d.Vertex;
26 
27 import javafx.application.Platform;
28 import javafx.scene.transform.Affine;
29 
30 public class CSGPhysicsManager implements IPhysicsManager {
31 
32  private RigidBody fallRigidBody;
33  private final Affine ballLocation = new Affine();
34  protected List<CSG> baseCSG = null;
35  private Transform updateTransform = new Transform();
36  private IPhysicsUpdate updateManager = null;
37  private PhysicsCore core;
38 
39  public CSGPhysicsManager(List<CSG> baseCSG, Transform pose, double mass, boolean adjustCenter,
40  PhysicsCore core) {
41  this.setBaseCSG(baseCSG);// force a hull of the shape to simplify physics
42 
43  ObjectArrayList<Vector3f> arg0 = new ObjectArrayList<>();
44  for (int i = 0; i < baseCSG.size(); i++) {
45 
46  CSG back = loadCSGToPoints(baseCSG.get(i), adjustCenter, pose, arg0);
47  back.setManipulator(baseCSG.get(i).getManipulator());
48  baseCSG.set(i, back);
49  }
50  CollisionShape fallShape = new com.bulletphysics.collision.shapes.ConvexHullShape(arg0);
51  setup(fallShape, pose, mass, core);
52  }
53 
54  public CSGPhysicsManager(ArrayList<CSG> baseCSG, Vector3f start, double mass, PhysicsCore core) {
55  this(baseCSG, new Transform(new Matrix4f(new Quat4f(0, 0, 0, 1), start, 1.0f)), mass, true, core);
56  }
57 
58  protected CSG loadCSGToPoints(CSG baseCSG, boolean adjustCenter, Transform pose, ObjectArrayList<Vector3f> arg0) {
59  CSG finalCSG = baseCSG;
60 
61  if (adjustCenter) {
62  double xcenter = baseCSG.getMaxX() / 2 + baseCSG.getMinX() / 2;
63  double ycenter = baseCSG.getMaxY() / 2 + baseCSG.getMinY() / 2;
64  double zcenter = baseCSG.getMaxZ() / 2 + baseCSG.getMinZ() / 2;
65 
66  TransformNR poseToMove = TransformFactory.bulletToNr(pose);
67  if (baseCSG.getMaxX() < 1 || baseCSG.getMinX() > -1) {
68  finalCSG = finalCSG.movex(-xcenter);
69  poseToMove.translateX(xcenter);
70  }
71  if (baseCSG.getMaxY() < 1 || baseCSG.getMinY() > -1) {
72  finalCSG = finalCSG.movey(-ycenter);
73  poseToMove.translateY(ycenter);
74  }
75  if (baseCSG.getMaxZ() < 1 || baseCSG.getMinZ() > -1) {
76  finalCSG = finalCSG.movez(-zcenter);
77  poseToMove.translateZ(zcenter);
78  }
79  TransformFactory.nrToBullet(poseToMove, pose);
80  }
81 
82  List<Polygon> polygons = finalCSG.getPolygons();
83  //if(polygons.size()>1000)
84  // polygons = getBoundingBox(finalCSG).getPolygons();
85  for (Polygon p : polygons) {
86  for (Vertex v : p.vertices) {
87  arg0.add(new Vector3f((float) v.getX(), (float) v.getY(), (float) v.getZ()));
88  }
89  }
90  return finalCSG;
91  }
92 
98  public static CSG getBoundingBox(CSG incoming) {
99  return new Cube((-incoming.getMinX() + incoming.getMaxX()), (-incoming.getMinY() + incoming.getMaxY()),
100  (-incoming.getMinZ() + incoming.getMaxZ())).toCSG().toXMax().movex(incoming.getMaxX()).toYMax()
101  .movey(incoming.getMaxY()).toZMax().movez(incoming.getMaxZ());
102  }
103 
104 
105  public void setup(CollisionShape fallShape, Transform pose, double mass, PhysicsCore core) {
106  this.setCore(core);
107 
108  // setup the motion state for the ball
109  System.out.println("Starting Object at " + TransformFactory.bulletToNr(pose));
110  DefaultMotionState fallMotionState = new DefaultMotionState(pose);
111  // This we're going to give mass so it responds to gravity
112  Vector3f fallInertia = new Vector3f(0, 0, 0);
113  fallShape.calculateLocalInertia((float) mass, fallInertia);
114  RigidBodyConstructionInfo fallRigidBodyCI = new RigidBodyConstructionInfo((float) mass, fallMotionState,
115  fallShape, fallInertia);
116 
117  fallRigidBodyCI.additionalDamping = true;
118  setFallRigidBody(new RigidBody(fallRigidBodyCI));
119  // update(40);
120  }
121 
122  public void update(float timeStep) {
123  fallRigidBody.getMotionState().getWorldTransform(updateTransform);
124  if (getUpdateManager() != null) {
125  try {
126  getUpdateManager().update(timeStep);
127  } catch (Exception e) {
128  // BowlerStudio.printStackTrace(e);
129  throw e;
130  }
131  }
132  }
133 
134  public RigidBody getFallRigidBody() {
135  return fallRigidBody;
136  }
137 
138  public void setFallRigidBody(RigidBody fallRigidBody) {
139 
140  this.fallRigidBody = fallRigidBody;
141  }
142 
143  public List<CSG> getBaseCSG() {
144  return baseCSG;
145  }
146 
147  public void setBaseCSG(List<CSG> baseCSG) {
148  for (CSG c : baseCSG) {
149  c.setManipulator(getRigidBodyLocation());
150  }
151  this.baseCSG = baseCSG;
152  }
153 
154  public Transform getUpdateTransform() {
155  return updateTransform;
156  }
157 
158  public Affine getRigidBodyLocation() {
159  return ballLocation;
160  }
161 
163  return updateManager;
164  }
165 
167  this.updateManager = updateManager;
168  }
169 
170  public PhysicsCore getCore() {
171  return core;
172  }
173 
174  public void setCore(PhysicsCore core) {
175  this.core = core;
176  }
177 
178 }
CSG loadCSGToPoints(CSG baseCSG, boolean adjustCenter, Transform pose, ObjectArrayList< Vector3f > arg0)
CSGPhysicsManager(List< CSG > baseCSG, Transform pose, double mass, boolean adjustCenter, PhysicsCore core)
void setup(CollisionShape fallShape, Transform pose, double mass, PhysicsCore core)
CSGPhysicsManager(ArrayList< CSG > baseCSG, Vector3f start, double mass, PhysicsCore core)
static void nrToBullet(TransformNR nr, com.bulletphysics.linearmath.Transform bullet)
static TransformNR bulletToNr(com.bulletphysics.linearmath.Transform bullet)
CSG movez(Number howFarToMove)
Definition: CSG.java:439
CSG movex(Number howFarToMove)
Definition: CSG.java:449
List< Polygon > getPolygons()
Definition: CSG.java:698
CSG setManipulator(Affine manipulator)
Definition: CSG.java:235
CSG toYMax(CSG target)
Definition: CSG.java:348
CSG movey(Number howFarToMove)
Definition: CSG.java:429
CSG toXMax(CSG target)
Definition: CSG.java:328
CSG toZMax(CSG target)
Definition: CSG.java:308