Search in sources :

Example 16 with CollisionShape

use of com.jme3.bullet.collision.shapes.CollisionShape in project jmonkeyengine by jMonkeyEngine.

the class VehicleControl method cloneForSpatial.

@Override
public Control cloneForSpatial(Spatial spatial) {
    VehicleControl control = new VehicleControl(collisionShape, mass);
    control.setAngularFactor(getAngularFactor());
    control.setAngularSleepingThreshold(getAngularSleepingThreshold());
    control.setAngularVelocity(getAngularVelocity());
    control.setCcdMotionThreshold(getCcdMotionThreshold());
    control.setCcdSweptSphereRadius(getCcdSweptSphereRadius());
    control.setCollideWithGroups(getCollideWithGroups());
    control.setCollisionGroup(getCollisionGroup());
    control.setDamping(getLinearDamping(), getAngularDamping());
    control.setFriction(getFriction());
    control.setGravity(getGravity());
    control.setKinematic(isKinematic());
    control.setLinearSleepingThreshold(getLinearSleepingThreshold());
    control.setLinearVelocity(getLinearVelocity());
    control.setPhysicsLocation(getPhysicsLocation());
    control.setPhysicsRotation(getPhysicsRotationMatrix());
    control.setRestitution(getRestitution());
    control.setFrictionSlip(getFrictionSlip());
    control.setMaxSuspensionTravelCm(getMaxSuspensionTravelCm());
    control.setSuspensionStiffness(getSuspensionStiffness());
    control.setSuspensionCompression(tuning.suspensionCompression);
    control.setSuspensionDamping(tuning.suspensionDamping);
    control.setMaxSuspensionForce(getMaxSuspensionForce());
    for (Iterator<VehicleWheel> it = wheels.iterator(); it.hasNext(); ) {
        VehicleWheel wheel = it.next();
        VehicleWheel newWheel = control.addWheel(wheel.getLocation(), wheel.getDirection(), wheel.getAxle(), wheel.getRestLength(), wheel.getRadius(), wheel.isFrontWheel());
        newWheel.setFrictionSlip(wheel.getFrictionSlip());
        newWheel.setMaxSuspensionTravelCm(wheel.getMaxSuspensionTravelCm());
        newWheel.setSuspensionStiffness(wheel.getSuspensionStiffness());
        newWheel.setWheelsDampingCompression(wheel.getWheelsDampingCompression());
        newWheel.setWheelsDampingRelaxation(wheel.getWheelsDampingRelaxation());
        newWheel.setMaxSuspensionForce(wheel.getMaxSuspensionForce());
        //TODO: bad way finding children!
        if (spatial instanceof Node) {
            Node node = (Node) spatial;
            Spatial wheelSpat = node.getChild(wheel.getWheelSpatial().getName());
            if (wheelSpat != null) {
                newWheel.setWheelSpatial(wheelSpat);
            }
        }
    }
    control.setApplyPhysicsLocal(isApplyPhysicsLocal());
    return control;
}
Also used : Spatial(com.jme3.scene.Spatial) VehicleWheel(com.jme3.bullet.objects.VehicleWheel) Node(com.jme3.scene.Node)

Example 17 with CollisionShape

use of com.jme3.bullet.collision.shapes.CollisionShape in project jmonkeyengine by jMonkeyEngine.

the class CollisionShapeFactory method getTransform.

/**
     * returns the correct transform for a collisionshape in relation
     * to the ancestor for which the collisionshape is generated
     * @param spat
     * @param parent
     * @return
     */
private static Transform getTransform(Spatial spat, Spatial parent) {
    Transform shapeTransform = new Transform();
    Spatial parentNode = spat.getParent() != null ? spat.getParent() : spat;
    Spatial currentSpatial = spat;
    //if we have parents combine their transforms
    while (parentNode != null) {
        if (parent == currentSpatial) {
            //real parent -> only apply scale, not transform
            Transform trans = new Transform();
            trans.setScale(currentSpatial.getLocalScale());
            shapeTransform.combineWithParent(trans);
            parentNode = null;
        } else {
            shapeTransform.combineWithParent(currentSpatial.getLocalTransform());
            parentNode = currentSpatial.getParent();
            currentSpatial = parentNode;
        }
    }
    return shapeTransform;
}
Also used : Transform(com.jme3.math.Transform)

Example 18 with CollisionShape

use of com.jme3.bullet.collision.shapes.CollisionShape in project jmonkeyengine by jMonkeyEngine.

the class CollisionShapeFactory method shiftCompoundShapeContents.

/**
     * This method moves each child shape of a compound shape by the given vector
     * @param vector
     */
public static void shiftCompoundShapeContents(CompoundCollisionShape compoundShape, Vector3f vector) {
    for (Iterator<ChildCollisionShape> it = new LinkedList(compoundShape.getChildren()).iterator(); it.hasNext(); ) {
        ChildCollisionShape childCollisionShape = it.next();
        CollisionShape child = childCollisionShape.shape;
        Vector3f location = childCollisionShape.location;
        Matrix3f rotation = childCollisionShape.rotation;
        compoundShape.removeChildShape(child);
        compoundShape.addChildShape(child, location.add(vector), rotation);
    }
}
Also used : ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) Matrix3f(com.jme3.math.Matrix3f) Vector3f(com.jme3.math.Vector3f) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) LinkedList(java.util.LinkedList)

Example 19 with CollisionShape

use of com.jme3.bullet.collision.shapes.CollisionShape in project jmonkeyengine by jMonkeyEngine.

the class CollisionShapeFactory method createCompoundShape.

private static CompoundCollisionShape createCompoundShape(Node realRootNode, Node rootNode, CompoundCollisionShape shape, boolean meshAccurate, boolean dynamic) {
    for (Spatial spatial : rootNode.getChildren()) {
        if (spatial instanceof TerrainQuad) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainQuad terrain = (TerrainQuad) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), trans.getScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Node) {
            createCompoundShape(realRootNode, (Node) spatial, shape, meshAccurate, dynamic);
        } else if (spatial instanceof TerrainPatch) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            TerrainPatch terrain = (TerrainPatch) spatial;
            Transform trans = getTransform(spatial, realRootNode);
            shape.addChildShape(new HeightfieldCollisionShape(terrain.getHeightMap(), terrain.getLocalScale()), trans.getTranslation(), trans.getRotation().toRotationMatrix());
        } else if (spatial instanceof Geometry) {
            Boolean bool = spatial.getUserData(UserData.JME_PHYSICSIGNORE);
            if (bool != null && bool.booleanValue()) {
                // go to the next child in the loop
                continue;
            }
            if (meshAccurate) {
                CollisionShape childShape = dynamic ? createSingleDynamicMeshShape((Geometry) spatial, realRootNode) : createSingleMeshShape((Geometry) spatial, realRootNode);
                if (childShape != null) {
                    Transform trans = getTransform(spatial, realRootNode);
                    shape.addChildShape(childShape, trans.getTranslation(), trans.getRotation().toRotationMatrix());
                }
            } else {
                Transform trans = getTransform(spatial, realRootNode);
                shape.addChildShape(createSingleBoxShape(spatial, realRootNode), trans.getTranslation(), trans.getRotation().toRotationMatrix());
            }
        }
    }
    return shape;
}
Also used : ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) Transform(com.jme3.math.Transform) TerrainQuad(com.jme3.terrain.geomipmap.TerrainQuad) TerrainPatch(com.jme3.terrain.geomipmap.TerrainPatch)

Example 20 with CollisionShape

use of com.jme3.bullet.collision.shapes.CollisionShape in project jmonkeyengine by jMonkeyEngine.

the class DebugShapeFactory method getDebugShape.

/** The maximum corner for the aabb used for triangles to include in ConcaveShape processing.*/
//    private static final Vector3f aabbMax = new Vector3f(1e30f, 1e30f, 1e30f);
/** The minimum corner for the aabb used for triangles to include in ConcaveShape processing.*/
//    private static final Vector3f aabbMin = new Vector3f(-1e30f, -1e30f, -1e30f);
/**
     * 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;
}
Also used : Geometry(com.jme3.scene.Geometry) CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) CollisionShape(com.jme3.bullet.collision.shapes.CollisionShape) CompoundCollisionShape(com.jme3.bullet.collision.shapes.CompoundCollisionShape) Spatial(com.jme3.scene.Spatial) Matrix3f(com.jme3.math.Matrix3f) Node(com.jme3.scene.Node) ChildCollisionShape(com.jme3.bullet.collision.shapes.infos.ChildCollisionShape) TempVars(com.jme3.util.TempVars)

Aggregations

CollisionShape (com.jme3.bullet.collision.shapes.CollisionShape)8 Vector3f (com.jme3.math.Vector3f)7 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)6 Geometry (com.jme3.scene.Geometry)6 Node (com.jme3.scene.Node)6 Matrix3f (com.jme3.math.Matrix3f)5 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)4 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)4 Spatial (com.jme3.scene.Spatial)4 CompoundCollisionShape (com.jme3.bullet.collision.shapes.CompoundCollisionShape)3 Mesh (com.jme3.scene.Mesh)3 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 Transform (com.bulletphysics.linearmath.Transform)2 BulletAppState (com.jme3.bullet.BulletAppState)2 CapsuleCollisionShape (com.jme3.bullet.collision.shapes.CapsuleCollisionShape)2 VehicleWheel (com.jme3.bullet.objects.VehicleWheel)2 Transform (com.jme3.math.Transform)2 Box (com.jme3.scene.shape.Box)2 TempVars (com.jme3.util.TempVars)2 ConcaveShape (com.bulletphysics.collision.shapes.ConcaveShape)1