home *** CD-ROM | disk | FTP | other *** search
/ Sony Community Place / BROWSER / APP / CPB20A2.EXE / DATA.Z / Beebee.java < prev    next >
Encoding:
Java Source  |  1997-01-30  |  3.9 KB  |  148 lines

  1. /*
  2.  *    File    : beebee.java
  3.  *    Version : 2.01 1997.01.29
  4.  *    Auther  : cori co.
  5.  *
  6.  *    (c)Copyright 1996 Sony Corporation. All rights reserved.
  7.  */
  8.  
  9. import vrml.*;
  10. import vrml.field.*;
  11. import vrml.node.*;
  12. import java.util.*;
  13. import vs.*;
  14. import java.lang.*;
  15.  
  16. public class Beebee extends Script {
  17.   final int X = 0;
  18.   final int Y = 1;
  19.   final int Z = 2;
  20.   final int DEGREE = 3;
  21.     
  22.   SFVec3f    BeeTranslation;
  23.   SFRotation    BeeRotation;
  24.     
  25.   float BeePosition[] = new float[3];
  26.   float p_pos[] = new float[3];
  27.   float n_pos[] = new float[3];
  28.   float UserPosition[] = new float[3];
  29.   float UserOrientation[] = new float[4];
  30.  
  31.   int   BeeAngry = 0;
  32.  
  33.   public void initialize() {
  34.     BeeTranslation =
  35.       (SFVec3f) getEventOut( "BeeTranslation" );
  36.     BeeRotation = (SFRotation)getEventOut( "BeeRotation" );
  37.         
  38.     BeePosition[X] = BeePosition[Y] = BeePosition[Z] = 0.0f;
  39.     UserPosition[X] = 0.0f;
  40.     UserPosition[Y] = 0.0f;
  41.     UserPosition[Z] = 20.0f;
  42.     UserOrientation[X] = UserOrientation[Z] = 0.0f;
  43.     UserOrientation[Y] =  1.0f;    
  44.     UserOrientation[DEGREE] = 0.0f;
  45.   }
  46.  
  47.   public void processEvent( Event e ) {
  48.     String name = e.getName();
  49.  
  50.     try {
  51.       if(name.equals("HoneycomTouchSensorIsActive"))
  52.     HoneycomTouchSensorIsActive(e);
  53.       else if(name.equals("PositionChanged"))
  54.     ProximitySensorPositionChanged(e);
  55.       else if(name.equals("OrientationChanged"))
  56.     ProximitySensorOrientationChanged(e);
  57.  
  58.       else if(name.equals("BeeTimeSensorCycleTime"))
  59.     BeeTimeSensorCycleTime(e);
  60.     } catch (NullPointerException n) {
  61.       System.out.println("Error! NullPointerException");
  62.     } catch (Throwable t) {
  63.       System.out.println("Error! Throwable");
  64.     }
  65.   }
  66.   
  67.   float randMinMax(float min, float max){
  68.     float value, range;
  69.     double rnd;
  70.     
  71.     range = max - min;
  72.     rnd = Math.random();
  73.     value = (float)(rnd * (double)range) + min;
  74.     return value;
  75.   }
  76.  
  77.   public void HoneycomTouchSensorIsActive ( Event e ){
  78.     ConstSFBool mouse_down = (ConstSFBool)e.getValue();
  79.     
  80.     if (mouse_down.getValue()) return;    /* mouseDown */
  81.     
  82.     if (BeeAngry > 0){
  83.       BeeAngry = 0;
  84.     }else{
  85.       double rnd;
  86.       BeeAngry = 300;
  87.       
  88.       if((rnd = Math.random())<0.5)
  89.     BeePosition[X] += 3.0f*rnd;
  90.       else
  91.     BeePosition[X] -= 3.0f*rnd;
  92.       if((rnd = Math.random())<0.5)
  93.     BeePosition[Y] += 3.0f*rnd;
  94.       else
  95.     BeePosition[Y] -= 3.0f*rnd;
  96.       if((rnd = Math.random())<0.5)
  97.     BeePosition[Z] += 3.0f*rnd;
  98.       else
  99.     BeePosition[Z] -= 3.0f*rnd;
  100.     } 
  101.   }
  102.  
  103.   public void ProximitySensorPositionChanged(Event e) {
  104.     ((ConstSFVec3f)e.getValue()).getValue(UserPosition); 
  105.   }
  106.  
  107.   public void ProximitySensorOrientationChanged(Event e) {
  108.     ((ConstSFRotation)e.getValue()).getValue(UserOrientation);
  109.   }
  110.  
  111.   public void BeeTimeSensorCycleTime(Event e){
  112.     float speed = 20.0f;
  113.     float f = 0.2f;
  114.     float distance;
  115.     float accel;
  116.         
  117.     if (BeeAngry > 0){
  118.       BeeAngry = BeeAngry - 1;
  119.       p_pos[X] = UserPosition[X] - (float)Math.sin(UserOrientation[DEGREE]);
  120.       p_pos[Y] = UserPosition[Y];
  121.       p_pos[Z] = UserPosition[Z] - (float)Math.cos(UserOrientation[DEGREE]);
  122.     }else{
  123.       p_pos[X] = p_pos[Y] = p_pos[Z] = 0.0f;
  124.     }
  125.     distance = p_pos[X] - BeePosition[X];
  126.     if(Math.abs(distance) < 0.3){
  127.       accel = 2.0f;
  128.     }else{
  129.       accel = 4.0f;
  130.     }
  131.     n_pos[X] = BeePosition[X] + (distance/speed)+randMinMax(-accel*f, accel*f);
  132.         
  133.     distance = p_pos[Y] - BeePosition[Y];
  134.     if(Math.abs(distance) < 0.3){
  135.       accel = 2.0f;
  136.     }else{
  137.       accel = 4.0f;
  138.     }
  139.     n_pos[Y] = BeePosition[Y] + (distance/speed)+randMinMax(-accel*f, accel*f);
  140.     n_pos[Z] = BeePosition[Z] + ((p_pos[Z] - BeePosition[Z])/speed)+randMinMax (-f, f);
  141.     
  142.     BeeTranslation.setValue(n_pos);
  143.     BeeRotation.setValue(UserOrientation);
  144.     BeePosition = n_pos;
  145.   }
  146. }
  147.  
  148.