Search in sources :

Example 11 with CollisionShape

use of com.bulletphysics.collision.shapes.CollisionShape 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);
    }
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) CompoundShape(com.bulletphysics.collision.shapes.CompoundShape)

Example 12 with CollisionShape

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

the class ConvexTriangleCallback method processTriangle.

public void processTriangle(Vector3f[] triangle, int partId, int triangleIndex) {
    // just for debugging purposes
    //printf("triangle %d",m_triangleCount++);
    // aabb filter is already applied!	
    ci.dispatcher1 = dispatcher;
    CollisionObject ob = (CollisionObject) triBody;
    // debug drawing of the overlapping triangles
    if (dispatchInfoPtr != null && dispatchInfoPtr.debugDraw != null && dispatchInfoPtr.debugDraw.getDebugMode() > 0) {
        Stack stack = Stack.enter();
        Vector3f color = stack.allocVector3f();
        color.set(255, 255, 0);
        Transform tr = ob.getWorldTransform(stack.allocTransform());
        Vector3f tmp1 = stack.allocVector3f();
        Vector3f tmp2 = stack.allocVector3f();
        tmp1.set(triangle[0]);
        tr.transform(tmp1);
        tmp2.set(triangle[1]);
        tr.transform(tmp2);
        dispatchInfoPtr.debugDraw.drawLine(tmp1, tmp2, color);
        tmp1.set(triangle[1]);
        tr.transform(tmp1);
        tmp2.set(triangle[2]);
        tr.transform(tmp2);
        dispatchInfoPtr.debugDraw.drawLine(tmp1, tmp2, color);
        tmp1.set(triangle[2]);
        tr.transform(tmp1);
        tmp2.set(triangle[0]);
        tr.transform(tmp2);
        dispatchInfoPtr.debugDraw.drawLine(tmp1, tmp2, color);
        //btVector3 center = triangle[0] + triangle[1]+triangle[2];
        //center *= btScalar(0.333333);
        //m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[0]),tr(center),color);
        //m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[1]),tr(center),color);
        //m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[2]),tr(center),color);
        stack.leave();
    }
    if (convexBody.getCollisionShape().isConvex()) {
        tm.init(triangle[0], triangle[1], triangle[2]);
        tm.setMargin(collisionMarginTriangle);
        CollisionShape tmpShape = ob.getCollisionShape();
        ob.internalSetTemporaryCollisionShape(tm);
        CollisionAlgorithm colAlgo = ci.dispatcher1.findAlgorithm(convexBody, triBody, manifoldPtr);
        // this should use the btDispatcher, so the actual registered algorithm is used
        //		btConvexConvexAlgorithm cvxcvxalgo(m_manifoldPtr,ci,m_convexBody,m_triBody);
        resultOut.setShapeIdentifiers(-1, -1, partId, triangleIndex);
        //cvxcvxalgo.setShapeIdentifiers(-1,-1,partId,triangleIndex);
        //cvxcvxalgo.processCollision(m_convexBody,m_triBody,*m_dispatchInfoPtr,m_resultOut);
        colAlgo.processCollision(convexBody, triBody, dispatchInfoPtr, resultOut);
        //colAlgo.destroy();
        ci.dispatcher1.freeCollisionAlgorithm(colAlgo);
        ob.internalSetTemporaryCollisionShape(tmpShape);
    }
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) CollisionAlgorithm(com.bulletphysics.collision.broadphase.CollisionAlgorithm) Vector3f(javax.vecmath.Vector3f) Transform(com.bulletphysics.linearmath.Transform) Stack(com.bulletphysics.util.Stack)

Example 13 with CollisionShape

use of com.bulletphysics.collision.shapes.CollisionShape 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();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) ConcaveShape(com.bulletphysics.collision.shapes.ConcaveShape) ConeShape(com.bulletphysics.collision.shapes.ConeShape) CompoundShape(com.bulletphysics.collision.shapes.CompoundShape) TypedConstraint(com.bulletphysics.dynamics.constraintsolver.TypedConstraint) ManifoldPoint(com.bulletphysics.collision.narrowphase.ManifoldPoint) Stack(com.bulletphysics.util.Stack) CylinderShape(com.bulletphysics.collision.shapes.CylinderShape) Vector3f(javax.vecmath.Vector3f) PolyhedralConvexShape(com.bulletphysics.collision.shapes.PolyhedralConvexShape) SphereShape(com.bulletphysics.collision.shapes.SphereShape) Transform(com.bulletphysics.linearmath.Transform) CapsuleShape(com.bulletphysics.collision.shapes.CapsuleShape)

Example 14 with CollisionShape

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

the class GImpactCollisionAlgorithm method gimpact_vs_gimpact.

public void gimpact_vs_gimpact(CollisionObject body0, CollisionObject body1, GImpactShapeInterface shape0, GImpactShapeInterface shape1) {
    if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE) {
        GImpactMeshShape meshshape0 = (GImpactMeshShape) shape0;
        part0 = meshshape0.getMeshPartCount();
        while ((part0--) != 0) {
            gimpact_vs_gimpact(body0, body1, meshshape0.getMeshPart(part0), shape1);
        }
        return;
    }
    if (shape1.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE) {
        GImpactMeshShape meshshape1 = (GImpactMeshShape) shape1;
        part1 = meshshape1.getMeshPartCount();
        while ((part1--) != 0) {
            gimpact_vs_gimpact(body0, body1, shape0, meshshape1.getMeshPart(part1));
        }
        return;
    }
    Stack stack = Stack.enter();
    Transform orgtrans0 = body0.getWorldTransform(stack.allocTransform());
    Transform orgtrans1 = body1.getWorldTransform(stack.allocTransform());
    PairSet pairset = tmpPairset;
    pairset.clear();
    gimpact_vs_gimpact_find_pairs(orgtrans0, orgtrans1, shape0, shape1, pairset);
    if (pairset.size() == 0) {
        stack.leave();
        return;
    }
    if (shape0.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE_PART && shape1.getGImpactShapeType() == ShapeType.TRIMESH_SHAPE_PART) {
        GImpactMeshShapePart shapepart0 = (GImpactMeshShapePart) shape0;
        GImpactMeshShapePart shapepart1 = (GImpactMeshShapePart) shape1;
        //specialized function
        //#ifdef BULLET_TRIANGLE_COLLISION
        //collide_gjk_triangles(body0,body1,shapepart0,shapepart1,&pairset[0].m_index1,pairset.size());
        //#else
        collide_sat_triangles(body0, body1, shapepart0, shapepart1, pairset, pairset.size());
        //#endif
        stack.leave();
        return;
    }
    // general function
    shape0.lockChildShapes();
    shape1.lockChildShapes();
    GIM_ShapeRetriever retriever0 = new GIM_ShapeRetriever(shape0);
    GIM_ShapeRetriever retriever1 = new GIM_ShapeRetriever(shape1);
    boolean child_has_transform0 = shape0.childrenHasTransform();
    boolean child_has_transform1 = shape1.childrenHasTransform();
    Transform tmpTrans = stack.allocTransform();
    int i = pairset.size();
    while ((i--) != 0) {
        Pair pair = pairset.get(i);
        triface0 = pair.index1;
        triface1 = pair.index2;
        CollisionShape colshape0 = retriever0.getChildShape(triface0);
        CollisionShape colshape1 = retriever1.getChildShape(triface1);
        if (child_has_transform0) {
            tmpTrans.mul(orgtrans0, shape0.getChildTransform(triface0));
            body0.setWorldTransform(tmpTrans);
        }
        if (child_has_transform1) {
            tmpTrans.mul(orgtrans1, shape1.getChildTransform(triface1));
            body1.setWorldTransform(tmpTrans);
        }
        // collide two convex shapes
        convex_vs_convex_collision(body0, body1, colshape0, colshape1);
        if (child_has_transform0) {
            body0.setWorldTransform(orgtrans0);
        }
        if (child_has_transform1) {
            body1.setWorldTransform(orgtrans1);
        }
    }
    shape0.unlockChildShapes();
    shape1.unlockChildShapes();
    stack.leave();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) Transform(com.bulletphysics.linearmath.Transform) Stack(com.bulletphysics.util.Stack)

Example 15 with CollisionShape

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

the class GImpactCollisionAlgorithm method gimpact_vs_compoundshape.

public void gimpact_vs_compoundshape(CollisionObject body0, CollisionObject body1, GImpactShapeInterface shape0, CompoundShape shape1, boolean swapped) {
    Stack stack = Stack.enter();
    Transform orgtrans1 = body1.getWorldTransform(stack.allocTransform());
    Transform childtrans1 = stack.allocTransform();
    Transform tmpTrans = stack.allocTransform();
    int i = shape1.getNumChildShapes();
    while ((i--) != 0) {
        CollisionShape colshape1 = shape1.getChildShape(i);
        childtrans1.mul(orgtrans1, shape1.getChildTransform(i, tmpTrans));
        body1.setWorldTransform(childtrans1);
        // collide child shape
        gimpact_vs_shape(body0, body1, shape0, colshape1, swapped);
        // restore transforms
        body1.setWorldTransform(orgtrans1);
    }
    stack.leave();
}
Also used : CollisionShape(com.bulletphysics.collision.shapes.CollisionShape) Transform(com.bulletphysics.linearmath.Transform) Stack(com.bulletphysics.util.Stack)

Aggregations

CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)17 Transform (com.bulletphysics.linearmath.Transform)11 Stack (com.bulletphysics.util.Stack)10 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)7 Vector3f (javax.vecmath.Vector3f)7 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)3 CollisionAlgorithm (com.bulletphysics.collision.broadphase.CollisionAlgorithm)2 CastResult (com.bulletphysics.collision.narrowphase.ConvexCast.CastResult)2 SubsimplexConvexCast (com.bulletphysics.collision.narrowphase.SubsimplexConvexCast)2 VoronoiSimplexSolver (com.bulletphysics.collision.narrowphase.VoronoiSimplexSolver)2 BvhTriangleMeshShape (com.bulletphysics.collision.shapes.BvhTriangleMeshShape)2 ConvexShape (com.bulletphysics.collision.shapes.ConvexShape)2 SphereShape (com.bulletphysics.collision.shapes.SphereShape)2 Matrix4f (javax.vecmath.Matrix4f)2 Matrix4 (com.badlogic.gdx.math.Matrix4)1 Vector3 (com.badlogic.gdx.math.Vector3)1 ConvexCast (com.bulletphysics.collision.narrowphase.ConvexCast)1 GjkConvexCast (com.bulletphysics.collision.narrowphase.GjkConvexCast)1 GjkEpaPenetrationDepthSolver (com.bulletphysics.collision.narrowphase.GjkEpaPenetrationDepthSolver)1 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)1