use of com.bulletphysics.collision.shapes.CompoundShape in project bdx by GoranM.
the class GImpactCollisionAlgorithm method gimpact_vs_shape.
public void gimpact_vs_shape(CollisionObject body0, CollisionObject body1, GImpactShapeInterface shape0, CollisionShape shape1, boolean swapped) {
if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE) {
GImpactMeshShape meshshape0 = (GImpactMeshShape) shape0;
part0 = meshshape0.getMeshPartCount();
while ((part0--) != 0) {
gimpact_vs_shape(body0, body1, meshshape0.getMeshPart(part0), shape1, swapped);
}
return;
}
//#ifdef GIMPACT_VS_PLANE_COLLISION
if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE_PART && shape1.getShapeType() == BroadphaseNativeType.STATIC_PLANE_PROXYTYPE) {
GImpactMeshShapePart shapepart = (GImpactMeshShapePart) shape0;
StaticPlaneShape planeshape = (StaticPlaneShape) shape1;
gimpacttrimeshpart_vs_plane_collision(body0, body1, shapepart, planeshape, swapped);
return;
}
if (shape1.isCompound()) {
CompoundShape compoundshape = (CompoundShape) shape1;
gimpact_vs_compoundshape(body0, body1, shape0, compoundshape, swapped);
return;
} else if (shape1.isConcave()) {
ConcaveShape concaveshape = (ConcaveShape) shape1;
gimpact_vs_concave(body0, body1, shape0, concaveshape, swapped);
return;
}
Stack stack = Stack.enter();
Transform orgtrans0 = body0.getWorldTransform(stack.allocTransform());
Transform orgtrans1 = body1.getWorldTransform(stack.allocTransform());
IntArrayList collided_results = new IntArrayList();
gimpact_vs_shape_find_pairs(orgtrans0, orgtrans1, shape0, shape1, collided_results);
if (collided_results.size() == 0) {
return;
}
shape0.lockChildShapes();
GIM_ShapeRetriever retriever0 = new GIM_ShapeRetriever(shape0);
boolean child_has_transform0 = shape0.childrenHasTransform();
Transform tmpTrans = stack.allocTransform();
int i = collided_results.size();
while ((i--) != 0) {
int child_index = collided_results.get(i);
if (swapped) {
triface1 = child_index;
} else {
triface0 = child_index;
}
CollisionShape colshape0 = retriever0.getChildShape(child_index);
if (child_has_transform0) {
tmpTrans.mul(orgtrans0, shape0.getChildTransform(child_index));
body0.setWorldTransform(tmpTrans);
}
// collide two shapes
if (swapped) {
shape_vs_shape_collision(body1, body0, shape1, colshape0);
} else {
shape_vs_shape_collision(body0, body1, colshape0, shape1);
}
// restore transforms
if (child_has_transform0) {
body0.setWorldTransform(orgtrans0);
}
}
shape0.unlockChildShapes();
stack.leave();
}
use of com.bulletphysics.collision.shapes.CompoundShape in project bdx by GoranM.
the class CompoundCollisionAlgorithm method calculateTimeOfImpact.
@Override
public float calculateTimeOfImpact(CollisionObject body0, CollisionObject body1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
Stack stack = Stack.enter();
CollisionObject colObj = isSwapped ? body1 : body0;
CollisionObject otherObj = isSwapped ? body0 : body1;
assert (colObj.getCollisionShape().isCompound());
CompoundShape compoundShape = (CompoundShape) colObj.getCollisionShape();
// We will use the OptimizedBVH, AABB tree to cull potential child-overlaps
// If both proxies are Compound, we will deal with that directly, by performing sequential/parallel tree traversals
// given Proxy0 and Proxy1, if both have a tree, Tree0 and Tree1, this means:
// determine overlapping nodes of Proxy1 using Proxy0 AABB against Tree1
// then use each overlapping node AABB against Tree0
// and vise versa.
Transform tmpTrans = stack.allocTransform();
Transform orgTrans = stack.allocTransform();
Transform childTrans = stack.allocTransform();
float hitFraction = 1f;
int numChildren = childCollisionAlgorithms.size();
int i;
for (i = 0; i < numChildren; i++) {
// temporarily exchange parent btCollisionShape with childShape, and recurse
CollisionShape childShape = compoundShape.getChildShape(i);
// backup
colObj.getWorldTransform(orgTrans);
compoundShape.getChildTransform(i, childTrans);
//btTransform newChildWorldTrans = orgTrans*childTrans ;
tmpTrans.set(orgTrans);
tmpTrans.mul(childTrans);
colObj.setWorldTransform(tmpTrans);
CollisionShape tmpShape = colObj.getCollisionShape();
colObj.internalSetTemporaryCollisionShape(childShape);
float frac = childCollisionAlgorithms.getQuick(i).calculateTimeOfImpact(colObj, otherObj, dispatchInfo, resultOut);
if (frac < hitFraction) {
hitFraction = frac;
}
// revert back
colObj.internalSetTemporaryCollisionShape(tmpShape);
colObj.setWorldTransform(orgTrans);
}
stack.leave();
return hitFraction;
}
use of com.bulletphysics.collision.shapes.CompoundShape in project bdx by GoranM.
the class CompoundCollisionAlgorithm method init.
public void init(CollisionAlgorithmConstructionInfo ci, CollisionObject body0, CollisionObject body1, boolean isSwapped) {
super.init(ci);
this.isSwapped = isSwapped;
CollisionObject colObj = isSwapped ? body1 : body0;
CollisionObject otherObj = isSwapped ? body0 : body1;
assert (colObj.getCollisionShape().isCompound());
CompoundShape compoundShape = (CompoundShape) colObj.getCollisionShape();
int numChildren = compoundShape.getNumChildShapes();
int i;
//childCollisionAlgorithms.resize(numChildren);
for (i = 0; i < numChildren; i++) {
CollisionShape tmpShape = colObj.getCollisionShape();
CollisionShape childShape = compoundShape.getChildShape(i);
colObj.internalSetTemporaryCollisionShape(childShape);
childCollisionAlgorithms.add(ci.dispatcher1.findAlgorithm(colObj, otherObj));
colObj.internalSetTemporaryCollisionShape(tmpShape);
}
}
use of com.bulletphysics.collision.shapes.CompoundShape in project bdx by GoranM.
the class DiscreteDynamicsWorld method debugDrawObject.
public void debugDrawObject(Transform worldTransform, CollisionShape shape, Vector3f color) {
Stack stack = Stack.enter();
Vector3f tmp = stack.allocVector3f();
Vector3f tmp2 = stack.allocVector3f();
// Draw a small simplex at the center of the object
{
Vector3f start = stack.alloc(worldTransform.origin);
tmp.set(1f, 0f, 0f);
worldTransform.basis.transform(tmp);
tmp.add(start);
tmp2.set(1f, 0f, 0f);
getDebugDrawer().drawLine(start, tmp, tmp2);
tmp.set(0f, 1f, 0f);
worldTransform.basis.transform(tmp);
tmp.add(start);
tmp2.set(0f, 1f, 0f);
getDebugDrawer().drawLine(start, tmp, tmp2);
tmp.set(0f, 0f, 1f);
worldTransform.basis.transform(tmp);
tmp.add(start);
tmp2.set(0f, 0f, 1f);
getDebugDrawer().drawLine(start, tmp, tmp2);
}
if (shape.getShapeType() == BroadphaseNativeType.COMPOUND_SHAPE_PROXYTYPE) {
CompoundShape compoundShape = (CompoundShape) shape;
for (int i = compoundShape.getNumChildShapes() - 1; i >= 0; i--) {
Transform childTrans = new Transform();
compoundShape.getChildTransform(i, childTrans);
CollisionShape colShape = compoundShape.getChildShape(i);
Transform res = new Transform();
res.mul(worldTransform, childTrans);
debugDrawObject(res, colShape, color);
}
} else {
switch(shape.getShapeType()) {
case SPHERE_SHAPE_PROXYTYPE:
{
SphereShape sphereShape = (SphereShape) shape;
//radius doesn't include the margin, so draw with margin
float radius = sphereShape.getMargin();
debugDrawSphere(radius, worldTransform, color);
break;
}
//}
case CAPSULE_SHAPE_PROXYTYPE:
{
CapsuleShape capsuleShape = (CapsuleShape) shape;
float radius = capsuleShape.getRadius();
float halfHeight = capsuleShape.getHalfHeight();
// Draw the ends
{
Transform childTransform = new Transform(worldTransform);
childTransform.origin.set(0, 0, halfHeight);
worldTransform.transform(childTransform.origin);
debugDrawSphere(radius, childTransform, color);
}
{
Transform childTransform = new Transform(worldTransform);
childTransform.origin.set(0, 0, -halfHeight);
worldTransform.transform(childTransform.origin);
debugDrawSphere(radius, childTransform, color);
}
// Draw some additional lines
Vector3f from = stack.allocVector3f();
Vector3f a = stack.allocVector3f();
Vector3f to = stack.allocVector3f();
Vector3f b = stack.allocVector3f();
from.set(worldTransform.origin);
a.set(-radius, 0, halfHeight);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(-radius, 0, -halfHeight);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
from.set(worldTransform.origin);
a.set(radius, 0, halfHeight);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(radius, 0, -halfHeight);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
from.set(worldTransform.origin);
a.set(0, -radius, halfHeight);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(0, -radius, -halfHeight);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
from.set(worldTransform.origin);
a.set(0, radius, halfHeight);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(0, radius, -halfHeight);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
break;
}
case CONE_SHAPE_PROXYTYPE:
{
ConeShape coneShape = (ConeShape) shape;
//+coneShape.getMargin();
float radius = coneShape.getRadius();
//+coneShape.getMargin();
float height = coneShape.getHeight();
int upAxis = coneShape.getConeUpIndex();
float[] tmpv = new float[] { 0, 0, 0 };
Vector3f offsetHeight = new Vector3f();
offsetHeight.get(tmpv);
tmpv[upAxis] = height * 0.5f;
offsetHeight.set(tmpv);
Vector3f offsetRadius = new Vector3f();
offsetRadius.get(tmpv);
tmpv[(upAxis + 1) % 3] = radius;
offsetRadius.set(tmpv);
Vector3f offset2Radius = new Vector3f();
offset2Radius.get(tmpv);
tmpv[(upAxis + 2) % 3] = radius;
offset2Radius.set(tmpv);
Vector3f from = stack.allocVector3f();
Vector3f a = stack.allocVector3f();
Vector3f to = stack.allocVector3f();
Vector3f b = stack.allocVector3f();
from.set(worldTransform.origin);
a.set(offsetHeight);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.add(offsetRadius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.sub(offsetRadius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.add(offset2Radius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.sub(offset2Radius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
break;
}
case CYLINDER_SHAPE_PROXYTYPE:
{
CylinderShape cylinder = (CylinderShape) shape;
int upAxis = cylinder.getUpAxis();
float radius = cylinder.getRadius();
float[] tmpv = new float[] { 0, 0, 0 };
Vector3f halfExtents = new Vector3f();
cylinder.getHalfExtentsWithMargin(halfExtents);
halfExtents.get(tmpv);
float halfHeight = tmpv[upAxis];
Vector3f offsetHeight = new Vector3f();
offsetHeight.get(tmpv);
tmpv[upAxis] = halfHeight;
offsetHeight.set(tmpv);
Vector3f offsetRadius = new Vector3f();
offsetRadius.get(tmpv);
tmpv[(upAxis + 1) % 3] = radius;
offsetRadius.set(tmpv);
Vector3f from = stack.allocVector3f();
Vector3f a = stack.allocVector3f();
Vector3f to = stack.allocVector3f();
Vector3f b = stack.allocVector3f();
from.set(worldTransform.origin);
a.set(offsetHeight);
a.add(offsetRadius);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.add(offsetRadius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
from.set(worldTransform.origin);
a.set(offsetHeight);
a.sub(offsetRadius);
worldTransform.basis.transform(a);
from.add(a);
to.set(worldTransform.origin);
b.set(offsetHeight);
b.negate();
b.sub(offsetRadius);
worldTransform.basis.transform(b);
to.add(b);
getDebugDrawer().drawLine(from, to, color);
break;
}
default:
{
if (shape.isConcave()) {
//btConcaveShape* concaveMesh = (btConcaveShape*) shape;
////todo pass camera, for some culling
//btVector3 aabbMax(btScalar(1e30),btScalar(1e30),btScalar(1e30));
//btVector3 aabbMin(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30));
//DebugDrawcallback drawCallback(getDebugDrawer(),worldTransform,color);
//concaveMesh.processAllTriangles(&drawCallback,aabbMin,aabbMax);
ConcaveShape concaveMesh = (ConcaveShape) shape;
//todo: pass camera for some culling
Vector3f aabbMax = stack.allocVector3f();
Vector3f aabbMin = stack.allocVector3f();
aabbMax.set(1e30f, 1e30f, 1e30f);
aabbMin.set(-1e30f, -1e30f, -1e30f);
//DebugDrawcallback drawCallback;
DebugDrawcallback drawCallback = new DebugDrawcallback(getDebugDrawer(), worldTransform, color);
concaveMesh.processAllTriangles(drawCallback, aabbMin, aabbMax);
}
/// for polyhedral shapes
if (shape.isPolyhedral()) {
PolyhedralConvexShape polyshape = (PolyhedralConvexShape) shape;
Vector3f a = stack.allocVector3f();
Vector3f b = stack.allocVector3f();
int i;
for (i = 0; i < polyshape.getNumEdges(); i++) {
polyshape.getEdge(i, a, b);
worldTransform.transform(a);
worldTransform.transform(b);
getDebugDrawer().drawLine(a, b, color);
}
}
}
}
}
stack.leave();
}
use of com.bulletphysics.collision.shapes.CompoundShape in project bdx by GoranM.
the class GameObject method parent.
public void parent(GameObject p, boolean compound) {
CompoundShape compShapeOld = null;
if (parent != null) {
parent.children.remove(this);
if (compound) {
compShapeOld = parent.compoundShape();
if (compShapeOld != null) {
scene.world.removeRigidBody(parent.body);
compShapeOld.removeChildShape(body.getCollisionShape());
scene.world.addRigidBody(parent.body);
}
}
} else if (p == null) {
return;
}
parent = p;
if (parent != null) {
parent.children.add(this);
updateLocalTransform();
updateLocalScale();
if (compound) {
CompoundShape compShape = parent.compoundShape();
if (compShape != null) {
scene.world.removeRigidBody(body);
compShape.addChildShape(new Transform(localTransform), body.getCollisionShape());
}
} else {
dynamics(false);
}
} else if (currBodyType == BodyType.STATIC || currBodyType == BodyType.SENSOR) {
if (compound && compShapeOld != null)
scene.world.addRigidBody(body);
} else {
dynamics(true);
}
}
Aggregations