Search in sources :

Example 1 with CastResult

use of com.bulletphysics.collision.narrowphase.ConvexCast.CastResult 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();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) ConcaveShape(com.bulletphysics.collision.shapes.ConcaveShape) CompoundShape(com.bulletphysics.collision.shapes.CompoundShape) Stack(com.bulletphysics.util.Stack) CastResult(com.bulletphysics.collision.narrowphase.ConvexCast.CastResult) BvhTriangleMeshShape(com.bulletphysics.collision.shapes.BvhTriangleMeshShape) VoronoiSimplexSolver(com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver) Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) SphereShape(com.bulletphysics.collision.shapes.SphereShape) Transform(com.bulletphysics.linearmath.Transform) SubsimplexConvexCast(com.bulletphysics.collision.narrowphase.SubsimplexConvexCast)

Example 2 with CastResult

use of com.bulletphysics.collision.narrowphase.ConvexCast.CastResult in project bdx by GoranM.

the class CollisionWorld method objectQuerySingle.

/**
	 * objectQuerySingle performs a collision detection query and calls the resultCallback. It is used internally by rayTest.
	 */
public static void objectQuerySingle(ConvexShape castShape, Transform convexFromTrans, Transform convexToTrans, CollisionObject collisionObject, CollisionShape collisionShape, Transform colObjWorldTransform, ConvexResultCallback resultCallback, float allowedPenetration) {
    Stack stack = Stack.enter();
    if (collisionShape.isConvex()) {
        CastResult castResult = new CastResult();
        castResult.allowedPenetration = allowedPenetration;
        // ??
        castResult.fraction = 1f;
        ConvexShape convexShape = (ConvexShape) collisionShape;
        VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
        GjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver = new GjkEpaPenetrationDepthSolver();
        // JAVA TODO: should be convexCaster1
        //ContinuousConvexCollision convexCaster1(castShape,convexShape,&simplexSolver,&gjkEpaPenetrationSolver);
        GjkConvexCast convexCaster2 = new GjkConvexCast(castShape, convexShape, simplexSolver);
        //btSubsimplexConvexCast convexCaster3(castShape,convexShape,&simplexSolver);
        ConvexCast castPtr = convexCaster2;
        if (castPtr.calcTimeOfImpact(convexFromTrans, convexToTrans, colObjWorldTransform, colObjWorldTransform, castResult)) {
            // add hit
            if (castResult.normal.lengthSquared() > 0.0001f) {
                if (castResult.fraction < resultCallback.closestHitFraction) {
                    castResult.normal.normalize();
                    LocalConvexResult localConvexResult = new LocalConvexResult(collisionObject, null, castResult.normal, castResult.hitPoint, castResult.fraction);
                    boolean normalInWorldSpace = true;
                    resultCallback.addSingleResult(localConvexResult, normalInWorldSpace);
                }
            }
        }
    } else {
        if (collisionShape.isConcave()) {
            if (collisionShape.getShapeType() == BroadphaseNativeType.TRIANGLE_MESH_SHAPE_PROXYTYPE) {
                BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape) collisionShape;
                Transform worldTocollisionObject = stack.allocTransform();
                worldTocollisionObject.inverse(colObjWorldTransform);
                Vector3f convexFromLocal = stack.allocVector3f();
                convexFromLocal.set(convexFromTrans.origin);
                worldTocollisionObject.transform(convexFromLocal);
                Vector3f convexToLocal = stack.allocVector3f();
                convexToLocal.set(convexToTrans.origin);
                worldTocollisionObject.transform(convexToLocal);
                // rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation
                Transform rotationXform = stack.allocTransform();
                Matrix3f tmpMat = stack.allocMatrix3f();
                tmpMat.mul(worldTocollisionObject.basis, convexToTrans.basis);
                rotationXform.set(tmpMat);
                BridgeTriangleConvexcastCallback tccb = new BridgeTriangleConvexcastCallback(castShape, convexFromTrans, convexToTrans, resultCallback, collisionObject, triangleMesh, colObjWorldTransform);
                tccb.hitFraction = resultCallback.closestHitFraction;
                tccb.normalInWorldSpace = true;
                Vector3f boxMinLocal = stack.allocVector3f();
                Vector3f boxMaxLocal = stack.allocVector3f();
                castShape.getAabb(rotationXform, boxMinLocal, boxMaxLocal);
                triangleMesh.performConvexcast(tccb, convexFromLocal, convexToLocal, boxMinLocal, boxMaxLocal);
            } else {
                BvhTriangleMeshShape triangleMesh = (BvhTriangleMeshShape) collisionShape;
                Transform worldTocollisionObject = stack.allocTransform();
                worldTocollisionObject.inverse(colObjWorldTransform);
                Vector3f convexFromLocal = stack.allocVector3f();
                convexFromLocal.set(convexFromTrans.origin);
                worldTocollisionObject.transform(convexFromLocal);
                Vector3f convexToLocal = stack.allocVector3f();
                convexToLocal.set(convexToTrans.origin);
                worldTocollisionObject.transform(convexToLocal);
                // rotation of box in local mesh space = MeshRotation^-1 * ConvexToRotation
                Transform rotationXform = stack.allocTransform();
                Matrix3f tmpMat = stack.allocMatrix3f();
                tmpMat.mul(worldTocollisionObject.basis, convexToTrans.basis);
                rotationXform.set(tmpMat);
                BridgeTriangleConvexcastCallback tccb = new BridgeTriangleConvexcastCallback(castShape, convexFromTrans, convexToTrans, resultCallback, collisionObject, triangleMesh, colObjWorldTransform);
                tccb.hitFraction = resultCallback.closestHitFraction;
                tccb.normalInWorldSpace = false;
                Vector3f boxMinLocal = stack.allocVector3f();
                Vector3f boxMaxLocal = stack.allocVector3f();
                castShape.getAabb(rotationXform, boxMinLocal, boxMaxLocal);
                Vector3f rayAabbMinLocal = stack.alloc(convexFromLocal);
                VectorUtil.setMin(rayAabbMinLocal, convexToLocal);
                Vector3f rayAabbMaxLocal = stack.alloc(convexFromLocal);
                VectorUtil.setMax(rayAabbMaxLocal, convexToLocal);
                rayAabbMinLocal.add(boxMinLocal);
                rayAabbMaxLocal.add(boxMaxLocal);
                triangleMesh.processAllTriangles(tccb, rayAabbMinLocal, rayAabbMaxLocal);
            }
        } else {
            // todo: use AABB tree or other BVH acceleration structure!
            if (collisionShape.isCompound()) {
                CompoundShape compoundShape = (CompoundShape) collisionShape;
                for (int i = 0; i < compoundShape.getNumChildShapes(); i++) {
                    Transform childTrans = compoundShape.getChildTransform(i, stack.allocTransform());
                    CollisionShape childCollisionShape = compoundShape.getChildShape(i);
                    Transform childWorldTrans = stack.allocTransform();
                    childWorldTrans.mul(colObjWorldTransform, childTrans);
                    // replace collision shape so that callback can determine the triangle
                    CollisionShape saveCollisionShape = collisionObject.getCollisionShape();
                    collisionObject.internalSetTemporaryCollisionShape(childCollisionShape);
                    objectQuerySingle(castShape, convexFromTrans, convexToTrans, collisionObject, childCollisionShape, childWorldTrans, resultCallback, allowedPenetration);
                    // restore
                    collisionObject.internalSetTemporaryCollisionShape(saveCollisionShape);
                }
            }
        }
    }
    stack.leave();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) GjkEpaPenetrationDepthSolver(com.bulletphysics.collision.narrowphase.GjkEpaPenetrationDepthSolver) CompoundShape(com.bulletphysics.collision.shapes.CompoundShape) Stack(com.bulletphysics.util.Stack) CastResult(com.bulletphysics.collision.narrowphase.ConvexCast.CastResult) Matrix3f(javax.vecmath.Matrix3f) BvhTriangleMeshShape(com.bulletphysics.collision.shapes.BvhTriangleMeshShape) VoronoiSimplexSolver(com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver) Vector3f(javax.vecmath.Vector3f) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) GjkConvexCast(com.bulletphysics.collision.narrowphase.GjkConvexCast) Transform(com.bulletphysics.linearmath.Transform) GjkConvexCast(com.bulletphysics.collision.narrowphase.GjkConvexCast) ConvexCast(com.bulletphysics.collision.narrowphase.ConvexCast) SubsimplexConvexCast(com.bulletphysics.collision.narrowphase.SubsimplexConvexCast)

Example 3 with CastResult

use of com.bulletphysics.collision.narrowphase.ConvexCast.CastResult in project bdx by GoranM.

the class TriangleConvexcastCallback method processTriangle.

public void processTriangle(Vector3f[] triangle, int partId, int triangleIndex) {
    TriangleShape triangleShape = new TriangleShape(triangle[0], triangle[1], triangle[2]);
    triangleShape.setMargin(triangleCollisionMargin);
    VoronoiSimplexSolver simplexSolver = new VoronoiSimplexSolver();
    GjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver = new GjkEpaPenetrationDepthSolver();
    //#define  USE_SUBSIMPLEX_CONVEX_CAST 1
    //if you reenable USE_SUBSIMPLEX_CONVEX_CAST see commented out code below
    //#ifdef USE_SUBSIMPLEX_CONVEX_CAST
    // TODO: implement ContinuousConvexCollision
    SubsimplexConvexCast convexCaster = new SubsimplexConvexCast(convexShape, triangleShape, simplexSolver);
    //#else
    // //btGjkConvexCast	convexCaster(m_convexShape,&triangleShape,&simplexSolver);
    //btContinuousConvexCollision convexCaster(m_convexShape,&triangleShape,&simplexSolver,&gjkEpaPenetrationSolver);
    //#endif //#USE_SUBSIMPLEX_CONVEX_CAST
    CastResult castResult = new CastResult();
    castResult.fraction = 1f;
    if (convexCaster.calcTimeOfImpact(convexShapeFrom, convexShapeTo, triangleToWorld, triangleToWorld, castResult)) {
        // add hit
        if (castResult.normal.lengthSquared() > 0.0001f) {
            if (castResult.fraction < hitFraction) {
                /* btContinuousConvexCast's normal is already in world space */
                /*
					//#ifdef USE_SUBSIMPLEX_CONVEX_CAST
					// rotate normal into worldspace
					convexShapeFrom.basis.transform(castResult.normal);
					//#endif //USE_SUBSIMPLEX_CONVEX_CAST
					*/
                castResult.normal.normalize();
                reportHit(castResult.normal, castResult.hitPoint, castResult.fraction, partId, triangleIndex);
            }
        }
    }
}
Also used : TriangleShape(com.bulletphysics.collision.shapes.TriangleShape) CastResult(com.bulletphysics.collision.narrowphase.ConvexCast.CastResult)

Aggregations

CastResult (com.bulletphysics.collision.narrowphase.ConvexCast.CastResult)3 SubsimplexConvexCast (com.bulletphysics.collision.narrowphase.SubsimplexConvexCast)2 VoronoiSimplexSolver (com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver)2 BvhTriangleMeshShape (com.bulletphysics.collision.shapes.BvhTriangleMeshShape)2 CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)2 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 ConvexShape (com.bulletphysics.collision.shapes.ConvexShape)2 Transform (com.bulletphysics.linearmath.Transform)2 Stack (com.bulletphysics.util.Stack)2 Vector3f (javax.vecmath.Vector3f)2 ConvexCast (com.bulletphysics.collision.narrowphase.ConvexCast)1 GjkConvexCast (com.bulletphysics.collision.narrowphase.GjkConvexCast)1 GjkEpaPenetrationDepthSolver (com.bulletphysics.collision.narrowphase.GjkEpaPenetrationDepthSolver)1 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)1 SphereShape (com.bulletphysics.collision.shapes.SphereShape)1 TriangleShape (com.bulletphysics.collision.shapes.TriangleShape)1 Matrix3f (javax.vecmath.Matrix3f)1