home *** CD-ROM | disk | FTP | other *** search
Java Source | 1995-03-11 | 17.7 KB | 632 lines |
- import java.*;
- import VRCAPI;
-
- public class VRCRobot implements Runnable
- {
- VRCObject robot;
- VRCObject upper_body;
-
- VRCObject top_lower;
- VRCObject top;
-
- VRCObject r_arm_peg;
- VRCObject r_arm_low;
- VRCObject r_arm_up;
-
- VRCObject l_arm_peg;
- VRCObject l_arm_low;
- VRCObject l_arm_up;
-
- VRCObject torso;
- VRCObject r_arm;
- VRCObject r_forearm;
- VRCObject r_hand;
- VRCObject l_arm;
- VRCObject l_forearm;
- VRCObject l_hand;
- VRCObject r_leg;
- VRCObject r_calf;
- VRCObject r_foot;
- VRCObject l_leg;
- VRCObject l_calf;
- VRCObject l_foot;
- VRCObject mid;
- VRCObject window;
- VRCObject l_leg_peg;
- VRCObject r_leg_peg;
-
-
- VRCAPI VreamApi;
- int ActiveWindow;
- boolean KeepRunning;
-
- public VRCRobot()
- {
- //start the rendering timer
- VreamApi = new VRCAPI();
- //get the current window (plug-in window)
- //you will need to send this to each object constructor
- ActiveWindow = VreamApi.ActiveHWND;
- //create a VRCObject for every object named in the world
- //call the constructor with the object name and the active window
- robot = new VRCObject("robot",ActiveWindow);
- upper_body = new VRCObject("upper_body",ActiveWindow);
- torso = new VRCObject("torso",ActiveWindow);
- r_arm = new VRCObject("r_arm",ActiveWindow);
- r_forearm = new VRCObject("r_forearm",ActiveWindow);
- r_hand = new VRCObject("r_hand",ActiveWindow);
- l_arm = new VRCObject("l_arm",ActiveWindow);
- l_forearm = new VRCObject("l_forearm",ActiveWindow);
- l_hand = new VRCObject("l_hand",ActiveWindow);
- r_leg = new VRCObject("r_leg",ActiveWindow);
- r_calf = new VRCObject("r_calf",ActiveWindow);
- r_foot = new VRCObject("r_foot",ActiveWindow);
- l_leg = new VRCObject("l_leg",ActiveWindow);
- l_calf = new VRCObject("l_calf",ActiveWindow);
- l_foot = new VRCObject("l_foot",ActiveWindow);
- mid = new VRCObject("mid",ActiveWindow);
- top_lower = new VRCObject("top_lower",ActiveWindow);
- top = new VRCObject("top",ActiveWindow);
- r_arm_peg = new VRCObject("r_arm_peg",ActiveWindow);
- r_arm_low = new VRCObject("r_arm_low",ActiveWindow);
- r_arm_up = new VRCObject("r_arm_up",ActiveWindow);
- l_arm_peg = new VRCObject("l_arm_peg",ActiveWindow);
- l_arm_low = new VRCObject("l_arm_low",ActiveWindow);
- l_arm_up = new VRCObject("l_arm_up",ActiveWindow);
- window = new VRCObject("window",ActiveWindow);
- l_leg_peg = new VRCObject("l_leg_peg",ActiveWindow);
- r_leg_peg = new VRCObject("r_leg_peg",ActiveWindow);
- }
-
- public void run(String sCommand)
- {
- if (sCommand.equals("walk")) walk();
- if (sCommand.equals("animate")) animate();
- }
-
- public void run()
- {
- //System.out.println("Robot run");
- KeepRunning = true;
- while(KeepRunning){
- }
- }
-
- public void destroy()
- {
- robot.deactivate();
- upper_body.deactivate();
- torso.deactivate();
- r_arm.deactivate();
- r_forearm.deactivate();
- r_hand.deactivate();
- l_arm.deactivate();
- l_forearm.deactivate();
- l_hand.deactivate();
- r_leg.deactivate();
- r_calf.deactivate();
- r_foot.deactivate();
- l_leg.deactivate();
- l_calf.deactivate();
- l_foot.deactivate();
- mid.deactivate();
- top_lower.deactivate();
- top.deactivate();
- r_arm_peg.deactivate();
- r_arm_low.deactivate();
- r_arm_up.deactivate();
- l_arm_peg.deactivate();
- l_arm_low.deactivate();
- l_arm_up.deactivate();
- window.deactivate();
- l_leg_peg.deactivate();
- r_leg_peg.deactivate();
- //kill the timer
- VreamApi.destroy();
- KeepRunning = false;
- //experimental
- System.gc();
- }
-
- void getRobotPosition()
- {
- robot.getPosition();
- System.out.println(robot.fPosVal[0]);
- System.out.println(robot.fPosVal[1]);
- System.out.println(robot.fPosVal[2]);
- }
-
- void getRobotRotation()
- {
- robot.getRotation();
- System.out.println(robot.fRotVal[0]);
- System.out.println(robot.fRotVal[1]);
- System.out.println(robot.fRotVal[2]);
- System.out.println(robot.fRotVal[3]);
- System.out.println(robot.fRotVal[4]);
- System.out.println(robot.fRotVal[5]);
- }
-
-
- void randomColor(VRCObject Obj)
- {
- int rgb[] = new int[3];
- rgb[0]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
- rgb[1]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
- rgb[2]=((int)(1/java.lang.Math.random())*(int)(1/java.lang.Math.random())+((int)(1/java.lang.Math.random())*10));
- Obj.setColor(rgb);
- }
-
- void colorRainbo()
- {
- for (int x=0;x<2;x++){
- randomColor(top);
- randomColor(top_lower);
- randomColor(window);
- randomColor(r_arm);
- randomColor(l_arm);
- randomColor(mid);
- randomColor(r_arm_peg);
- randomColor(l_arm_peg);
- randomColor(r_arm_low);
- randomColor(r_arm_up);
- randomColor(l_arm_low);
- randomColor(l_arm_up);
- randomColor(r_forearm);
- randomColor(l_forearm);
- randomColor(r_hand);
- randomColor(l_hand);
- randomColor(l_leg_peg);
- randomColor(r_leg_peg);
- randomColor(r_leg);
- randomColor(l_leg);
- randomColor(r_calf);
- randomColor(l_calf);
- randomColor(r_foot);
- randomColor(l_foot);
- }
- }
-
- void tranRobot(float tranVal[])
- {
- robot.TranslateRel(tranVal);
- }
-
- void tranUpperBody(float tranVal[])
- {
- upper_body.Translate(tranVal);
- }
-
- void rotUpperBody(float rotVal[]){
- rotVal[3] = 0; rotVal[4] = (float)0.1; rotVal[5] = 0;
- upper_body.RotateRel(rotVal);
- }
-
- void spinUpperBody(float rotVal[])
- {
- upper_body.Spin(rotVal);
- }
-
- void tranTorso(float tranVal[])
- {
- torso.Translate(tranVal);
- }
-
- void rotTorso(float rotVal[])
- {
- //midpoints go here
- rotVal[3] = 0; rotVal[4] = 0; rotVal[5] = 0;
- torso.RotateRel(rotVal);
- }
-
- void spinTorso(float rotVal[])
- {
- torso.Spin(rotVal);
- }
-
- void tranRightArm(float tranVal[])
- {
- r_arm.Translate(tranVal);
- }
-
- void rotRightArm(float rotVal[])
- {
- //set Object rotation midpoints
- rotVal[3]=(float)-2.25; rotVal[4]=(float)1.75; rotVal[5]=0;
- r_arm.RotateRel(rotVal);
- }
-
- void spinRightArm(float rotVal[])
- {
- r_arm.Spin(rotVal);
- }
-
- void tranRightForearm(float tranVal[])
- {
- r_forearm.Translate(tranVal);
- }
-
- void rotRightForearm(float rotVal[])
- {
- rotVal[3]=(float)-2.5; rotVal[4]=(float)0.25; rotVal[5]=0;
- r_forearm.RotateRel(rotVal);
- }
-
-
- void rotRightHand(float rotVal[]){
- rotVal[3] = (float)-2.5; rotVal[4] = 0; rotVal[5] = (float)-0.75;
- }
-
-
- void rotLeftArm(float rotVal[]){
- rotVal[3]=(float)2.25; rotVal[4]=(float)1.75; rotVal[5]=0;
- l_arm.RotateRel(rotVal);
- }
-
-
- void rotLeftForearm(float rotVal[]){
- rotVal[3] = (float)2.5; rotVal[4] = 0; rotVal[5] = (float)0.25;
- l_forearm.RotateRel(rotVal);
- }
-
-
- void rotRightLeg(float rotVal[]){
- rotVal[3] = (float)-1; rotVal[4] = 0; rotVal[5] = (float)0;
- r_leg.RotateRel(rotVal);
- }
-
- void rotRightCalf(float rotVal[]){
- rotVal[3] = -1;rotVal[4] = (float)-1.25; rotVal[5] = 0;
- r_calf.RotateRel(rotVal);
- }
- void rotRightFoot(float rotVal[]){
- rotVal[3] = -1;rotVal[4] = (float)-2.25; rotVal[5] = 0;
- r_foot.RotateRel(rotVal);
- }
- void rotLeftLeg(float rotVal[]){
- rotVal[3] = (float)1; rotVal[4] = 0; rotVal[5] = (float)0;
- l_leg.RotateRel(rotVal);
- }
-
- void rotLeftCalf(float rotVal[]){
- rotVal[3] = (float)-1; rotVal[4] = (float)-1.25; rotVal[5] = 0;
- l_calf.RotateRel(rotVal);
- }
- void rotLeftFoot(float rotVal[]){
- rotVal[3] = (float)-1; rotVal[4] = (float)-2.25; rotVal[5] = 0;
- l_foot.RotateRel(rotVal);
- }
-
- void rotateArm(String arm,int iDir){
- float fRotVal[] = new float[6];
- if(iDir == 1){
- //up
- fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)0.314;
- }else{
- //down
- fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)-0.314;
- }
- if(arm.equals("right")){
- rotRightArm(fRotVal);
- rotRightForearm(fRotVal);
- }else{
- rotLeftArm(fRotVal);
- rotLeftForearm(fRotVal);
- }
- }
-
- void rotateFoot(String foot,int iDir){
- float fRotVal[] = new float[6];
- if(iDir == 1){
- //up
- fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)0.03725;
- }else{
- //down
- fRotVal[0] = 0;fRotVal[1] = (float)0;fRotVal[2] = (float)-0.03725;
- }
- if(foot.equals("right")){
- rotRightFoot(fRotVal);
- }else{
- rotLeftFoot(fRotVal);
- }
- }
-
-
- public void rotateLeg(String leg,int iDir){
- float fRotVal[] = new float[6];
- float fRotVil[] = new float[6];
- if(iDir == 1){
- //up
- fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)0.314;
- fRotVil[0] = 0;fRotVil[1] = 0;fRotVil[2] = (float)-0.314;
- }else{
- //down
- fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)-0.314;
- fRotVil[0] = 0;fRotVil[1] = 0;fRotVil[2] = (float)0.314;
- }
- if(leg.equals("right")){
- rotRightLeg(fRotVal);
- rotRightCalf(fRotVil);
- }else{
- rotLeftLeg(fRotVal);
- rotLeftCalf(fRotVil);
- }
- }
-
- public void rotatesLeg(String leg,int iDir){
- float fRotVal[] = new float[6];
- if(iDir == 1){
- //up
- fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)0.03725;
- }else{
- //down
- fRotVal[0] = 0;fRotVal[1] = 0;fRotVal[2] = (float)-0.03725;
- }
- if(leg.equals("right")){
- rotRightLeg(fRotVal);
-
- }else{
- rotLeftLeg(fRotVal);
-
- }
- }
-
- public void rotateTorso(int iDir){
- float fRotVal[] = new float[6];
- if(iDir == 1){
- //left
- fRotVal[0] =(float)0.314 ; fRotVal[1] =0;fRotVal[2] =0;
- }else{
- //right
- fRotVal[0] =-(float)0.314; fRotVal[1] =0;fRotVal[2] =0;
- }
- rotTorso(fRotVal);
- }
-
- public void pause(int delay){
- try{
- VreamApi.VRCTimer.sleep(1000/delay);
- }catch (InterruptedException ignored){
- return;
- }
- return;
- }
-
- public void wave(String sArm){
- for(int x=0;x <5;x++){
- rotateArm(sArm,-1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateArm(sArm,1);
- pause(22);
- }
- }
-
- public void twist(int iDir){
- for(int x=0;x <5;x++){
- rotateTorso(iDir);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateTorso(iDir*-1);
- pause(22);
- }
- }
-
- public void liftLeg(String sLeg){
- for(int x=0;x <5;x++){
- rotateLeg(sLeg,-1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateLeg(sLeg,1);
- pause(22);
- }
- }
-
- public void bendFoot(String sFoot){
- for(int x=0;x <5;x++){
- rotateFoot(sFoot,-1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateFoot(sFoot,1);
- pause(22);
- }
- }
-
-
- public void walk(){
- float tranVal[] = new float[3];
- tranVal[0]=0;tranVal[1]=0;tranVal[2]=(float)0.075;
- for(int x=0;x <5;x++){
- rotateFoot("left",-1);
- rotatesLeg("left",1);
- rotateLeg("right",-1);
- tranRobot(tranVal);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateFoot("left",-1);
- rotatesLeg("left",1);
- rotatesLeg("right",-1);
- rotateLeg("right",1);
- rotateFoot("right",1);
- tranRobot(tranVal);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateFoot("left",1);
- rotatesLeg("left",-1);
- rotateFoot("left",1);
- rotatesLeg("left",-1);
- rotatesLeg("right",1);
- rotateFoot("right",-1);
- tranRobot(tranVal);
- pause(22);
- }
-
-
- for(int x=0;x <5;x++){
- rotateFoot("right",-1);
- rotatesLeg("right",1);
- rotateLeg("left",-1);
- tranRobot(tranVal);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateFoot("right",-1);
- rotatesLeg("right",1);
- rotatesLeg("left",-1);
- rotateLeg("left",1);
- rotateFoot("left",1);
- tranRobot(tranVal);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateFoot("right",1);
- rotatesLeg("right",-1);
- rotateFoot("right",1);
- rotatesLeg("right",-1);
- rotatesLeg("left",1);
- rotateFoot("left",-1);
- tranRobot(tranVal);
- pause(22);
- }
- }
-
- public void animate(){
- for(int x=0;x <5;x++){
- rotateArm("right",-1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateArm("left",-1);
- rotateTorso(-1);
- rotateLeg("right",-1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateArm("right",1);
- rotateLeg("right",1);
- rotateTorso(1);
- pause(22);
- }
- for(int x=0;x <5;x++){
- rotateArm("left",1);
- pause(22);
- }
- }
-
- public void asyncWalk(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(2);
- VRCRobo.sendCommand("walk");
- }
-
- public void asyncColor(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("color");
- }
-
- public void asyncRWave(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("rwave");
- }
-
- public void asyncLWave(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("lwave");
- }
-
- public void asyncLLeg(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("lleg");
- }
-
- public void asyncRLeg(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("rleg");
- }
-
- public void asyncAnimate(){
- VRCAsyncRobot VRCRobo = new VRCAsyncRobot(this);
- VRCRobo.repeat(1);
- VRCRobo.sendCommand("animate");
- }
- }
-
- //this class demonstartes how to make your object behave in an asynchronous manner
- class VRCAsyncRobot extends Thread
- {
- VRCRobot VRCRobo;
- private boolean kill = false;
- private String sCommand = new String();
- private int repeat = 1;
-
- VRCAsyncRobot(VRCRobot superRobo){
- VRCRobo = superRobo;
- start();
- }
-
- public void run(){
- while(!kill){
- if(sCommand.equals("walk")){
- System.out.println("Walking");
- for(int x=0;x<repeat;x++)VRCRobo.walk();
- sCommand = "";
- suicide();
- }else if(sCommand.equals("animate")){
- for(int x=0;x<repeat;x++)VRCRobo.animate();
- sCommand = "";
- suicide();
- }else if(sCommand.equals("rwave")){
- for(int x=0;x<repeat;x++)VRCRobo.wave("right");
- sCommand = "";
- suicide();
- }else if(sCommand.equals("lwave")){
- for(int x=0;x<repeat;x++)VRCRobo.wave("left");
- sCommand = "";
- suicide();
- }else if(sCommand.equals("lleg")){
- for(int x=0;x<repeat;x++)VRCRobo.liftLeg("left");
- sCommand = "";
- suicide();
- }else if(sCommand.equals("rleg")){
- for(int x=0;x<repeat;x++)VRCRobo.liftLeg("right");
- sCommand = "";
- suicide();
- }else if(sCommand.equals("color")){
- for(int x=0;x<repeat;x++)VRCRobo.colorRainbo();
- sCommand = "";
- suicide();
- }
- }
- }
-
- public void destroy(){
- repeat = 0;
- kill = true;
- stop();
- }
-
- public void sendCommand(String sCmd){
- sCommand = sCmd;
- }
-
- public void suicide(){
- kill = true;
- stop();
- }
-
- public void repeat(int inum){
- repeat = inum;
- }
- }
-