home *** CD-ROM | disk | FTP | other *** search
/ Netscape Plug-Ins Developer's Kit / Netscape_Plug-Ins_Developers_Kit.iso / plugins / WIRL / wirlb1g.exe / data.1 / VRCRobot.java < prev    next >
Encoding:
Java Source  |  1995-03-11  |  17.7 KB  |  632 lines

  1. import java.*;
  2. import VRCAPI;
  3.  
  4. public class VRCRobot implements Runnable
  5. {
  6.     VRCObject robot;
  7.     VRCObject upper_body;
  8.  
  9.     VRCObject top_lower;
  10.     VRCObject top;
  11.  
  12.     VRCObject r_arm_peg;
  13.     VRCObject r_arm_low;
  14.     VRCObject r_arm_up;
  15.  
  16.     VRCObject l_arm_peg;
  17.     VRCObject l_arm_low;
  18.     VRCObject l_arm_up;
  19.  
  20.     VRCObject torso;
  21.     VRCObject r_arm;
  22.     VRCObject r_forearm;
  23.     VRCObject r_hand;
  24.     VRCObject l_arm;
  25.     VRCObject l_forearm;
  26.     VRCObject l_hand;
  27.     VRCObject r_leg;
  28.     VRCObject r_calf;
  29.     VRCObject r_foot;
  30.     VRCObject l_leg;
  31.     VRCObject l_calf;
  32.     VRCObject l_foot;
  33.     VRCObject mid;
  34.     VRCObject window;
  35.     VRCObject l_leg_peg;
  36.     VRCObject r_leg_peg;
  37.  
  38.  
  39.     VRCAPI VreamApi;
  40.     int ActiveWindow;
  41.     boolean KeepRunning;
  42.  
  43.     public VRCRobot()
  44.     {
  45.         //start the rendering timer
  46.         VreamApi = new VRCAPI();
  47.         //get the current window (plug-in window)
  48.         //you will need to send this to each object constructor
  49.         ActiveWindow = VreamApi.ActiveHWND;
  50.         //create a VRCObject for every object named in the world
  51.         //call the constructor with the object name and the active window
  52.         robot = new VRCObject("robot",ActiveWindow);
  53.         upper_body = new VRCObject("upper_body",ActiveWindow);
  54.         torso = new VRCObject("torso",ActiveWindow);
  55.         r_arm = new VRCObject("r_arm",ActiveWindow);
  56.         r_forearm = new VRCObject("r_forearm",ActiveWindow);
  57.         r_hand = new VRCObject("r_hand",ActiveWindow);
  58.         l_arm = new VRCObject("l_arm",ActiveWindow);
  59.         l_forearm = new VRCObject("l_forearm",ActiveWindow);
  60.         l_hand = new VRCObject("l_hand",ActiveWindow);
  61.         r_leg = new VRCObject("r_leg",ActiveWindow);
  62.         r_calf = new VRCObject("r_calf",ActiveWindow);
  63.         r_foot = new VRCObject("r_foot",ActiveWindow);
  64.         l_leg = new VRCObject("l_leg",ActiveWindow);
  65.         l_calf = new VRCObject("l_calf",ActiveWindow);
  66.         l_foot = new VRCObject("l_foot",ActiveWindow);
  67.         mid = new VRCObject("mid",ActiveWindow);
  68.         top_lower = new VRCObject("top_lower",ActiveWindow);
  69.         top = new VRCObject("top",ActiveWindow);
  70.         r_arm_peg = new VRCObject("r_arm_peg",ActiveWindow);
  71.         r_arm_low = new VRCObject("r_arm_low",ActiveWindow);
  72.         r_arm_up = new VRCObject("r_arm_up",ActiveWindow);
  73.         l_arm_peg = new VRCObject("l_arm_peg",ActiveWindow);
  74.         l_arm_low = new VRCObject("l_arm_low",ActiveWindow);
  75.         l_arm_up = new VRCObject("l_arm_up",ActiveWindow);
  76.         window = new VRCObject("window",ActiveWindow);
  77.         l_leg_peg = new VRCObject("l_leg_peg",ActiveWindow);
  78.         r_leg_peg = new VRCObject("r_leg_peg",ActiveWindow);
  79.     }
  80.  
  81.     public void run(String sCommand)
  82.     {
  83.         if (sCommand.equals("walk")) walk();
  84.         if (sCommand.equals("animate")) animate();
  85.     }
  86.  
  87.     public void run()
  88.     {
  89.         //System.out.println("Robot run");
  90.         KeepRunning = true;
  91.         while(KeepRunning){
  92.         }
  93.     }
  94.  
  95.     public void destroy()
  96.     {
  97.         robot.deactivate();
  98.         upper_body.deactivate();
  99.         torso.deactivate();
  100.         r_arm.deactivate();
  101.         r_forearm.deactivate();
  102.         r_hand.deactivate();
  103.         l_arm.deactivate();
  104.         l_forearm.deactivate();
  105.         l_hand.deactivate();
  106.         r_leg.deactivate();
  107.         r_calf.deactivate();
  108.         r_foot.deactivate();
  109.         l_leg.deactivate();
  110.         l_calf.deactivate();
  111.         l_foot.deactivate();
  112.         mid.deactivate();
  113.         top_lower.deactivate();
  114.         top.deactivate();
  115.         r_arm_peg.deactivate();
  116.         r_arm_low.deactivate();
  117.         r_arm_up.deactivate();
  118.         l_arm_peg.deactivate();
  119.         l_arm_low.deactivate();
  120.         l_arm_up.deactivate();
  121.         window.deactivate();
  122.         l_leg_peg.deactivate();
  123.         r_leg_peg.deactivate();
  124.         //kill the timer
  125.         VreamApi.destroy();
  126.         KeepRunning = false;
  127.         //experimental
  128.         System.gc();
  129.     }
  130.  
  131.     void getRobotPosition()
  132.     {
  133.         robot.getPosition();
  134.         System.out.println(robot.fPosVal[0]);
  135.         System.out.println(robot.fPosVal[1]);
  136.         System.out.println(robot.fPosVal[2]);
  137.     }
  138.  
  139.     void getRobotRotation()
  140.     {
  141.         robot.getRotation();
  142.         System.out.println(robot.fRotVal[0]);
  143.         System.out.println(robot.fRotVal[1]);
  144.         System.out.println(robot.fRotVal[2]);
  145.         System.out.println(robot.fRotVal[3]);
  146.         System.out.println(robot.fRotVal[4]);
  147.         System.out.println(robot.fRotVal[5]);
  148.     }
  149.  
  150.  
  151.     void randomColor(VRCObject Obj)
  152.     {
  153.         int rgb[] = new int[3];
  154.         rgb[0]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
  155.         rgb[1]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
  156.         rgb[2]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
  157.         Obj.setColor(rgb);
  158.     }
  159.  
  160.     void colorRainbo()
  161.     {
  162.         for (int x=0;x<2;x++){
  163.             randomColor(top);
  164.             randomColor(top_lower);
  165.             randomColor(window);
  166.             randomColor(r_arm);
  167.             randomColor(l_arm);
  168.             randomColor(mid);
  169.             randomColor(r_arm_peg);
  170.             randomColor(l_arm_peg);
  171.             randomColor(r_arm_low);
  172.             randomColor(r_arm_up);
  173.             randomColor(l_arm_low);
  174.             randomColor(l_arm_up);
  175.             randomColor(r_forearm);
  176.             randomColor(l_forearm);
  177.             randomColor(r_hand);
  178.             randomColor(l_hand);
  179.             randomColor(l_leg_peg);
  180.             randomColor(r_leg_peg);
  181.             randomColor(r_leg);
  182.             randomColor(l_leg);
  183.             randomColor(r_calf);
  184.             randomColor(l_calf);
  185.             randomColor(r_foot);
  186.             randomColor(l_foot);
  187.        }
  188.     }
  189.  
  190.     void tranRobot(float tranVal[])
  191.     {
  192.         robot.TranslateRel(tranVal);
  193.     }
  194.  
  195.     void tranUpperBody(float tranVal[])
  196.     {
  197.         upper_body.Translate(tranVal);
  198.     }
  199.  
  200.     void rotUpperBody(float rotVal[]){
  201.         rotVal[3] = 0; rotVal[4] = (float)0.1; rotVal[5] = 0;
  202.         upper_body.RotateRel(rotVal);
  203.     }
  204.  
  205.     void spinUpperBody(float rotVal[])
  206.     {
  207.         upper_body.Spin(rotVal);
  208.     }
  209.  
  210.     void tranTorso(float tranVal[])
  211.     {
  212.         torso.Translate(tranVal);
  213.     }
  214.  
  215.     void rotTorso(float rotVal[])
  216.     {
  217.         //midpoints go here
  218.         rotVal[3] = 0; rotVal[4] = 0; rotVal[5] = 0;
  219.         torso.RotateRel(rotVal);
  220.     }
  221.  
  222.     void spinTorso(float rotVal[])
  223.     {
  224.         torso.Spin(rotVal);
  225.     }
  226.  
  227.     void tranRightArm(float tranVal[])
  228.     {
  229.         r_arm.Translate(tranVal);
  230.     }
  231.  
  232.     void rotRightArm(float rotVal[])
  233.     {
  234.         //set Object rotation midpoints
  235.         rotVal[3]=(float)-2.25; rotVal[4]=(float)1.75; rotVal[5]=0;
  236.         r_arm.RotateRel(rotVal);
  237.     }
  238.  
  239.     void spinRightArm(float rotVal[])
  240.     {
  241.         r_arm.Spin(rotVal);
  242.     }
  243.  
  244.     void tranRightForearm(float tranVal[])
  245.     {
  246.         r_forearm.Translate(tranVal);
  247.     }
  248.  
  249.     void rotRightForearm(float rotVal[])
  250.     {
  251.         rotVal[3]=(float)-2.5; rotVal[4]=(float)0.25; rotVal[5]=0;
  252.         r_forearm.RotateRel(rotVal);
  253.     }
  254.  
  255.  
  256.     void rotRightHand(float rotVal[]){
  257.         rotVal[3] = (float)-2.5; rotVal[4] = 0; rotVal[5] = (float)-0.75;
  258.     }
  259.  
  260.  
  261.     void rotLeftArm(float rotVal[]){
  262.         rotVal[3]=(float)2.25; rotVal[4]=(float)1.75; rotVal[5]=0;
  263.         l_arm.RotateRel(rotVal);
  264.     }
  265.  
  266.  
  267.     void rotLeftForearm(float rotVal[]){
  268.         rotVal[3] = (float)2.5; rotVal[4] = 0; rotVal[5] = (float)0.25;
  269.         l_forearm.RotateRel(rotVal);
  270.     }
  271.  
  272.  
  273.     void rotRightLeg(float rotVal[]){
  274.         rotVal[3] = (float)-1; rotVal[4] = 0; rotVal[5] = (float)0;
  275.         r_leg.RotateRel(rotVal);
  276.     }
  277.  
  278.     void rotRightCalf(float rotVal[]){
  279.         rotVal[3] = -1;rotVal[4] = (float)-1.25; rotVal[5] = 0;
  280.         r_calf.RotateRel(rotVal);
  281.     }
  282.     void rotRightFoot(float rotVal[]){
  283.         rotVal[3] = -1;rotVal[4] = (float)-2.25; rotVal[5] = 0;
  284.         r_foot.RotateRel(rotVal);
  285.     }
  286.     void rotLeftLeg(float rotVal[]){
  287.         rotVal[3] = (float)1; rotVal[4] = 0; rotVal[5] = (float)0;
  288.         l_leg.RotateRel(rotVal);
  289.     }
  290.  
  291.     void rotLeftCalf(float rotVal[]){
  292.         rotVal[3] = (float)-1; rotVal[4] = (float)-1.25; rotVal[5] = 0;
  293.         l_calf.RotateRel(rotVal);
  294.     }
  295.     void rotLeftFoot(float rotVal[]){
  296.         rotVal[3] = (float)-1; rotVal[4] = (float)-2.25; rotVal[5] = 0;
  297.         l_foot.RotateRel(rotVal);
  298.     }
  299.  
  300.     void rotateArm(String arm,int iDir){
  301.         float fRotVal[] = new float[6];
  302.         if(iDir == 1){
  303.             //up
  304.             fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)0.314;
  305.         }else{
  306.             //down
  307.             fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)-0.314;
  308.         }
  309.         if(arm.equals("right")){
  310.             rotRightArm(fRotVal);
  311.             rotRightForearm(fRotVal);
  312.         }else{
  313.             rotLeftArm(fRotVal);
  314.             rotLeftForearm(fRotVal);
  315.         }
  316.     }
  317.  
  318.     void rotateFoot(String foot,int iDir){
  319.         float fRotVal[] = new float[6];
  320.         if(iDir == 1){
  321.             //up
  322.             fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)0.03725;
  323.         }else{
  324.             //down
  325.             fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)-0.03725;
  326.         }
  327.         if(foot.equals("right")){
  328.             rotRightFoot(fRotVal);
  329.         }else{
  330.             rotLeftFoot(fRotVal);
  331.         }
  332.     }
  333.  
  334.  
  335.     public void rotateLeg(String leg,int iDir){
  336.         float fRotVal[] = new float[6];
  337.         float fRotVil[] = new float[6];
  338.         if(iDir == 1){
  339.             //up
  340.             fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)0.314;
  341.             fRotVil[0] = 0;fRotVil[1] = 0;fRotVil[2] = (float)-0.314;
  342.         }else{
  343.             //down
  344.             fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)-0.314;
  345.             fRotVil[0] = 0;fRotVil[1] = 0;fRotVil[2] = (float)0.314;
  346.         }
  347.         if(leg.equals("right")){
  348.             rotRightLeg(fRotVal);
  349.             rotRightCalf(fRotVil);
  350.         }else{
  351.             rotLeftLeg(fRotVal);
  352.             rotLeftCalf(fRotVil);
  353.         }
  354.     }
  355.  
  356. public void rotatesLeg(String leg,int iDir){
  357.         float fRotVal[] = new float[6];
  358.         if(iDir == 1){
  359.             //up
  360.             fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)0.03725;
  361.         }else{
  362.             //down
  363.             fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)-0.03725;
  364.         }
  365.         if(leg.equals("right")){
  366.             rotRightLeg(fRotVal);
  367.  
  368.         }else{
  369.             rotLeftLeg(fRotVal);
  370.  
  371.         }
  372.     }
  373.  
  374.     public void rotateTorso(int iDir){
  375.         float fRotVal[] = new float[6];
  376.         if(iDir == 1){
  377.             //left
  378.             fRotVal[0] =(float)0.314 ; fRotVal[1] =0;fRotVal[2] =0;
  379.         }else{
  380.             //right
  381.             fRotVal[0] =-(float)0.314; fRotVal[1] =0;fRotVal[2] =0;
  382.         }
  383.         rotTorso(fRotVal);
  384.     }
  385.  
  386.     public void pause(int delay){
  387.         try{
  388.             VreamApi.VRCTimer.sleep(1000/delay);
  389.         }catch (InterruptedException ignored){
  390.             return;
  391.         }
  392.         return;
  393.     }
  394.  
  395.     public void wave(String sArm){
  396.         for(int x=0;x <5;x++){
  397.             rotateArm(sArm,-1);
  398.             pause(22);
  399.         }
  400.         for(int x=0;x <5;x++){
  401.             rotateArm(sArm,1);
  402.             pause(22);
  403.         }
  404.     }
  405.  
  406.     public void twist(int iDir){
  407.         for(int x=0;x <5;x++){
  408.             rotateTorso(iDir);
  409.             pause(22);
  410.         }
  411.         for(int x=0;x <5;x++){
  412.             rotateTorso(iDir*-1);
  413.             pause(22);
  414.         }
  415.     }
  416.  
  417.     public void liftLeg(String sLeg){
  418.          for(int x=0;x <5;x++){
  419.              rotateLeg(sLeg,-1);
  420.              pause(22);
  421.          }
  422.          for(int x=0;x <5;x++){
  423.              rotateLeg(sLeg,1);
  424.              pause(22);
  425.          }
  426.     }
  427.  
  428.     public void bendFoot(String sFoot){
  429.          for(int x=0;x <5;x++){
  430.              rotateFoot(sFoot,-1);
  431.              pause(22);
  432.          }
  433.          for(int x=0;x <5;x++){
  434.              rotateFoot(sFoot,1);
  435.              pause(22);
  436.          }
  437.     }
  438.  
  439.  
  440.     public void walk(){
  441.         float tranVal[] = new float[3];
  442.         tranVal[0]=0;tranVal[1]=0;tranVal[2]=(float)0.075;
  443.         for(int x=0;x <5;x++){
  444.              rotateFoot("left",-1);
  445.              rotatesLeg("left",1);
  446.              rotateLeg("right",-1);
  447.              tranRobot(tranVal);
  448.              pause(22);
  449.          }
  450.         for(int x=0;x <5;x++){
  451.              rotateFoot("left",-1);
  452.              rotatesLeg("left",1);
  453.              rotatesLeg("right",-1);
  454.              rotateLeg("right",1);
  455.              rotateFoot("right",1);
  456.              tranRobot(tranVal);
  457.              pause(22);
  458.          }
  459.          for(int x=0;x <5;x++){
  460.              rotateFoot("left",1);
  461.              rotatesLeg("left",-1);
  462.              rotateFoot("left",1);
  463.              rotatesLeg("left",-1);
  464.              rotatesLeg("right",1);
  465.              rotateFoot("right",-1);
  466.              tranRobot(tranVal);
  467.              pause(22);
  468.          }
  469.  
  470.  
  471.          for(int x=0;x <5;x++){
  472.              rotateFoot("right",-1);
  473.              rotatesLeg("right",1);
  474.              rotateLeg("left",-1);
  475.              tranRobot(tranVal);
  476.              pause(22);
  477.          }
  478.         for(int x=0;x <5;x++){
  479.              rotateFoot("right",-1);
  480.              rotatesLeg("right",1);
  481.              rotatesLeg("left",-1);
  482.              rotateLeg("left",1);
  483.              rotateFoot("left",1);
  484.              tranRobot(tranVal);
  485.              pause(22);
  486.          }
  487.          for(int x=0;x <5;x++){
  488.              rotateFoot("right",1);
  489.              rotatesLeg("right",-1);
  490.              rotateFoot("right",1);
  491.              rotatesLeg("right",-1);
  492.              rotatesLeg("left",1);
  493.              rotateFoot("left",-1);
  494.              tranRobot(tranVal);
  495.              pause(22);
  496.          }
  497.     }
  498.  
  499.     public void animate(){
  500.          for(int x=0;x <5;x++){
  501.              rotateArm("right",-1);
  502.              pause(22);
  503.          }
  504.          for(int x=0;x <5;x++){
  505.              rotateArm("left",-1);
  506.              rotateTorso(-1);
  507.              rotateLeg("right",-1);
  508.              pause(22);
  509.          }
  510.          for(int x=0;x <5;x++){
  511.              rotateArm("right",1);
  512.              rotateLeg("right",1);
  513.              rotateTorso(1);
  514.              pause(22);
  515.          }
  516.          for(int x=0;x <5;x++){
  517.              rotateArm("left",1);
  518.              pause(22);
  519.          }
  520.     }
  521.  
  522.     public void asyncWalk(){
  523.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  524.         VRCRobo.repeat(2);
  525.         VRCRobo.sendCommand("walk");
  526.     }
  527.  
  528.     public void asyncColor(){
  529.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  530.         VRCRobo.repeat(1);
  531.         VRCRobo.sendCommand("color");
  532.     }
  533.  
  534.     public void asyncRWave(){
  535.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  536.         VRCRobo.repeat(1);
  537.         VRCRobo.sendCommand("rwave");
  538.     }
  539.  
  540.     public void asyncLWave(){
  541.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  542.         VRCRobo.repeat(1);
  543.         VRCRobo.sendCommand("lwave");
  544.     }
  545.  
  546.     public void asyncLLeg(){
  547.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  548.         VRCRobo.repeat(1);
  549.         VRCRobo.sendCommand("lleg");
  550.     }
  551.  
  552.     public void asyncRLeg(){
  553.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  554.         VRCRobo.repeat(1);
  555.         VRCRobo.sendCommand("rleg");
  556.     }
  557.  
  558.     public void asyncAnimate(){
  559.         VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
  560.         VRCRobo.repeat(1);
  561.         VRCRobo.sendCommand("animate");
  562.     }
  563. }
  564.  
  565. //this class demonstartes how to make your object behave in an asynchronous manner
  566. class VRCAsyncRobot extends Thread
  567. {
  568.     VRCRobot VRCRobo;
  569.     private boolean kill = false;
  570.     private String sCommand = new String();
  571.     private int repeat = 1;
  572.  
  573.     VRCAsyncRobot(VRCRobot superRobo){
  574.         VRCRobo = superRobo;
  575.         start();
  576.     }
  577.  
  578.     public void run(){
  579.         while(!kill){
  580.             if(sCommand.equals("walk")){
  581.                 System.out.println("Walking");
  582.                 for(int x=0;x<repeat;x++)VRCRobo.walk();
  583.                 sCommand = "";
  584.                 suicide();
  585.             }else if(sCommand.equals("animate")){
  586.                 for(int x=0;x<repeat;x++)VRCRobo.animate();
  587.                 sCommand = "";
  588.                 suicide();
  589.             }else if(sCommand.equals("rwave")){
  590.                 for(int x=0;x<repeat;x++)VRCRobo.wave("right");
  591.                 sCommand = "";
  592.                 suicide();
  593.             }else if(sCommand.equals("lwave")){
  594.                 for(int x=0;x<repeat;x++)VRCRobo.wave("left");
  595.                 sCommand = "";
  596.                 suicide();
  597.             }else if(sCommand.equals("lleg")){
  598.                 for(int x=0;x<repeat;x++)VRCRobo.liftLeg("left");
  599.                 sCommand = "";
  600.                 suicide();
  601.             }else if(sCommand.equals("rleg")){
  602.                 for(int x=0;x<repeat;x++)VRCRobo.liftLeg("right");
  603.                 sCommand = "";
  604.                 suicide();
  605.             }else if(sCommand.equals("color")){
  606.                 for(int x=0;x<repeat;x++)VRCRobo.colorRainbo();
  607.                 sCommand = "";
  608.                 suicide();
  609.             }
  610.         }
  611.     }
  612.  
  613.     public void destroy(){
  614.         repeat = 0;
  615.         kill = true;
  616.         stop();
  617.     }
  618.  
  619.     public void sendCommand(String sCmd){
  620.         sCommand = sCmd;
  621.     }
  622.  
  623.     public void suicide(){
  624.         kill = true;
  625.         stop();
  626.     }
  627.  
  628.     public void repeat(int inum){
  629.         repeat = inum;
  630.     }
  631. }
  632.