// ********************************************************************** // EXECUTIVE SUMMARY // Module Name: Entity.java // Description: Definition of the Entity class // Author: Kent A. Watsen, http://www.mbay.net/~watsen // ********************************************************************** package mil.navy.nps.eaiDemoBoids; // ********************************************************************** // IMPORTS // ********************************************************************** // Standard classes import java.*; import java.util.*; // EAI classes import vrml.external.Node; import vrml.external.Browser; import vrml.external.field.*; import vrml.external.exception.*; import netscape.javascript.JSObject; // NPS library stuff import mil.navy.nps.dis.*; import mil.navy.nps.math.*; // ********************************************************************** // CLASS // ********************************************************************** public class Entity extends Object { static final String vrmlString = "Shape { appearance Appearance { material Material { diffuseColor 1 1 1 } } geometry IndexedFaceSet { coord Coordinate { point [0 0 -3, -2 0 3, 2 0 3, 0 2 3] } coordIndex [0 2 1 -1, 0 1 3 -1, 0 3 2 -1, 1 2 3 -1] ccw FALSE} }"; static Hashtable entityContainer = null; Node myNode[] = null; Node myTransform[] = null; EventInSFVec3f myPos = null; EventInSFRotation myRot = null; float position[] = null; // x, y, z float rotation[] = null; // axis, angle (radians) float eulers[] = null; // h, p, r Quaternion tmpQuat = null; int currFrame = 0; EntityID entityID = null; EntityStatePdu espdu = null; float pduTime = 0.0f; public Entity (Browser browser, EntityID _entityID) { EventInMFNode tmpAddChildren = null; // first set passed args we wish to keep entityID = _entityID; // now create and add our entity container if (entityContainer == null) { entityContainer = new Hashtable(10); } entityContainer.put(entityID, this); myNode = browser.createVrmlFromString(vrmlString); myTransform = browser.createVrmlFromString("Transform {}" ); tmpAddChildren = (EventInMFNode)myTransform[0].getEventIn("addChildren"); tmpAddChildren.setValue(myNode); myPos = (EventInSFVec3f) myTransform[0].getEventIn("set_translation"); myRot = (EventInSFRotation) myTransform[0].getEventIn("set_rotation"); position = new float[3]; rotation = new float[4]; eulers = new float[3]; tmpQuat = new Quaternion(); // allocate up front so runtime efficient } public void dispose() { // remove entity from hashtable entityContainer.remove(entityID); } public void setESPDU(EntityStatePdu _espdu) { espdu = _espdu; position[0] = (float) espdu.getEntityLocationX(); position[1] = (float) espdu.getEntityLocationY(); position[2] = (float) espdu.getEntityLocationZ(); eulers[0] = (float) espdu.getEntityOrientationPsi(); // heading eulers[1] = (float) espdu.getEntityOrientationTheta(); // pitch eulers[2] = (float) espdu.getEntityOrientationPhi(); // roll pduTime = 0.0f; } public EntityStatePdu getESPDU() { return espdu; } public void cycle() { float dt = 0.1f; // assume 10Hz float hpr[] = null; // Increment timer pduTime = pduTime + dt; // linear dead reackon position[0] = position[0] + dt*(float)espdu.getEntityLinearVelocityX(); position[1] = position[1] + dt*(float)espdu.getEntityLinearVelocityY(); position[2] = position[2] + dt*(float)espdu.getEntityLinearVelocityZ(); eulers[0] = eulers[0] + dt*(float)espdu.getEntityAngularVelocityY(); // heading eulers[1] = eulers[1] + dt*(float)espdu.getEntityAngularVelocityX(); // pitch eulers[2] = eulers[2] + dt*(float)espdu.getEntityAngularVelocityZ(); // roll // now convert eulers to axis/angle tmpQuat.setEulers(eulers); tmpQuat.getAxisAngle(rotation); // now apply any changes in pos/rot myPos.setValue(position); myRot.setValue(rotation); } public static int getNum() { if (entityContainer == null) return 0; return entityContainer.size(); } public static Entity get(int index) { Enumeration enumeration = null; Entity entity = null; if (entityContainer == null) return null; if (index < 0 || index >= entityContainer.size()) return null; enumeration = entityContainer.elements(); for (int i=0; i<=index; i++) { entity = (Entity)enumeration.nextElement(); } return entity; } public static Entity get(EntityID _entityID) { if (entityContainer == null) return null; return (Entity)entityContainer.get(_entityID); } public Node[] getNode() { return myTransform; } public float getPduTime() { return pduTime; } }