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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations