home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-20 | 1.2 KB | 46 lines |
- //
- // an agent is floating randomly.
- //
-
- import java.util.*;
- import vrml.*;
- import vrml.node.*;
- import vrml.field.*;
-
- public class FloatingAgent extends Script{
- SFVec3f setAgentPosition;
- float agentPosition[] = new float[3];
- Random randomNumGenerator = new Random();
-
- public void initialize(){
- // get the reference of the event-out 'setAgentPosition'.
- setAgentPosition = (SFVec3f)getEventOut("setAgentPosition");
-
- // initialize the agent position.
- agentPosition[0] = 0.0f;
- agentPosition[1] = 0.0f;
- agentPosition[2] = 0.0f;
- }
-
- public void processEvent(Event e){
- if(e.getName().equals("interval") == true){
- moveAgent();
- }
- }
-
- // generate random float value ranging between -0.1 to 0.1.
- float generateRandomFloat(){
- return(randomNumGenerator.nextFloat() * 0.2f - 0.1f);
- }
-
- // move the agent randomly.
- void moveAgent(){
- agentPosition[0] += generateRandomFloat();
- agentPosition[1] += generateRandomFloat();
- agentPosition[2] += generateRandomFloat();
-
- // move the agent to the new position.
- setAgentPosition.setValue(agentPosition);
- }
- }
-