Quick Guide

来源:互联网 发布:java语言编程软件win10 编辑:程序博客网 时间:2024/06/05 02:31

1.创建机器控制器

主要包括:主程序, 环境的描述类, 机器人类

(1)主程序MyProg.java

import simbad.gui.Simbad;public class MyProg {    public static void main(String[] args) {        Simbad frame = new Simbad(new MyEnv() ,false);    }}

Simbad frmae = new Simbad(new MyEnv(),false)    主程序的只要作用是启动Simbad并加载环境描述 ,MyEnv是下面要创建的环境描述的类


(2)描述环境 MyEnv.java

import simbad.sim.*;import javax.vecmath.Vector3d;public class MyEnv extends EnvironmentDescription {    public MyEnv(){        add(new Arch(new Vector3d(3,0,-3),this));        add(new MyRobot(new Vector3d(0, 0, 0),"my robot"));    }}
描述机器人运行的环境:
add(Arch(new Vectord3d(3,0,-3),this));  向环境中添加一个3D拱形.
add(new MyRobot(new Vector3d(0,0,0),"my robot"))  向环境中添加一个机器人.当中的MyRobot是下面要创建的机器人类

(3)机器人类MyRobot.java
import  simbad.sim.*;import javax.vecmath.Vector3d;public class MyRobot extends Agent {    public MyRobot (Vector3d position, String name) {             super(position,name);    }    public void initBehavior() {}        public void performBehavior() {        if (collisionDetected()) {            // stop the robot            setTranslationalVelocity(0.0);            setRotationalVelocity(0);        } else {            // progress at 0.5 m/s            setTranslationalVelocity(0.5);            // frequently change orientation             if ((getCounter() % 100)==0)                setRotationalVelocity(Math.PI/2 * (0.5 - Math.random()));        }    }}

MyRobot类继承于Agent类,这里需要重写两个方法:initBehavior和performBehavior
InitBehavior: 在Agent生命开始的时候由模拟器调用,这里为空,什么也不做.
performBehavior:每次操作的时候又模拟器调用.

--------------------------------------------------------------------------------------------------------------
最后编译和运行:
(我把simbad.jar 与上面的类放在同一个文件夹内)
编译:    javac -classpath simbad.jar MyProg.java MyEnv.java MyRobot.java
运行:    java -classpath simbad.jar:. MyProg

运行截图: