Search in sources :

Example 1 with CylinderShape

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

the class CylinderCollisionShape method createShape.

protected void createShape() {
    switch(axis) {
        case 0:
            cShape = new CylinderShapeX(Converter.convert(halfExtents));
            break;
        case 1:
            cShape = new CylinderShape(Converter.convert(halfExtents));
            break;
        case 2:
            cShape = new CylinderShapeZ(Converter.convert(halfExtents));
            break;
    }
    cShape.setLocalScaling(Converter.convert(getScale()));
    cShape.setMargin(margin);
}
Also used : CylinderShape(com.bulletphysics.collision.shapes.CylinderShape) CylinderShapeX(com.bulletphysics.collision.shapes.CylinderShapeX) CylinderShapeZ(com.bulletphysics.collision.shapes.CylinderShapeZ)

Example 2 with CylinderShape

use of com.bulletphysics.collision.shapes.CylinderShape in project Terasology by MovingBlocks.

the class BulletPhysics method getShapeFor.

/**
 * Returns the shape belonging to the given entity. It currently knows 4
 * different shapes: Sphere, Capsule, Cylinder or arbitrary.
 * The shape is determined based on the shape component of the given entity.
 * If the entity has somehow got multiple shapes, only one is picked. The
 * order of priority is: Sphere, Capsule, Cylinder, arbitrary.
 * <br><br>
 * TODO: Flyweight this (take scale as parameter)
 *
 * @param entity the entity to get the shape of.
 * @return the shape of the entity, ready to be used by Bullet.
 */
private ConvexShape getShapeFor(EntityRef entity) {
    BoxShapeComponent box = entity.getComponent(BoxShapeComponent.class);
    if (box != null) {
        Vector3f halfExtents = new Vector3f(VecMath.to(box.extents));
        halfExtents.scale(0.5f);
        return new BoxShape(halfExtents);
    }
    SphereShapeComponent sphere = entity.getComponent(SphereShapeComponent.class);
    if (sphere != null) {
        return new SphereShape(sphere.radius);
    }
    CapsuleShapeComponent capsule = entity.getComponent(CapsuleShapeComponent.class);
    if (capsule != null) {
        return new CapsuleShape(capsule.radius, capsule.height);
    }
    CylinderShapeComponent cylinder = entity.getComponent(CylinderShapeComponent.class);
    if (cylinder != null) {
        return new CylinderShape(new Vector3f(cylinder.radius, 0.5f * cylinder.height, cylinder.radius));
    }
    HullShapeComponent hull = entity.getComponent(HullShapeComponent.class);
    if (hull != null) {
        ObjectArrayList<Vector3f> verts = new ObjectArrayList<>();
        TFloatIterator iterator = hull.sourceMesh.getVertices().iterator();
        while (iterator.hasNext()) {
            Vector3f newVert = new Vector3f();
            newVert.x = iterator.next();
            newVert.y = iterator.next();
            newVert.z = iterator.next();
            verts.add(newVert);
        }
        return new ConvexHullShape(verts);
    }
    CharacterMovementComponent characterMovementComponent = entity.getComponent(CharacterMovementComponent.class);
    if (characterMovementComponent != null) {
        return new CapsuleShape(characterMovementComponent.radius, characterMovementComponent.height);
    }
    logger.error("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has neither. Entity: {}", entity);
    throw new IllegalArgumentException("Creating physics object that requires a ShapeComponent or CharacterMovementComponent, but has neither. Entity: " + entity);
}
Also used : SphereShapeComponent(org.terasology.physics.components.shapes.SphereShapeComponent) ConvexHullShape(com.bulletphysics.collision.shapes.ConvexHullShape) CylinderShapeComponent(org.terasology.physics.components.shapes.CylinderShapeComponent) HullShapeComponent(org.terasology.physics.components.shapes.HullShapeComponent) CharacterMovementComponent(org.terasology.logic.characters.CharacterMovementComponent) CapsuleShapeComponent(org.terasology.physics.components.shapes.CapsuleShapeComponent) CylinderShape(com.bulletphysics.collision.shapes.CylinderShape) ObjectArrayList(com.bulletphysics.util.ObjectArrayList) Vector3f(javax.vecmath.Vector3f) TFloatIterator(gnu.trove.iterator.TFloatIterator) BoxShapeComponent(org.terasology.physics.components.shapes.BoxShapeComponent) SphereShape(com.bulletphysics.collision.shapes.SphereShape) BoxShape(com.bulletphysics.collision.shapes.BoxShape) CapsuleShape(com.bulletphysics.collision.shapes.CapsuleShape)

Example 3 with CylinderShape

use of com.bulletphysics.collision.shapes.CylinderShape 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)

Aggregations

CylinderShape (com.bulletphysics.collision.shapes.CylinderShape)3 CapsuleShape (com.bulletphysics.collision.shapes.CapsuleShape)2 SphereShape (com.bulletphysics.collision.shapes.SphereShape)2 Vector3f (javax.vecmath.Vector3f)2 ManifoldPoint (com.bulletphysics.collision.narrowphase.ManifoldPoint)1 BoxShape (com.bulletphysics.collision.shapes.BoxShape)1 CollisionShape (com.bulletphysics.collision.shapes.CollisionShape)1 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)1 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)1 ConeShape (com.bulletphysics.collision.shapes.ConeShape)1 ConvexHullShape (com.bulletphysics.collision.shapes.ConvexHullShape)1 CylinderShapeX (com.bulletphysics.collision.shapes.CylinderShapeX)1 CylinderShapeZ (com.bulletphysics.collision.shapes.CylinderShapeZ)1 PolyhedralConvexShape (com.bulletphysics.collision.shapes.PolyhedralConvexShape)1 TypedConstraint (com.bulletphysics.dynamics.constraintsolver.TypedConstraint)1 Transform (com.bulletphysics.linearmath.Transform)1 ObjectArrayList (com.bulletphysics.util.ObjectArrayList)1 Stack (com.bulletphysics.util.Stack)1 TFloatIterator (gnu.trove.iterator.TFloatIterator)1 CharacterMovementComponent (org.terasology.logic.characters.CharacterMovementComponent)1