Java源码示例:com.jme3.bullet.collision.shapes.CollisionShape

示例1
@Override
protected Sheet createSheet() {
    Sheet sheet = super.createSheet();
    Sheet.Set set = Sheet.createPropertiesSet();
    set.setDisplayName("GhostControl");
    set.setName(GhostControl.class.getName());
    GhostControl obj = geom;//getLookup().lookup(Spatial.class);
    if (obj == null) {
        return sheet;
    }

    set.put(makeProperty(obj, Vector3f.class, "getPhysicsLocation", "setPhysicsLocation", "Physics Location"));
    set.put(makeProperty(obj, Quaternion.class, "getPhysicsRotation", "setPhysicsRotation", "Physics Rotation"));
    
    set.put(makeProperty(obj, CollisionShape.class, "getCollisionShape", "setCollisionShape", "Collision Shape"));
    set.put(makeProperty(obj, int.class, "getCollisionGroup", "setCollisionGroup", "Collision Group"));
    set.put(makeProperty(obj, int.class, "getCollideWithGroups", "setCollideWithGroups", "Collide With Groups"));

    sheet.put(set);
    return sheet;

}
 
示例2
@Override
public void simpleInitApp() {
    stateManager.attach(bulletAppState);
    initCrossHair();

    Spatial s = assetManager.loadModel("Models/Elephant/Elephant.mesh.xml");
    s.setLocalScale(0.1f);

    CollisionShape collisionShape = CollisionShapeFactory.createMeshShape(s);
    Node n = new Node("elephant");
    n.addControl(new RigidBodyControl(collisionShape, 1));
    n.getControl(RigidBodyControl.class).setKinematic(true);
    bulletAppState.getPhysicsSpace().add(n);
    rootNode.attachChild(n);
    bulletAppState.setDebugEnabled(true);
}
 
示例3
private void buildPlayer() {
    spaceCraft = assetManager.loadModel("Models/HoverTank/Tank2.mesh.xml");
    CollisionShape colShape = CollisionShapeFactory.createDynamicMeshShape(spaceCraft);
    spaceCraft.setShadowMode(ShadowMode.CastAndReceive);
    spaceCraft.setLocalTranslation(new Vector3f(-140, 14, -23));
    spaceCraft.setLocalRotation(new Quaternion(new float[]{0, 0.01f, 0}));

    hoverControl = new PhysicsHoverControl(colShape, 500);
    hoverControl.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);

    spaceCraft.addControl(hoverControl);


    rootNode.attachChild(spaceCraft);
    getPhysicsSpace().add(hoverControl);

    ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
    spaceCraft.addControl(chaseCam);

    flyCam.setEnabled(false);
}
 
示例4
/**
 * Update this control. Invoked once per frame during the logical-state
 * update, provided the control is enabled and added to a scene. Should be
 * invoked only by a subclass or by AbstractControl.
 *
 * @param tpf the time interval between frames (in seconds, ≥0)
 */
@Override
protected void controlUpdate(float tpf) {
    CollisionShape newShape = body.getCollisionShape();
    Vector3f newScale = newShape.getScale();
    if (myShape != newShape || !oldScale.equals(newScale)) {
        myShape = newShape;
        oldScale.set(newScale);

        Node node = (Node) spatial;
        node.detachChild(geom);

        geom = DebugShapeFactory.getDebugShape(myShape);
        geom.setName(body.toString());

        node.attachChild(geom);
    }
    geom.setMaterial(debugAppState.DEBUG_PINK);

    body.getPhysicsLocation(location);
    applyPhysicsTransform(location, Quaternion.IDENTITY);
}
 
示例5
/**
 * Instantiate a purely kinematic link between the specified skeleton bone
 * and the specified rigid body.
 *
 * @param control the control that will manage this link (not null, alias
 * created)
 * @param bone the corresponding bone (not null, alias created)
 * @param collisionShape the desired shape (not null, alias created)
 * @param mass the mass (in physics-mass units)
 * @param localOffset the location of the body's center (in the bone's local
 * coordinates, not null, unaffected)
 */
PhysicsLink(DacLinks control, Joint bone, CollisionShape collisionShape,
        float mass, Vector3f localOffset) {
    assert control != null;
    assert bone != null;
    assert collisionShape != null;
    assert localOffset != null;

    this.control = control;
    this.bone = bone;
    rigidBody = createRigidBody(mass, collisionShape);

    logger.log(Level.FINE, "Creating link for bone {0} with mass={1}",
            new Object[]{bone.getName(), rigidBody.getMass()});

    this.localOffset = localOffset.clone();
    updateKPTransform();
}
 
示例6
/**
 * Create a jointless BoneLink for the named bone, and add it to the
 * boneLinks map.
 *
 * @param boneName the name of the bone to be linked (not null)
 * @param vertexLocations the set of vertex locations (not null, not empty)
 */
private void createBoneLink(String boneName, VectorSet vertexLocations) {
    Joint bone = findBone(boneName);
    Transform boneToMesh = bone.getModelTransform();
    Transform meshToBone = boneToMesh.invert();
    //logger3.log(Level.INFO, "meshToBone = {0}", meshToBone);
    /*
     * Create the CollisionShape and locate the center of mass.
     */
    CollisionShape shape;
    Vector3f center;
    if (vertexLocations == null || vertexLocations.numVectors() == 0) {
        throw new IllegalStateException("no vertex for " + boneName);
    } else {
        center = vertexLocations.mean(null);
        center.subtractLocal(bone.getModelTransform().getTranslation());
        shape = createShape(meshToBone, center, vertexLocations);
    }

    meshToBone.getTranslation().zero();
    float mass = super.mass(boneName);
    Vector3f offset = meshToBone.transformVector(center, null);
    BoneLink link = new BoneLink(this, bone, shape, mass, offset);
    boneLinks.put(boneName, link);
}
 
示例7
/**
 * This method creates a hull shape for the given Spatial.<br>
 * If you want to have mesh-accurate dynamic shapes (CPU intense!!!) use GImpact shapes, its probably best to do so with a low-poly version of your model.
 * @return A HullCollisionShape or a CompoundCollisionShape with HullCollisionShapes as children if the supplied spatial is a Node.
 */
public static CollisionShape createDynamicMeshShape(Spatial spatial) {
    if (spatial instanceof Geometry) {
        return createSingleDynamicMeshShape((Geometry) spatial, spatial);
    } else if (spatial instanceof Node) {
        return createCompoundShape((Node) spatial, (Node) spatial, new CompoundCollisionShape(), true, true);
    } else {
        throw new IllegalArgumentException("Supplied spatial must either be Node or Geometry!");
    }

}
 
示例8
@Override
@FxThread
public @NotNull Array<TreeNode<?>> getChildren(@NotNull final NodeTree<?> nodeTree) {

    final ChildCollisionShape element = getElement();
    final CollisionShape shape = element.shape;

    final Array<TreeNode<?>> result = ArrayFactory.newArray(TreeNode.class, 1);
    result.add(FACTORY_REGISTRY.createFor(shape));

    return result;
}
 
示例9
public PMDRigidBody(PMDNode pmdNode, Bone bone, int rigidBodyType, Vector3f pos, Quaternion rot, CollisionShape cs, float f) {
        super(cs, f);
        this.pmdNode = pmdNode;
        this.bone = bone;
        this.rigidBodyType = rigidBodyType;
        this.pos.set(pos);
        this.rot.set(rot);

        invPos.set(pos);
        invPos.negateLocal();
        invRot.set(rot);
        invRot.inverseLocal();
        invM.setTransform(pos, new Vector3f(1f, 1f, 1f), rot.toRotationMatrix());
        m.set(invM);
        invM.invertLocal();
        m2.loadIdentity();
        centerBone = pmdNode.getSkeleton().getBone("センター");
        if (bone == centerBone) {
            centerFlag = true;
        } else {
            centerFlag = false;
        }
//        if (bone != null) {
//            if (!isKinematic()) {
//                bone.setUseModelSpaceVectors(true);
//            }else {
//                bone.setUseModelSpaceVectors(false);
//            }
//        }
    }
 
示例10
@Override
@FxThread
protected @NotNull CollisionShape createShape(@NotNull final VarTable vars) {

    final float height = vars.getFloat(PROPERTY_HEIGHT);
    final float radius = vars.getFloat(PROPERTY_RADIUS);
    final Axis axis = vars.get(PROPERTY_AXIS);

    return new ConeCollisionShape(radius, height, axis.ordinal());
}
 
示例11
/**
 * Performs a sweep collision test and returns the results as a list of PhysicsSweepTestResults<br/>
 * You have to use different Transforms for start and end (at least distance > 0.4f).
 * SweepTest will not see a collision if it starts INSIDE an object and is moving AWAY from its center.
 */
public List<PhysicsSweepTestResult> sweepTest(CollisionShape shape, Transform start, Transform end, List<PhysicsSweepTestResult> results) {
    results.clear();
    if (!(shape.getCShape() instanceof ConvexShape)) {
        Logger.getLogger(PhysicsSpace.class.getName()).log(Level.WARNING, "Trying to sweep test with incompatible mesh shape!");
        return results;
    }
    dynamicsWorld.convexSweepTest((ConvexShape) shape.getCShape(), Converter.convert(start, sweepTrans1), Converter.convert(end, sweepTrans2), new InternalSweepListener(results));
    return results;
}
 
示例12
@Override
@FxThread
protected @NotNull CollisionShape createShape(@NotNull final VarTable vars) {
    final Vector3f halfExtents = vars.get(PROPERTY_HALF_EXTENTS);
    final Axis axis = vars.get(PROPERTY_AXIS);
    return new CylinderCollisionShape(halfExtents, axis.ordinal());
}
 
示例13
/**
     * Performs a sweep collision test and returns the results as a list of PhysicsSweepTestResults<br/>
     * You have to use different Transforms for start and end (at least distance > 0.4f).
     * SweepTest will not see a collision if it starts INSIDE an object and is moving AWAY from its center.
     */
    public List<PhysicsSweepTestResult> sweepTest(CollisionShape shape, Transform start, Transform end) {
        List<PhysicsSweepTestResult> results = new LinkedList<PhysicsSweepTestResult>();
//        if (!(shape.getCShape() instanceof ConvexShape)) {
//            Logger.getLogger(PhysicsSpace.class.getName()).log(Level.WARNING, "Trying to sweep test with incompatible mesh shape!");
//            return results;
//        }
//        dynamicsWorld.convexSweepTest((ConvexShape) shape.getCShape(), Converter.convert(start, sweepTrans1), Converter.convert(end, sweepTrans2), new InternalSweepListener(results));
        return results;

    }
 
示例14
private static Geometry createDebugShape(CollisionShape shape) {
    Geometry geom = new Geometry();
    geom.setMesh(DebugShapeFactory.getDebugMesh(shape));
    //        geom.setLocalScale(shape.getScale());
    geom.updateModelBound();
    return geom;
}
 
示例15
/**
     * @param shape The CollisionShape (no Mesh or CompoundCollisionShapes)
     * @param stepHeight The quantization size for vertical movement
     */
    public PhysicsCharacter(CollisionShape shape, float stepHeight) {
        this.collisionShape = shape;
//        if (shape instanceof MeshCollisionShape || shape instanceof CompoundCollisionShape) {
//            throw (new UnsupportedOperationException("Kinematic character nodes cannot have mesh or compound collision shapes"));
//        }
        this.stepHeight = stepHeight;
        buildObject();
    }
 
示例16
@Override
public void simpleInitApp() {
    flyCam.setEnabled(false);

    BulletAppState bulletAppState = new BulletAppState();
    bulletAppState.setDebugEnabled(true);
    bulletAppState.setSpeed(0f);
    stateManager.attach(bulletAppState);
    PhysicsSpace space = bulletAppState.getPhysicsSpace();

    float radius = 1f;
    CollisionShape sphere = new SphereCollisionShape(radius);
    CollisionShape box = new BoxCollisionShape(Vector3f.UNIT_XYZ);

    RigidBodyControl rbc = new RigidBodyControl(box);
    rbc.setEnabled(false);
    rbc.setPhysicsSpace(space);
    rootNode.addControl(rbc);

    BetterCharacterControl bcc = new BetterCharacterControl(radius, 4f, 1f);
    bcc.setEnabled(false);
    bcc.setPhysicsSpace(space);
    rootNode.addControl(bcc);

    GhostControl gc = new GhostControl(sphere);
    gc.setEnabled(false);
    gc.setPhysicsSpace(space);
    rootNode.addControl(gc);
}
 
示例17
@Override
protected Sheet createSheet() {
    Sheet sheet = super.createSheet();
    Sheet.Set set = Sheet.createPropertiesSet();
    set.setDisplayName("RigidBodyControl");
    set.setName(RigidBodyControl.class.getName());
    RigidBodyControl obj = geom;//getLookup().lookup(Spatial.class);
    if (obj == null) {
        return sheet;
    }

    set.put(makeProperty(obj, Vector3f.class, "getPhysicsLocation", "setPhysicsLocation", "Physics Location"));
    set.put(makeProperty(obj, Quaternion.class, "getPhysicsRotation", "setPhysicsRotation", "Physics Rotation"));

    set.put(makeProperty(obj, CollisionShape.class, "getCollisionShape", "setCollisionShape", "Collision Shape"));
    set.put(makeProperty(obj, int.class, "getCollisionGroup", "setCollisionGroup", "Collision Group"));
    set.put(makeProperty(obj, int.class, "getCollideWithGroups", "setCollideWithGroups", "Collide With Groups"));
    
    set.put(makeProperty(obj, float.class, "getFriction", "setFriction", "Friction"));
    set.put(makeProperty(obj, float.class, "getMass", "setMass", "Mass"));
    set.put(makeProperty(obj, boolean.class, "isKinematic", "setKinematic", "Kinematic"));
    set.put(makeProperty(obj, Vector3f.class, "getGravity", "setGravity", "Gravity"));
    set.put(makeProperty(obj, float.class, "getLinearDamping", "setLinearDamping", "Linear Damping"));
    set.put(makeProperty(obj, float.class, "getAngularDamping", "setAngularDamping", "Angular Damping"));
    set.put(makeProperty(obj, float.class, "getRestitution", "setRestitution", "Restitution"));

    set.put(makeProperty(obj, float.class, "getLinearSleepingThreshold", "setLinearSleepingThreshold", "Linear Sleeping Threshold"));
    set.put(makeProperty(obj, float.class, "getAngularSleepingThreshold", "setAngularSleepingThreshold", "Angular Sleeping Threshold"));

    sheet.put(set);
    return sheet;

}
 
示例18
/**
 * Add 3x3 terrain to the scene and the PhysicsSpace.
 */
private void addTerrain() {
    int patchSize = 3;
    int mapSize = 3;
    TerrainQuad quad
            = new TerrainQuad("terrain", patchSize, mapSize, nineHeights);
    rootNode.attachChild(quad);
    quad.setMaterial(quadMaterial);

    CollisionShape shape = CollisionShapeFactory.createMeshShape(quad);
    float massForStatic = 0f;
    RigidBodyControl rbc = new RigidBodyControl(shape, massForStatic);
    rbc.setPhysicsSpace(physicsSpace);
    quad.addControl(rbc);
}
 
示例19
/**
 * Creates a debug shape from the given collision shape. This is mostly used internally.<br>
 * To attach a debug shape to a physics object, call <code>attachDebugShape(AssetManager manager);</code> on it.
 * @param collisionShape
 * @return
 */
public static Spatial getDebugShape(CollisionShape collisionShape) {
    if (collisionShape == null) {
        return null;
    }
    Spatial debugShape;
    if (collisionShape instanceof CompoundCollisionShape) {
        CompoundCollisionShape shape = (CompoundCollisionShape) collisionShape;
        List<ChildCollisionShape> children = shape.getChildren();
        Node node = new Node("DebugShapeNode");
        for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) {
            ChildCollisionShape childCollisionShape = it.next();
            CollisionShape ccollisionShape = childCollisionShape.shape;
            Geometry geometry = createDebugShape(ccollisionShape);

            // apply translation
            geometry.setLocalTranslation(childCollisionShape.location);

            // apply rotation
            TempVars vars = TempVars.get();                
            Matrix3f tempRot = vars.tempMat3;

            tempRot.set(geometry.getLocalRotation());
            childCollisionShape.rotation.mult(tempRot, tempRot);
            geometry.setLocalRotation(tempRot);

            vars.release();

            node.attachChild(geometry);
        }
        debugShape = node;
    } else {
        debugShape = createDebugShape(collisionShape);
    }
    if (debugShape == null) {
        return null;
    }
    debugShape.updateGeometricState();
    return debugShape;
}
 
示例20
public void setCollisionShape(CollisionShape collisionShape) {
    super.setCollisionShape(collisionShape);
    if(collisionShape instanceof MeshCollisionShape && mass!=0){
        throw new IllegalStateException("Dynamic rigidbody can not have mesh collision shape!");
    }
    if (rBody == null) {
        rebuildRigidBody();
    } else {
        collisionShape.calculateLocalInertia(mass, localInertia);
        constructionInfo.collisionShape = collisionShape.getCShape();
        rBody.setCollisionShape(collisionShape.getCShape());
    }
}
 
示例21
/**
     * Performs a sweep collision test and returns the results as a list of PhysicsSweepTestResults<br/>
     * You have to use different Transforms for start and end (at least distance > 0.4f).
     * SweepTest will not see a collision if it starts INSIDE an object and is moving AWAY from its center.
     */
    public List<PhysicsSweepTestResult> sweepTest(CollisionShape shape, Transform start, Transform end, List<PhysicsSweepTestResult> results) {
        results.clear();
//        if (!(shape.getCShape() instanceof ConvexShape)) {
//            Logger.getLogger(PhysicsSpace.class.getName()).log(Level.WARNING, "Trying to sweep test with incompatible mesh shape!");
//            return results;
//        }
//        dynamicsWorld.convexSweepTest((ConvexShape) shape.getCShape(), Converter.convert(start, sweepTrans1), Converter.convert(end, sweepTrans2), new InternalSweepListener(results));
        return results;
    }
 
示例22
/**
 * Apply the specified CollisionShape to this object. Note that the object
 * should not be in any physics space while changing shape; the object gets
 * rebuilt on the physics side.
 *
 * @param collisionShape the shape to apply (not null, alias created)
 */
@Override
public void setCollisionShape(CollisionShape collisionShape) {
    super.setCollisionShape(collisionShape);
    if (objectId == 0) {
        buildObject();
    } else {
        attachCollisionShape(objectId, collisionShape.getObjectId());
    }
}
 
示例23
/**
     * Apply the specified CollisionShape to this character. Note that the
     * character should not be in any physics space while changing shape; the
     * character gets rebuilt on the physics side.
     *
     * @param collisionShape the shape to apply (not null, alias created)
     */
    @Override
    public void setCollisionShape(CollisionShape collisionShape) {
//        if (!(collisionShape.getObjectId() instanceof ConvexShape)) {
//            throw (new UnsupportedOperationException("Kinematic character nodes cannot have mesh collision shapes"));
//        }
        super.setCollisionShape(collisionShape);
        if (objectId == 0) {
            buildObject();
        } else {
            attachCollisionShape(objectId, collisionShape.getObjectId());
        }
    }
 
示例24
/**
 * @param shape The CollisionShape (no Mesh or CompoundCollisionShapes)
 * @param stepHeight The quantization size for vertical movement
 */
public PhysicsCharacter(CollisionShape shape, float stepHeight) {
    this.collisionShape = shape;
    if (!(shape.getCShape() instanceof ConvexShape)) {
        throw (new UnsupportedOperationException("Kinematic character nodes cannot have mesh collision shapes"));
    }
    this.stepHeight = stepHeight;
    buildObject();
}
 
示例25
/**
 * Performs a sweep collision test and returns the results as a list of PhysicsSweepTestResults<br/>
 * You have to use different Transforms for start and end (at least distance greater than 0.4f).
 * SweepTest will not see a collision if it starts INSIDE an object and is moving AWAY from its center.
 */
public List<PhysicsSweepTestResult> sweepTest(CollisionShape shape, Transform start, Transform end, List<PhysicsSweepTestResult> results) {
    results.clear();
    if (!(shape.getCShape() instanceof ConvexShape)) {
        logger.log(Level.WARNING, "Trying to sweep test with incompatible mesh shape!");
        return results;
    }
    dynamicsWorld.convexSweepTest((ConvexShape) shape.getCShape(), Converter.convert(start, sweepTrans1), Converter.convert(end, sweepTrans2), new InternalSweepListener(results));
    return results;
}
 
示例26
/**
 * Create a debug spatial from the specified collision shape.
 * <p>
 * This is mostly used internally. To attach a debug shape to a physics
 * object, call <code>attachDebugShape(AssetManager manager);</code> on it.
 *
 * @param collisionShape the shape to visualize (may be null, unaffected)
 * @return a new tree of geometries, or null
 */
public static Spatial getDebugShape(CollisionShape collisionShape) {
    if (collisionShape == null) {
        return null;
    }
    Spatial debugShape;
    if (collisionShape instanceof CompoundCollisionShape) {
        CompoundCollisionShape shape = (CompoundCollisionShape) collisionShape;
        List<ChildCollisionShape> children = shape.getChildren();
        Node node = new Node("DebugShapeNode");
        for (Iterator<ChildCollisionShape> it = children.iterator(); it.hasNext();) {
            ChildCollisionShape childCollisionShape = it.next();
            CollisionShape ccollisionShape = childCollisionShape.shape;
            Geometry geometry = createDebugShape(ccollisionShape);

            // apply translation
            geometry.setLocalTranslation(childCollisionShape.location);

            // apply rotation
            TempVars vars = TempVars.get();                
            Matrix3f tempRot = vars.tempMat3;

            tempRot.set(geometry.getLocalRotation());
            childCollisionShape.rotation.mult(tempRot, tempRot);
            geometry.setLocalRotation(tempRot);

            vars.release();

            node.attachChild(geometry);
        }
        debugShape = node;
    } else {
        debugShape = createDebugShape(collisionShape);
    }
    if (debugShape == null) {
        return null;
    }
    debugShape.updateGeometricState();
    return debugShape;
}
 
示例27
/**
     * Create a geometry for visualizing the specified shape.
     *
     * @param shape (not null, unaffected)
     * @return a new geometry (not null)
     */
    private static Geometry createDebugShape(CollisionShape shape) {
        Geometry geom = new Geometry();
        geom.setMesh(DebugShapeFactory.getDebugMesh(shape));
//        geom.setLocalScale(shape.getScale());
        geom.updateModelBound();
        return geom;
    }
 
示例28
public static CollisionShape createBoxShape(Spatial spatial) {
    if (spatial instanceof Geometry) {
        return createSingleBoxShape((Geometry) spatial, spatial);
    } else if (spatial instanceof Node) {
        return createBoxCompoundShape((Node) spatial);
    } else {
        throw new IllegalArgumentException("Supplied spatial must either be Node or Geometry!");
    }
}
 
示例29
/**
 * Update this control. Invoked once per frame during the logical-state
 * update, provided the control is enabled and added to a scene. Should be
 * invoked only by a subclass or by AbstractControl.
 *
 * @param tpf the time interval between frames (in seconds, &ge;0)
 */
@Override
protected void controlUpdate(float tpf) {
    CollisionShape newShape = body.getCollisionShape();
    Vector3f newScale = newShape.getScale();
    if (myShape != newShape || !oldScale.equals(newScale)) {
        myShape = newShape;
        oldScale.set(newScale);

        Node node = (Node) spatial;
        node.detachChild(geom);

        geom = DebugShapeFactory.getDebugShape(myShape);
        geom.setName(body.toString());

        node.attachChild(geom);
    }
    if(body.isActive()){
        geom.setMaterial(debugAppState.DEBUG_MAGENTA);
    }else{
        geom.setMaterial(debugAppState.DEBUG_BLUE);
    }

    body.getPhysicsLocation(location);
    body.getPhysicsRotation(rotation);
    applyPhysicsTransform(location, rotation);
}
 
示例30
@Override
    public void setCollisionShape(CollisionShape collisionShape) {
//        if (!(collisionShape.getObjectId() instanceof ConvexShape)) {
//            throw (new UnsupportedOperationException("Kinematic character nodes cannot have mesh collision shapes"));
//        }
        super.setCollisionShape(collisionShape);
        if (objectId == 0) {
            buildObject();
        } else {
            attachCollisionShape(objectId, collisionShape.getObjectId());
        }
    }