use of com.bulletphysics.collision.shapes.ConcaveShape in project bdx by GoranM.
the class CollisionWorld method rayTestSingle.
// TODO
public static void rayTestSingle(Transform rayFromTrans, Transform rayToTrans, CollisionObject collisionObject, CollisionShape collisionShape, Transform colObjWorldTransform, RayResultCallback resultCallback) {
Stack stack = Stack.enter();
SphereShape pointShape = new SphereShape(0f);
pointShape.setMargin(0f);
ConvexShape castShape = pointShape;
if (collisionShape.isConvex()) {
CastResult castResult = new CastResult();
castResult.fraction = resultCallback.closestHitFraction;
ConvexShape convexShape = (ConvexShape) collisionShape;
VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
//#define USE_SUBSIMPLEX_CONVEX_CAST 1
//#ifdef USE_SUBSIMPLEX_CONVEX_CAST
SubsimplexConvexCast convexCaster = new SubsimplexConvexCast(castShape, convexShape, simplexSolver);
if (convexCaster.calcTimeOfImpact(rayFromTrans, rayToTrans, colObjWorldTransform, colObjWorldTransform, castResult)) {
//add hit
if (castResult.normal.lengthSquared() > 0.0001f) {
if (castResult.fraction < resultCallback.closestHitFraction) {
//#ifdef USE_SUBSIMPLEX_CONVEX_CAST
//rotate normal into worldspace
rayFromTrans.basis.transform(castResult.normal);
//#endif //USE_SUBSIMPLEX_CONVEX_CAST
castResult.normal.normalize();
LocalRayResult localRayResult = new LocalRayResult(collisionObject, null, castResult.normal, castResult.fraction);
boolean normalInWorldSpace = true;
resultCallback.addSingleResult(localRayResult, normalInWorldSpace);
}
}
}
} else {
if (collisionShape.isConcave()) {
if (collisionShape.getShapeType() == BroadphaseNativeType.TRIANGLE_MESH_SHAPE_PROXYTYPE) {
// optimized version for BvhTriangleMeshShape
BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape) collisionShape;
Transform worldTocollisionObject = stack.allocTransform();
worldTocollisionObject.inverse(colObjWorldTransform);
Vector3f rayFromLocal = stack.alloc(rayFromTrans.origin);
worldTocollisionObject.transform(rayFromLocal);
Vector3f rayToLocal = stack.alloc(rayToTrans.origin);
worldTocollisionObject.transform(rayToLocal);
BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
rcb.hitFraction = resultCallback.closestHitFraction;
triangleMesh.performRaycast(rcb, rayFromLocal, rayToLocal);
} else {
ConcaveShape triangleMesh = (ConcaveShape) collisionShape;
Transform worldTocollisionObject = stack.allocTransform();
worldTocollisionObject.inverse(colObjWorldTransform);
Vector3f rayFromLocal = stack.alloc(rayFromTrans.origin);
worldTocollisionObject.transform(rayFromLocal);
Vector3f rayToLocal = stack.alloc(rayToTrans.origin);
worldTocollisionObject.transform(rayToLocal);
BridgeTriangleRaycastCallback rcb = new BridgeTriangleRaycastCallback(rayFromLocal, rayToLocal, resultCallback, collisionObject, triangleMesh);
rcb.hitFraction = resultCallback.closestHitFraction;
Vector3f rayAabbMinLocal = stack.alloc(rayFromLocal);
VectorUtil.setMin(rayAabbMinLocal, rayToLocal);
Vector3f rayAabbMaxLocal = stack.alloc(rayFromLocal);
VectorUtil.setMax(rayAabbMaxLocal, rayToLocal);
triangleMesh.processAllTriangles(rcb, rayAabbMinLocal, rayAabbMaxLocal);
}
} else {
// todo: use AABB tree or other BVH acceleration structure!
if (collisionShape.isCompound()) {
CompoundShape compoundShape = (CompoundShape) collisionShape;
int i = 0;
Transform childTrans = stack.allocTransform();
for (i = 0; i < compoundShape.getNumChildShapes(); i++) {
compoundShape.getChildTransform(i, childTrans);
CollisionShape childCollisionShape = compoundShape.getChildShape(i);
Transform childWorldTrans = stack.alloc(colObjWorldTransform);
childWorldTrans.mul(childTrans);
// replace collision shape so that callback can determine the triangle
CollisionShape saveCollisionShape = collisionObject.getCollisionShape();
collisionObject.internalSetTemporaryCollisionShape(childCollisionShape);
rayTestSingle(rayFromTrans, rayToTrans, collisionObject, childCollisionShape, childWorldTrans, resultCallback);
// restore
collisionObject.internalSetTemporaryCollisionShape(saveCollisionShape);
}
}
}
}
stack.leave();
}
use of com.bulletphysics.collision.shapes.ConcaveShape in project bdx by GoranM.
the class ConvexConcaveCollisionAlgorithm method processCollision.
@Override
public void processCollision(CollisionObject body0, CollisionObject body1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
CollisionObject convexBody = isSwapped ? body1 : body0;
CollisionObject triBody = isSwapped ? body0 : body1;
if (triBody.getCollisionShape().isConcave()) {
CollisionObject triOb = triBody;
ConcaveShape concaveShape = (ConcaveShape) triOb.getCollisionShape();
if (convexBody.getCollisionShape().isConvex()) {
float collisionMarginTriangle = concaveShape.getMargin();
resultOut.setPersistentManifold(btConvexTriangleCallback.manifoldPtr);
btConvexTriangleCallback.setTimeStepAndCounters(collisionMarginTriangle, dispatchInfo, resultOut);
// Disable persistency. previously, some older algorithm calculated all contacts in one go, so you can clear it here.
//m_dispatcher->clearManifold(m_btConvexTriangleCallback.m_manifoldPtr);
btConvexTriangleCallback.manifoldPtr.setBodies(convexBody, triBody);
Stack stack = Stack.enter();
concaveShape.processAllTriangles(btConvexTriangleCallback, btConvexTriangleCallback.getAabbMin(stack.allocVector3f()), btConvexTriangleCallback.getAabbMax(stack.allocVector3f()));
resultOut.refreshContactPoints();
stack.leave();
}
}
}
use of com.bulletphysics.collision.shapes.ConcaveShape 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.ConcaveShape in project jmonkeyengine by jMonkeyEngine.
the class BufferedTriangleCallback method getDebugMesh.
public static Mesh getDebugMesh(CollisionShape shape) {
Mesh mesh = null;
if (shape.getCShape() instanceof ConvexShape) {
mesh = new Mesh();
mesh.setBuffer(Type.Position, 3, getVertices((ConvexShape) shape.getCShape()));
mesh.getFloatBuffer(Type.Position).clear();
} else if (shape.getCShape() instanceof ConcaveShape) {
mesh = new Mesh();
mesh.setBuffer(Type.Position, 3, getVertices((ConcaveShape) shape.getCShape()));
mesh.getFloatBuffer(Type.Position).clear();
}
return mesh;
}
use of com.bulletphysics.collision.shapes.ConcaveShape in project bdx by GoranM.
the class ConvexConcaveCollisionAlgorithm method calculateTimeOfImpact.
@Override
public float calculateTimeOfImpact(CollisionObject body0, CollisionObject body1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
Stack stack = Stack.enter();
Vector3f tmp = stack.allocVector3f();
CollisionObject convexbody = isSwapped ? body1 : body0;
CollisionObject triBody = isSwapped ? body0 : body1;
// quick approximation using raycast, todo: hook up to the continuous collision detection (one of the btConvexCast)
// only perform CCD above a certain threshold, this prevents blocking on the long run
// because object in a blocked ccd state (hitfraction<1) get their linear velocity halved each frame...
tmp.sub(convexbody.getInterpolationWorldTransform(stack.allocTransform()).origin, convexbody.getWorldTransform(stack.allocTransform()).origin);
float squareMot0 = tmp.lengthSquared();
if (squareMot0 < convexbody.getCcdSquareMotionThreshold()) {
stack.leave();
return 1f;
}
Transform tmpTrans = stack.allocTransform();
//const btVector3& from = convexbody->m_worldTransform.getOrigin();
//btVector3 to = convexbody->m_interpolationWorldTransform.getOrigin();
//todo: only do if the motion exceeds the 'radius'
Transform triInv = triBody.getWorldTransform(stack.allocTransform());
triInv.inverse();
Transform convexFromLocal = stack.allocTransform();
convexFromLocal.mul(triInv, convexbody.getWorldTransform(tmpTrans));
Transform convexToLocal = stack.allocTransform();
convexToLocal.mul(triInv, convexbody.getInterpolationWorldTransform(tmpTrans));
if (triBody.getCollisionShape().isConcave()) {
Vector3f rayAabbMin = stack.alloc(convexFromLocal.origin);
VectorUtil.setMin(rayAabbMin, convexToLocal.origin);
Vector3f rayAabbMax = stack.alloc(convexFromLocal.origin);
VectorUtil.setMax(rayAabbMax, convexToLocal.origin);
float ccdRadius0 = convexbody.getCcdSweptSphereRadius();
tmp.set(ccdRadius0, ccdRadius0, ccdRadius0);
rayAabbMin.sub(tmp);
rayAabbMax.add(tmp);
// is this available?
float curHitFraction = 1f;
LocalTriangleSphereCastCallback raycastCallback = new LocalTriangleSphereCastCallback(convexFromLocal, convexToLocal, convexbody.getCcdSweptSphereRadius(), curHitFraction);
raycastCallback.hitFraction = convexbody.getHitFraction();
CollisionObject concavebody = triBody;
ConcaveShape triangleMesh = (ConcaveShape) concavebody.getCollisionShape();
if (triangleMesh != null) {
triangleMesh.processAllTriangles(raycastCallback, rayAabbMin, rayAabbMax);
}
if (raycastCallback.hitFraction < convexbody.getHitFraction()) {
convexbody.setHitFraction(raycastCallback.hitFraction);
stack.leave();
return raycastCallback.hitFraction;
}
}
stack.leave();
return 1f;
}
Aggregations