Search in sources :

Example 26 with Entry

use of com.jme3.util.IntMap.Entry in project jmonkeyengine by jMonkeyEngine.

the class SimulationNode method simulateSkeleton.

/**
     * Simulates the bone node.
     */
private void simulateSkeleton() {
    LOGGER.fine("Simulating skeleton.");
    Set<Long> alteredOmas = new HashSet<Long>();
    if (animations != null) {
        TempVars vars = TempVars.get();
        AnimChannel animChannel = animControl.createChannel();
        for (Animation animation : animations) {
            float[] animationTimeBoundaries = this.computeAnimationTimeBoundaries(animation);
            int maxFrame = (int) animationTimeBoundaries[0];
            float maxTime = animationTimeBoundaries[1];
            Map<Integer, VirtualTrack> tracks = new HashMap<Integer, VirtualTrack>();
            for (int frame = 0; frame < maxFrame; ++frame) {
                // this MUST be done here, otherwise setting next frame of animation will
                // lead to possible errors
                this.reset();
                // first set proper time for all bones in all the tracks ...
                for (Track track : animation.getTracks()) {
                    float time = ((BoneTrack) track).getTimes()[frame];
                    track.setTime(time, 1, animControl, animChannel, vars);
                    skeleton.updateWorldVectors();
                }
                // ... and then apply constraints from the root bone to the last child ...
                Set<Long> applied = new HashSet<Long>();
                for (Bone rootBone : skeleton.getRoots()) {
                    // ignore the 0-indexed bone
                    if (skeleton.getBoneIndex(rootBone) > 0) {
                        this.applyConstraints(rootBone, alteredOmas, applied, frame, new Stack<Bone>());
                    }
                }
                // ... add virtual tracks if neccessary, for bones that were altered but had no tracks before ...
                for (Long boneOMA : alteredOmas) {
                    BoneContext boneContext = blenderContext.getBoneContext(boneOMA);
                    int boneIndex = skeleton.getBoneIndex(boneContext.getBone());
                    if (!tracks.containsKey(boneIndex)) {
                        tracks.put(boneIndex, new VirtualTrack(boneContext.getBone().getName(), maxFrame, maxTime));
                    }
                }
                alteredOmas.clear();
                // ... and fill in another frame in the result track
                for (Entry<Integer, VirtualTrack> trackEntry : tracks.entrySet()) {
                    Bone bone = skeleton.getBone(trackEntry.getKey());
                    Transform startTransform = boneStartTransforms.get(bone);
                    // track contains differences between the frame position and bind positions of bones/spatials
                    Vector3f bonePositionDifference = bone.getLocalPosition().subtract(startTransform.getTranslation());
                    Quaternion boneRotationDifference = startTransform.getRotation().inverse().mult(bone.getLocalRotation()).normalizeLocal();
                    Vector3f boneScaleDifference = bone.getLocalScale().divide(startTransform.getScale());
                    trackEntry.getValue().setTransform(frame, new Transform(bonePositionDifference, boneRotationDifference, boneScaleDifference));
                }
            }
            for (Entry<Integer, VirtualTrack> trackEntry : tracks.entrySet()) {
                Track newTrack = trackEntry.getValue().getAsBoneTrack(trackEntry.getKey());
                if (newTrack != null) {
                    boolean trackReplaced = false;
                    for (Track track : animation.getTracks()) {
                        if (((BoneTrack) track).getTargetBoneIndex() == trackEntry.getKey().intValue()) {
                            animation.removeTrack(track);
                            animation.addTrack(newTrack);
                            trackReplaced = true;
                            break;
                        }
                    }
                    if (!trackReplaced) {
                        animation.addTrack(newTrack);
                    }
                }
            }
        }
        vars.release();
        animControl.clearChannels();
        this.reset();
    }
}
Also used : HashMap(java.util.HashMap) Quaternion(com.jme3.math.Quaternion) AnimChannel(com.jme3.animation.AnimChannel) TempVars(com.jme3.util.TempVars) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) Vector3f(com.jme3.math.Vector3f) Animation(com.jme3.animation.Animation) Bone(com.jme3.animation.Bone) Transform(com.jme3.math.Transform) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track) HashSet(java.util.HashSet)

Example 27 with Entry

use of com.jme3.util.IntMap.Entry in project jmonkeyengine by jMonkeyEngine.

the class BulletDebugAppState method updateGhosts.

private void updateGhosts() {
    HashMap<PhysicsGhostObject, Spatial> oldObjects = ghosts;
    ghosts = new HashMap<PhysicsGhostObject, Spatial>();
    Collection<PhysicsGhostObject> current = space.getGhostObjectList();
    //create new map
    for (Iterator<PhysicsGhostObject> it = current.iterator(); it.hasNext(); ) {
        PhysicsGhostObject physicsObject = it.next();
        //copy existing spatials
        if (oldObjects.containsKey(physicsObject)) {
            Spatial spat = oldObjects.get(physicsObject);
            ghosts.put(physicsObject, spat);
            oldObjects.remove(physicsObject);
        } else {
            if (filter == null || filter.displayObject(physicsObject)) {
                logger.log(Level.FINE, "Create new debug GhostObject");
                //create new spatial
                Node node = new Node(physicsObject.toString());
                node.addControl(new BulletGhostObjectDebugControl(this, physicsObject));
                ghosts.put(physicsObject, node);
                physicsDebugRootNode.attachChild(node);
            }
        }
    }
    //remove leftover spatials
    for (Map.Entry<PhysicsGhostObject, Spatial> entry : oldObjects.entrySet()) {
        PhysicsGhostObject object = entry.getKey();
        Spatial spatial = entry.getValue();
        spatial.removeFromParent();
    }
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) PhysicsGhostObject(com.jme3.bullet.objects.PhysicsGhostObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Entry

use of com.jme3.util.IntMap.Entry in project jmonkeyengine by jMonkeyEngine.

the class BulletDebugAppState method updateJoints.

private void updateJoints() {
    HashMap<PhysicsJoint, Spatial> oldObjects = joints;
    joints = new HashMap<PhysicsJoint, Spatial>();
    Collection<PhysicsJoint> current = space.getJointList();
    //create new map
    for (Iterator<PhysicsJoint> it = current.iterator(); it.hasNext(); ) {
        PhysicsJoint physicsObject = it.next();
        //copy existing spatials
        if (oldObjects.containsKey(physicsObject)) {
            Spatial spat = oldObjects.get(physicsObject);
            joints.put(physicsObject, spat);
            oldObjects.remove(physicsObject);
        } else {
            if (filter == null || filter.displayObject(physicsObject)) {
                logger.log(Level.FINE, "Create new debug Joint");
                //create new spatial
                Node node = new Node(physicsObject.toString());
                node.addControl(new BulletJointDebugControl(this, physicsObject));
                joints.put(physicsObject, node);
                physicsDebugRootNode.attachChild(node);
            }
        }
    }
    //remove leftover spatials
    for (Map.Entry<PhysicsJoint, Spatial> entry : oldObjects.entrySet()) {
        PhysicsJoint object = entry.getKey();
        Spatial spatial = entry.getValue();
        spatial.removeFromParent();
    }
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) PhysicsJoint(com.jme3.bullet.joints.PhysicsJoint) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with Entry

use of com.jme3.util.IntMap.Entry in project jmonkeyengine by jMonkeyEngine.

the class BulletDebugAppState method updateVehicles.

private void updateVehicles() {
    HashMap<PhysicsVehicle, Spatial> oldObjects = vehicles;
    vehicles = new HashMap<PhysicsVehicle, Spatial>();
    Collection<PhysicsVehicle> current = space.getVehicleList();
    //create new map
    for (Iterator<PhysicsVehicle> it = current.iterator(); it.hasNext(); ) {
        PhysicsVehicle physicsObject = it.next();
        //copy existing spatials
        if (oldObjects.containsKey(physicsObject)) {
            Spatial spat = oldObjects.get(physicsObject);
            vehicles.put(physicsObject, spat);
            oldObjects.remove(physicsObject);
        } else {
            if (filter == null || filter.displayObject(physicsObject)) {
                logger.log(Level.FINE, "Create new debug Vehicle");
                //create new spatial
                Node node = new Node(physicsObject.toString());
                node.addControl(new BulletVehicleDebugControl(this, physicsObject));
                vehicles.put(physicsObject, node);
                physicsDebugRootNode.attachChild(node);
            }
        }
    }
    //remove leftover spatials
    for (Map.Entry<PhysicsVehicle, Spatial> entry : oldObjects.entrySet()) {
        PhysicsVehicle object = entry.getKey();
        Spatial spatial = entry.getValue();
        spatial.removeFromParent();
    }
}
Also used : PhysicsVehicle(com.jme3.bullet.objects.PhysicsVehicle) Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with Entry

use of com.jme3.util.IntMap.Entry in project jmonkeyengine by jMonkeyEngine.

the class BulletDebugAppState method updateRigidBodies.

private void updateRigidBodies() {
    HashMap<PhysicsRigidBody, Spatial> oldObjects = bodies;
    bodies = new HashMap<PhysicsRigidBody, Spatial>();
    Collection<PhysicsRigidBody> current = space.getRigidBodyList();
    //create new map
    for (Iterator<PhysicsRigidBody> it = current.iterator(); it.hasNext(); ) {
        PhysicsRigidBody physicsObject = it.next();
        //copy existing spatials
        if (oldObjects.containsKey(physicsObject)) {
            Spatial spat = oldObjects.get(physicsObject);
            bodies.put(physicsObject, spat);
            oldObjects.remove(physicsObject);
        } else {
            if (filter == null || filter.displayObject(physicsObject)) {
                logger.log(Level.FINE, "Create new debug RigidBody");
                //create new spatial
                Node node = new Node(physicsObject.toString());
                node.addControl(new BulletRigidBodyDebugControl(this, physicsObject));
                bodies.put(physicsObject, node);
                physicsDebugRootNode.attachChild(node);
            }
        }
    }
    //remove leftover spatials
    for (Map.Entry<PhysicsRigidBody, Spatial> entry : oldObjects.entrySet()) {
        PhysicsRigidBody object = entry.getKey();
        Spatial spatial = entry.getValue();
        spatial.removeFromParent();
    }
}
Also used : Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) PhysicsRigidBody(com.jme3.bullet.objects.PhysicsRigidBody) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)17 Map (java.util.Map)13 ArrayList (java.util.ArrayList)11 List (java.util.List)11 Vector2f (com.jme3.math.Vector2f)8 Vector3f (com.jme3.math.Vector3f)7 Node (com.jme3.scene.Node)6 Spatial (com.jme3.scene.Spatial)6 VertexBuffer (com.jme3.scene.VertexBuffer)5 Material (com.jme3.material.Material)4 FloatBuffer (java.nio.FloatBuffer)4 Bone (com.jme3.animation.Bone)3 Geometry (com.jme3.scene.Geometry)3 Mesh (com.jme3.scene.Mesh)3 Pointer (com.jme3.scene.plugins.blender.file.Pointer)3 Structure (com.jme3.scene.plugins.blender.file.Structure)3 Face (com.jme3.scene.plugins.blender.meshes.Face)3 Image (com.jme3.texture.Image)3 IntMap (com.jme3.util.IntMap)3 ByteBuffer (java.nio.ByteBuffer)3