Java Code Examples for com.jme3.scene.Spatial#getControl()

The following examples show how to use com.jme3.scene.Spatial#getControl() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SceneToolController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void attachPhysicsSelection(Spatial geom) {
    PhysicsCollisionObject control = geom.getControl(RigidBodyControl.class);
    if (control == null) {
        control = geom.getControl(VehicleControl.class);
    }
    if (control == null) {
        control = geom.getControl(GhostControl.class);
    }
    if (control == null) {
        control = geom.getControl(CharacterControl.class);
    }
    if (control == null) {
        return;
    }
    Spatial selectionGeometry = DebugShapeFactory.getDebugShape(control.getCollisionShape());
    if (selectionGeometry != null) {
        selectionGeometry.setMaterial(blueMat);
        selectionGeometry.setLocalTransform(geom.getWorldTransform());
        toolsNode.attachChild(selectionGeometry);
        selectionShape = selectionGeometry;
    }
}
 
Example 2
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Add all collision objects and joints in the specified subtree of the
 * scene graph to this space (e.g. after loading from disk). Note:
 * recursive!
 *
 * @param spatial the root of the subtree (not null)
 */
public void addAll(Spatial spatial) {
    add(spatial);

    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //add joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                //add(physicsJoint.getBodyB());
                add(physicsJoint);
            }
        }
    }
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            addAll(spat);
        }
    }
}
 
Example 3
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Remove all physics controls and joints in the specified subtree of the
 * scene graph from the physics space (e.g. before saving to disk) Note:
 * recursive!
 *
 * @param spatial the root of the subtree (not null)
 */
public void removeAll(Spatial spatial) {
    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //remove joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                removeJoint(physicsJoint);
                //remove(physicsJoint.getBodyB());
            }
        }
    }
        
    remove(spatial);
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            removeAll(spat);
        }
    }
}
 
Example 4
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * adds an object to the physics space
 * @param obj the PhysicsControl or Spatial with PhysicsControl to add
 */
public void add(Object obj) {
    if (obj == null) return;
    if (obj instanceof PhysicsControl) {
        ((PhysicsControl) obj).setPhysicsSpace(this);
    } else if (obj instanceof Spatial) {
        Spatial node = (Spatial) obj;
        for (int i = 0; i < node.getNumControls(); i++) {
            if (node.getControl(i) instanceof PhysicsControl) {
                add(node.getControl(i));
            }
        }
    } else if (obj instanceof PhysicsCollisionObject) {
        addCollisionObject((PhysicsCollisionObject) obj);
    } else if (obj instanceof PhysicsJoint) {
        addJoint((PhysicsJoint) obj);
    } else {
        throw (new UnsupportedOperationException("Cannot add this kind of object to the physics space."));
    }
}
 
Example 5
Source File: PhysicsSpace.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * adds an object to the physics space
 * @param obj the PhysicsControl or Spatial with PhysicsControl to add
 */
public void add(Object obj) {
    if (obj instanceof PhysicsControl) {
        ((PhysicsControl) obj).setPhysicsSpace(this);
    } else if (obj instanceof Spatial) {
        Spatial node = (Spatial) obj;
        PhysicsControl control = node.getControl(PhysicsControl.class);
        control.setPhysicsSpace(this);
    } else if (obj instanceof PhysicsCollisionObject) {
        addCollisionObject((PhysicsCollisionObject) obj);
    } else if (obj instanceof PhysicsJoint) {
        addJoint((PhysicsJoint) obj);
    } else {
        throw (new UnsupportedOperationException("Cannot add this kind of object to the physics space."));
    }
}
 
Example 6
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * removes an object from the physics space
 *
 * @param obj the PhysicsControl or Spatial with PhysicsControl to remove
 */
public void remove(Object obj) {
    if (obj == null) return;
    if (obj instanceof PhysicsControl) {
        ((PhysicsControl) obj).setPhysicsSpace(null);
    } else if (obj instanceof Spatial) {
        Spatial node = (Spatial) obj;
        for (int i = 0; i < node.getNumControls(); i++) {
            if (node.getControl(i) instanceof PhysicsControl) {
                remove(node.getControl(i));
            }
        }
    } else if (obj instanceof PhysicsCollisionObject) {
        removeCollisionObject((PhysicsCollisionObject) obj);
    } else if (obj instanceof PhysicsJoint) {
        removeJoint((PhysicsJoint) obj);
    } else {
        throw (new UnsupportedOperationException("Cannot remove this kind of object from the physics space."));
    }
}
 
Example 7
Source File: PhysicsSpace.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * removes an object from the physics space
 * @param obj the PhysicsControl or Spatial with PhysicsControl to remove
 */
public void remove(Object obj) {
    if (obj instanceof PhysicsControl) {
        ((PhysicsControl) obj).setPhysicsSpace(null);
    } else if (obj instanceof Spatial) {
        Spatial node = (Spatial) obj;
        PhysicsControl control = node.getControl(PhysicsControl.class);
        control.setPhysicsSpace(null);
    } else if (obj instanceof PhysicsCollisionObject) {
        removeCollisionObject((PhysicsCollisionObject) obj);
    } else if (obj instanceof PhysicsJoint) {
        removeJoint((PhysicsJoint) obj);
    } else if (obj == null) {
        return;
    } else {
        throw (new UnsupportedOperationException("Cannot remove this kind of object from the physics space."+obj));
    }
}
 
Example 8
Source File: WheelElementModelPropertyControl.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Check a spatial to have a edited wheel.
 *
 * @param spatial the spatial.
 * @return true if the spatial has this wheel.
 */
private boolean checkSpatial(@NotNull Spatial spatial) {

    var control = spatial.getControl(VehicleControl.class);
    if (control == null) {
        return false;
    }

    var numWheels = control.getNumWheels();

    for (var i = 0; i < numWheels; i++) {
        var wheel = control.getWheel(i);
        if (wheel == getEditObject()) {
            return true;
        }
    }

    return false;
}
 
Example 9
Source File: TestIssue1340.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private SkinningControl getSkinningControl(Spatial model) {
    SkinningControl control = model.getControl(SkinningControl.class);
    if (control != null) {
        return control;
    }

    if (model instanceof Node) {
        for (Spatial child : ((Node) model).getChildren()) {
            SkinningControl skinningControl = getSkinningControl(child);
            if (skinningControl != null) {
                return skinningControl;
            }
        }
    }

    return null;
}
 
Example 10
Source File: PhysicsSpace.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Removes all physics controls and joints in the given spatial from the physics space
 * (e.g. before saving to disk) - recursive if node
 * @param spatial the rootnode containing the physics objects
 */
public void removeAll(Spatial spatial) {
    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        //remove joints with physicsNode as BodyA
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            if (physicsNode.equals(physicsJoint.getBodyA())) {
                removeJoint(physicsJoint);
                //remove(physicsJoint.getBodyB());
            }
        }
    }
    
    remove(spatial);
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            removeAll(spat);
        }
    }
}
 
Example 11
Source File: KinematicRagdollControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Generate physics shapes and bone links for the skeleton.
 *
 * @param model the spatial with the model's SkeletonControl (not null)
 */
protected void scanSpatial(Spatial model) {
    AnimControl animControl = model.getControl(AnimControl.class);
    Map<Integer, List<Float>> pointsMap = null;
    if (weightThreshold == -1.0f) {
        pointsMap = RagdollUtils.buildPointMap(model);
    }

    skeleton = animControl.getSkeleton();
    skeleton.resetAndUpdate();
    for (int i = 0; i < skeleton.getRoots().length; i++) {
        Bone childBone = skeleton.getRoots()[i];
        if (childBone.getParent() == null) {
            logger.log(Level.FINE, "Found root bone in skeleton {0}", skeleton);
            boneRecursion(model, childBone, baseRigidBody, 1, pointsMap);
        }
    }
}
 
Example 12
Source File: MouseEventControl.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Convenience method that will remove the specified listeners
 *  from a Spatial only if a MouseEventControl already exists.
 */
public static void removeListenersFromSpatial( Spatial s, MouseListener... listeners ) {
    if( s == null ) {
        return;
    }
    MouseEventControl mec = s.getControl(MouseEventControl.class);        
    if( mec == null ) {
        return;
    } else {
        mec.listeners.removeAll(Arrays.asList(listeners));
    }
}
 
Example 13
Source File: MouseEventControl.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Convenience method that will add a MouseEventControl if it
 *  doesn't exist, while adding the specified listeners.
 */
public static void addListenersToSpatial( Spatial s, MouseListener... listeners ) {
    if( s == null ) {
        return;
    }
    MouseEventControl mec = s.getControl(MouseEventControl.class);
    if( mec == null ) {
        s.addControl(new MouseEventControl(listeners));
    } else {
        mec.listeners.addAll(Arrays.asList(listeners));
    }
}
 
Example 14
Source File: CursorEventControl.java    From Lemur with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *  Convenience method that will add a CursorEventControl if it
 *  doesn't exist, while adding the specified listeners.
 */
public static void addListenersToSpatial( Spatial s, CursorListener... listeners ) {
    if( s == null ) {
        return;
    }
    CursorEventControl cec = s.getControl(CursorEventControl.class);
    if( cec == null ) {
        s.addControl(new CursorEventControl(listeners));
    } else {
        cec.listeners.addAll(Arrays.asList(listeners));
    }
}
 
Example 15
Source File: TestOgreAnim.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
    flyCam.setMoveSpeed(10f);
    cam.setLocation(new Vector3f(6.4013605f, 7.488437f, 12.843031f));
    cam.setRotation(new Quaternion(-0.060740203f, 0.93925786f, -0.2398315f, -0.2378785f));

    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal());
    dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
    rootNode.addLight(dl);

    Spatial model = (Spatial) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    model.center();

    control = model.getControl(AnimControl.class);
    control.addListener(this);
    channel = control.createChannel();

    for (String anim : control.getAnimationNames())
        System.out.println(anim);

    channel.setAnim("stand");

    SkeletonControl skeletonControl = model.getControl(SkeletonControl.class);

    Box b = new Box(.25f,3f,.25f);
    Geometry item = new Geometry("Item", b);
    item.move(0, 1.5f, 0);
    item.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
    Node n = skeletonControl.getAttachmentsNode("hand.right");
    n.attachChild(item);

    rootNode.attachChild(model);

    inputManager.addListener(this, "Attack");
    inputManager.addMapping("Attack", new KeyTrigger(KeyInput.KEY_SPACE));
}
 
Example 16
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Build a map of mesh vertices in a subtree of the scene graph.
 *
 * @param model the root of the subtree (may be null)
 * @return a new map (not null)
 */
public static Map<Integer, List<Float>> buildPointMap(Spatial model) {
    Map<Integer, List<Float>> map = new HashMap<>();

    SkeletonControl skeletonCtrl = model.getControl(SkeletonControl.class);
    Mesh[] targetMeshes = skeletonCtrl.getTargets();
    for (Mesh mesh : targetMeshes) {
        buildPointMapForMesh(mesh, map);
    }

    return map;
}
 
Example 17
Source File: TestAnimMigration.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void setupModel(Spatial model) {
    if (composer != null) {
        return;
    }
    composer = model.getControl(AnimComposer.class);
    if (composer != null) {

        SkinningControl sc = model.getControl(SkinningControl.class);
        debugAppState.addArmatureFrom(sc);

        anims.clear();
        for (String name : composer.getAnimClipsNames()) {
            anims.add(name);
        }
        composer.actionSequence("Sequence1",
                composer.makeAction("Walk"),
                composer.makeAction("Run"),
                composer.makeAction("Jumping")).setSpeed(1);

        composer.actionSequence("Sequence2",
                composer.makeAction("Walk"),
                composer.makeAction("Run"),
                composer.makeAction("Jumping")).setSpeed(-1);

        action = composer.actionBlended("Blend", new LinearBlendSpace(1, 4),
                "Walk", "Run");

        action.getBlendSpace().setValue(1);

        composer.action("Walk").setSpeed(-1);

        composer.makeLayer("LeftArm", ArmatureMask.createMask(sc.getArmature(), "shoulder.L"));

        anims.addFirst("Blend");
        anims.addFirst("Sequence2");
        anims.addFirst("Sequence1");

        if (anims.isEmpty()) {
            return;
        }
        if (playAnim) {
            String anim = anims.poll();
            anims.add(anim);
            composer.setCurrentAction(anim);
            System.err.println(anim);
        }

    } else {
        if (model instanceof Node) {
            Node n = (Node) model;
            for (Spatial child : n.getChildren()) {
                setupModel(child);
            }
        }
    }

}
 
Example 18
Source File: KinematicRagdollControl.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Create spatial-dependent data. Invoked when this control is added to a
 * scene.
 *
 * @param model the controlled spatial (not null)
 */
@Override
protected void createSpatialData(Spatial model) {
    targetModel = model;
    Node parent = model.getParent();


    Vector3f initPosition = model.getLocalTranslation().clone();
    Quaternion initRotation = model.getLocalRotation().clone();
    initScale = model.getLocalScale().clone();

    model.removeFromParent();
    model.setLocalTranslation(Vector3f.ZERO);
    model.setLocalRotation(Quaternion.IDENTITY);
    model.setLocalScale(1);
    //HACK ALERT change this
    //I remove the skeletonControl and readd it to the spatial to make sure it's after the ragdollControl in the stack
    //Find a proper way to order the controls.
    SkeletonControl sc = model.getControl(SkeletonControl.class);
    if(sc == null){
        throw new IllegalArgumentException("The root node of the model should have a SkeletonControl. Make sure the control is there and that it's not on a sub node.");
    }
    model.removeControl(sc);
    model.addControl(sc);

    if (boneList.isEmpty()) {
        // add all bones to the list
        skeleton = sc.getSkeleton();
        for (int boneI = 0; boneI < skeleton.getBoneCount(); boneI++) {
            String boneName = skeleton.getBone(boneI).getName();
            boneList.add(boneName);
        }
    }
    // filter out bones without vertices
    filterBoneList(sc);

    if (boneList.isEmpty()) {
        throw new IllegalArgumentException(
                "No suitable bones were found in the model's skeleton.");
    }

    // put into bind pose and compute bone transforms in model space
    // maybe don't reset to ragdoll out of animations?
    scanSpatial(model);


    if (parent != null) {
        parent.attachChild(model);

    }
    model.setLocalTranslation(initPosition);
    model.setLocalRotation(initRotation);
    model.setLocalScale(initScale);

    if (added) {
        addPhysics(space);
    }
    logger.log(Level.FINE, "Created physics ragdoll for skeleton {0}", skeleton);
}
 
Example 19
Source File: PhysicsSpace.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * adds all physics controls and joints in the given spatial node to the physics space
 * (e.g. after loading from disk) - recursive if node
 * @param spatial the rootnode containing the physics objects
 */
public void addAll(Spatial spatial) {
    if (spatial.getControl(RigidBodyControl.class) != null) {
        RigidBodyControl physicsNode = spatial.getControl(RigidBodyControl.class);
        if (!physicsNodes.containsValue(physicsNode)) {
            physicsNode.setPhysicsSpace(this);
        }
        //add joints
        List<PhysicsJoint> joints = physicsNode.getJoints();
        for (Iterator<PhysicsJoint> it1 = joints.iterator(); it1.hasNext();) {
            PhysicsJoint physicsJoint = it1.next();
            //add connected physicsnodes if they are not already added
            if (!physicsNodes.containsValue(physicsJoint.getBodyA())) {
                if (physicsJoint.getBodyA() instanceof PhysicsControl) {
                    add(physicsJoint.getBodyA());
                } else {
                    addRigidBody(physicsJoint.getBodyA());
                }
            }
            if (!physicsNodes.containsValue(physicsJoint.getBodyB())) {
                if (physicsJoint.getBodyA() instanceof PhysicsControl) {
                    add(physicsJoint.getBodyB());
                } else {
                    addRigidBody(physicsJoint.getBodyB());
                }
            }
            if (!physicsJoints.contains(physicsJoint)) {
                addJoint(physicsJoint);
            }
        }
    } else if (spatial.getControl(PhysicsControl.class) != null) {
        spatial.getControl(PhysicsControl.class).setPhysicsSpace(this);
    }
    //recursion
    if (spatial instanceof Node) {
        List<Spatial> children = ((Node) spatial).getChildren();
        for (Iterator<Spatial> it = children.iterator(); it.hasNext();) {
            Spatial spat = it.next();
            addAll(spat);
        }
    }
}
 
Example 20
Source File: KinematicRagdollControl.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setSpatial(Spatial model) {
    if (model == null) {
        removeFromPhysicsSpace();
        clearData();
        return;
    }
    targetModel = model;
    Node parent = model.getParent();


    Vector3f initPosition = model.getLocalTranslation().clone();
    Quaternion initRotation = model.getLocalRotation().clone();
    initScale = model.getLocalScale().clone();

    model.removeFromParent();
    model.setLocalTranslation(Vector3f.ZERO);
    model.setLocalRotation(Quaternion.IDENTITY);
    model.setLocalScale(1);
    //HACK ALERT change this
    //I remove the skeletonControl and readd it to the spatial to make sure it's after the ragdollControl in the stack
    //Find a proper way to order the controls.
    SkeletonControl sc = model.getControl(SkeletonControl.class);
    model.removeControl(sc);
    model.addControl(sc);
    //---- 

    removeFromPhysicsSpace();
    clearData();
    // put into bind pose and compute bone transforms in model space
    // maybe dont reset to ragdoll out of animations?
    scanSpatial(model);


    if (parent != null) {
        parent.attachChild(model);

    }
    model.setLocalTranslation(initPosition);
    model.setLocalRotation(initRotation);
    model.setLocalScale(initScale);

    logger.log(Level.INFO, "Created physics ragdoll for skeleton {0}", skeleton);
}