Search in sources :

Example 1 with SphereShape

use of com.bulletphysics.collision.shapes.SphereShape in project jmonkeyengine by jMonkeyEngine.

the class SphereCollisionShape method createShape.

protected void createShape() {
    cShape = new SphereShape(radius);
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
Also used : SphereShape(com.bulletphysics.collision.shapes.SphereShape)

Example 2 with SphereShape

use of com.bulletphysics.collision.shapes.SphereShape 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 3 with SphereShape

use of com.bulletphysics.collision.shapes.SphereShape in project bdx by GoranM.

the class ConvexConvexAlgorithm method calculateTimeOfImpact.

@Override
public float calculateTimeOfImpact(CollisionObject col0, CollisionObject col1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
    Stack stack = Stack.enter();
    Vector3f tmp = stack.allocVector3f();
    Transform tmpTrans1 = stack.allocTransform();
    Transform tmpTrans2 = stack.allocTransform();
    // Rather then checking ALL pairs, only calculate TOI when motion exceeds threshold
    // Linear motion for one of objects needs to exceed m_ccdSquareMotionThreshold
    // col0->m_worldTransform,
    float resultFraction = 1f;
    tmp.sub(col0.getInterpolationWorldTransform(tmpTrans1).origin, col0.getWorldTransform(tmpTrans2).origin);
    float squareMot0 = tmp.lengthSquared();
    tmp.sub(col1.getInterpolationWorldTransform(tmpTrans1).origin, col1.getWorldTransform(tmpTrans2).origin);
    float squareMot1 = tmp.lengthSquared();
    if (squareMot0 < col0.getCcdSquareMotionThreshold() && squareMot1 < col1.getCcdSquareMotionThreshold()) {
        return resultFraction;
    }
    if (disableCcd) {
        stack.leave();
        return 1f;
    }
    Transform tmpTrans3 = stack.allocTransform();
    Transform tmpTrans4 = stack.allocTransform();
    // An adhoc way of testing the Continuous Collision Detection algorithms
    // One object is approximated as a sphere, to simplify things
    // Starting in penetration should report no time of impact
    // For proper CCD, better accuracy and handling of 'allowed' penetration should be added
    // also the mainloop of the physics should have a kind of toi queue (something like Brian Mirtich's application of Timewarp for Rigidbodies)
    // Convex0 against sphere for Convex1
    {
        ConvexShape convex0 = (ConvexShape) col0.getCollisionShape();
        // todo: allow non-zero sphere sizes, for better approximation
        SphereShape sphere1 = new SphereShape(col1.getCcdSweptSphereRadius());
        ConvexCast.CastResult result = new ConvexCast.CastResult();
        VoronoiSimplexSolver voronoiSimplex = new VoronoiSimplexSolver();
        //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
        ///Simplification, one object is simplified as a sphere
        GjkConvexCast ccd1 = new GjkConvexCast(convex0, sphere1, voronoiSimplex);
        //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
        if (ccd1.calcTimeOfImpact(col0.getWorldTransform(tmpTrans1), col0.getInterpolationWorldTransform(tmpTrans2), col1.getWorldTransform(tmpTrans3), col1.getInterpolationWorldTransform(tmpTrans4), result)) {
            if (col0.getHitFraction() > result.fraction) {
                col0.setHitFraction(result.fraction);
            }
            if (col1.getHitFraction() > result.fraction) {
                col1.setHitFraction(result.fraction);
            }
            if (resultFraction > result.fraction) {
                resultFraction = result.fraction;
            }
        }
    }
    // Sphere (for convex0) against Convex1
    {
        ConvexShape convex1 = (ConvexShape) col1.getCollisionShape();
        // todo: allow non-zero sphere sizes, for better approximation
        SphereShape sphere0 = new SphereShape(col0.getCcdSweptSphereRadius());
        ConvexCast.CastResult result = new ConvexCast.CastResult();
        VoronoiSimplexSolver voronoiSimplex = new VoronoiSimplexSolver();
        //SubsimplexConvexCast ccd0(&sphere,min0,&voronoiSimplex);
        ///Simplification, one object is simplified as a sphere
        GjkConvexCast ccd1 = new GjkConvexCast(sphere0, convex1, voronoiSimplex);
        //ContinuousConvexCollision ccd(min0,min1,&voronoiSimplex,0);
        if (ccd1.calcTimeOfImpact(col0.getWorldTransform(tmpTrans1), col0.getInterpolationWorldTransform(tmpTrans2), col1.getWorldTransform(tmpTrans3), col1.getInterpolationWorldTransform(tmpTrans4), result)) {
            if (col0.getHitFraction() > result.fraction) {
                col0.setHitFraction(result.fraction);
            }
            if (col1.getHitFraction() > result.fraction) {
                col1.setHitFraction(result.fraction);
            }
            if (resultFraction > result.fraction) {
                resultFraction = result.fraction;
            }
        }
    }
    stack.leave();
    return resultFraction;
}
Also used : Vector3f(javax.vecmath.Vector3f) VoronoiSimplexSolver(com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver) ConvexShape(com.bulletphysics.collision.shapes.ConvexShape) SphereShape(com.bulletphysics.collision.shapes.SphereShape) GjkConvexCast(com.bulletphysics.collision.narrowphase.GjkConvexCast) Transform(com.bulletphysics.linearmath.Transform) Stack(com.bulletphysics.util.Stack) GjkConvexCast(com.bulletphysics.collision.narrowphase.GjkConvexCast) ConvexCast(com.bulletphysics.collision.narrowphase.ConvexCast)

Example 4 with SphereShape

use of com.bulletphysics.collision.shapes.SphereShape in project bdx by GoranM.

the class SphereSphereCollisionAlgorithm method processCollision.

@Override
public void processCollision(CollisionObject col0, CollisionObject col1, DispatcherInfo dispatchInfo, ManifoldResult resultOut) {
    if (manifoldPtr == null) {
        return;
    }
    Stack stack = Stack.enter();
    Transform tmpTrans1 = stack.allocTransform();
    Transform tmpTrans2 = stack.allocTransform();
    resultOut.setPersistentManifold(manifoldPtr);
    SphereShape sphere0 = (SphereShape) col0.getCollisionShape();
    SphereShape sphere1 = (SphereShape) col1.getCollisionShape();
    Vector3f diff = stack.allocVector3f();
    diff.sub(col0.getWorldTransform(tmpTrans1).origin, col1.getWorldTransform(tmpTrans2).origin);
    float len = diff.length();
    float radius0 = sphere0.getRadius();
    float radius1 = sphere1.getRadius();
    // if distance positive, don't generate a new contact
    if (len > (radius0 + radius1)) {
        //#ifndef CLEAR_MANIFOLD
        resultOut.refreshContactPoints();
        //#endif //CLEAR_MANIFOLD
        return;
    }
    // distance (negative means penetration)
    float dist = len - (radius0 + radius1);
    Vector3f normalOnSurfaceB = stack.allocVector3f();
    normalOnSurfaceB.set(1f, 0f, 0f);
    if (len > BulletGlobals.FLT_EPSILON) {
        normalOnSurfaceB.scale(1f / len, diff);
    }
    Vector3f tmp = stack.allocVector3f();
    // point on A (worldspace)
    Vector3f pos0 = stack.allocVector3f();
    tmp.scale(radius0, normalOnSurfaceB);
    pos0.sub(col0.getWorldTransform(tmpTrans1).origin, tmp);
    // point on B (worldspace)
    Vector3f pos1 = stack.allocVector3f();
    tmp.scale(radius1, normalOnSurfaceB);
    pos1.add(col1.getWorldTransform(tmpTrans2).origin, tmp);
    // report a contact. internally this will be kept persistent, and contact reduction is done
    resultOut.addContactPoint(normalOnSurfaceB, pos1, dist);
    //#ifndef CLEAR_MANIFOLD
    resultOut.refreshContactPoints();
    //#endif //CLEAR_MANIFOLD
    stack.leave();
}
Also used : Vector3f(javax.vecmath.Vector3f) SphereShape(com.bulletphysics.collision.shapes.SphereShape) Transform(com.bulletphysics.linearmath.Transform) Stack(com.bulletphysics.util.Stack)

Example 5 with SphereShape

use of com.bulletphysics.collision.shapes.SphereShape in project bdx by GoranM.

the class DiscreteDynamicsWorld method integrateTransforms.

protected void integrateTransforms(float timeStep) {
    BulletStats.pushProfile("integrateTransforms");
    Stack stack = Stack.enter();
    int sp = stack.getSp();
    try {
        Vector3f tmp = stack.allocVector3f();
        Transform tmpTrans = stack.allocTransform();
        Transform predictedTrans = stack.allocTransform();
        for (int i = 0; i < collisionObjects.size(); i++) {
            CollisionObject colObj = collisionObjects.getQuick(i);
            RigidBody body = RigidBody.upcast(colObj);
            if (body != null) {
                body.setHitFraction(1f);
                if (body.isActive() && (!body.isStaticOrKinematicObject())) {
                    body.predictIntegratedTransform(timeStep, predictedTrans);
                    tmp.sub(predictedTrans.origin, body.getWorldTransform(tmpTrans).origin);
                    float squareMotion = tmp.lengthSquared();
                    if (body.getCcdSquareMotionThreshold() != 0f && body.getCcdSquareMotionThreshold() < squareMotion) {
                        BulletStats.pushProfile("CCD motion clamping");
                        try {
                            if (body.getCollisionShape().isConvex()) {
                                BulletStats.gNumClampedCcdMotions++;
                                ClosestNotMeConvexResultCallback sweepResults = new ClosestNotMeConvexResultCallback(body, body.getWorldTransform(tmpTrans).origin, predictedTrans.origin, getBroadphase().getOverlappingPairCache(), getDispatcher());
                                //ConvexShape convexShape = (ConvexShape)body.getCollisionShape();
                                //btConvexShape* convexShape = static_cast<btConvexShape*>(body->getCollisionShape());
                                SphereShape tmpSphere = new SphereShape(body.getCcdSweptSphereRadius());
                                sweepResults.collisionFilterGroup = body.getBroadphaseProxy().collisionFilterGroup;
                                sweepResults.collisionFilterMask = body.getBroadphaseProxy().collisionFilterMask;
                                convexSweepTest(tmpSphere, body.getWorldTransform(tmpTrans), predictedTrans, sweepResults);
                                // JAVA NOTE: added closestHitFraction test to prevent objects being stuck
                                if (sweepResults.hasHit() && (sweepResults.closestHitFraction > 0.0001f)) {
                                    body.setHitFraction(sweepResults.closestHitFraction);
                                    body.predictIntegratedTransform(timeStep * body.getHitFraction(), predictedTrans);
                                    body.setHitFraction(0f);
                                //System.out.printf("clamped integration to hit fraction = %f\n", sweepResults.closestHitFraction);
                                }
                            }
                        } finally {
                            BulletStats.popProfile();
                        }
                    }
                    body.proceedToTransform(predictedTrans);
                }
            }
        }
    } finally {
        stack.leave(sp);
        BulletStats.popProfile();
    }
}
Also used : CollisionObject(com.bulletphysics.collision.dispatch.CollisionObject) Vector3f(javax.vecmath.Vector3f) SphereShape(com.bulletphysics.collision.shapes.SphereShape) Transform(com.bulletphysics.linearmath.Transform) TypedConstraint(com.bulletphysics.dynamics.constraintsolver.TypedConstraint) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) Stack(com.bulletphysics.util.Stack)

Aggregations

SphereShape (com.bulletphysics.collision.shapes.SphereShape)6 Transform (com.bulletphysics.linearmath.Transform)5 Stack (com.bulletphysics.util.Stack)5 Vector3f (javax.vecmath.Vector3f)5 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)2 VoronoiSimplexSolver (com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver)2 CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)2 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)2 ConvexShape (com.bulletphysics.collision.shapes.ConvexShape)2 TypedConstraint (com.bulletphysics.dynamics.constraintsolver.TypedConstraint)2 CollisionObject (com.bulletphysics.collision.dispatch.CollisionObject)1 ConvexCast (com.bulletphysics.collision.narrowphase.ConvexCast)1 CastResult (com.bulletphysics.collision.narrowphase.ConvexCast.CastResult)1 GjkConvexCast (com.bulletphysics.collision.narrowphase.GjkConvexCast)1 SubsimplexConvexCast (com.bulletphysics.collision.narrowphase.SubsimplexConvexCast)1 BvhTriangleMeshShape (com.bulletphysics.collision.shapes.BvhTriangleMeshShape)1 CapsuleShape (com.bulletphysics.collision.shapes.CapsuleShape)1 ConeShape (com.bulletphysics.collision.shapes.ConeShape)1 CylinderShape (com.bulletphysics.collision.shapes.CylinderShape)1