⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socsmallsim.java

📁 TeamBots 是一个可移植的多代理机器人仿真器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        /**         * Draw the robot's state.         */        public void drawState(Graphics g, int w, int h,                double t, double b, double l, double r)                {                top =t; bottom =b; left =l; right =r;                if (DEBUG) System.out.println("draw "+                        w + " " +                        h + " " +                        t + " " +                        b + " " +                        l + " " +                        r + " ");                double meterspp = (r - l) / (double)w;                int radius = (int)(RADIUS / meterspp);                int xpix = (int)((position.x - l) / meterspp);                int ypix = (int)((double)h - ((position.y - b) / meterspp));                /*--- draw State ---*/                g.setColor(background);                g.drawString(display_string,xpix+radius+3,ypix-radius);                displayVectors.draw(g,w,h,t,b,l,r);		}	/**         * Set the length of the trail (in movement steps).         * @param l int, the length of the trail.         */        public void setTrailLength(int l)		{		trail = new CircularBuffer(l);		}        /**         * Clear the trail.         */        public void clearTrail()		{		trail.clear();		}	/**	 * Draw the robot's ID.	 */	public void drawID(Graphics g, int w, int h,		double t, double b, double l, double r)		{		top =t; bottom =b; left =l; right =r;		if (DEBUG) System.out.println("draw "+			w + " " +			h + " " +			t + " " +			b + " " +			l + " " +			r + " ");		double meterspp = (r - l) / (double)w;		int radius = (int)(RADIUS / meterspp);		int xpix = (int)((position.x - l) / meterspp);		int ypix = (int)((double)h - ((position.y - b) / meterspp));		/*--- draw ID ---*/		g.setColor(background);                g.drawString(String.valueOf(getID()%5),xpix-radius,ypix-radius);		}        /**         * Draw the object as an icon.         * Default is just to do a regular draw.         */        public void drawIcon(Graphics g, int w, int h,                double t, double b, double l, double r)                {                draw(g, w, h, t, b, l, r);                }	/**	 * Draw the robot.	 */	public void draw(Graphics g, int w, int h,		double t, double b, double l, double r)		{		top =t; bottom =b; left =l; right =r;		if (DEBUG) System.out.println("draw "+			w + " " +			h + " " +			t + " " +			b + " " +			l + " " +			r + " ");		double meterspp = (r - l) / (double)w;		if (DEBUG) System.out.println("meterspp "+meterspp);		int radius = (int)(RADIUS / meterspp);		int steerd = (int)Units.RadToDeg(steer.t);		int xpix = (int)((position.x - l) / meterspp);		int ypix = (int)((double)h - ((position.y - b) / meterspp));		if (DEBUG) System.out.println("robot at"+			" at "+xpix+","+ypix);		/*--- draw the main body ---*/		g.setColor(foreground);		g.fillArc(xpix - radius, ypix - radius,				radius + radius, radius + radius,				steerd, 90);		g.fillArc(xpix - radius, ypix - radius,				radius + radius, radius + radius,				steerd+180, 90);		g.setColor(background);		g.fillArc(xpix - radius, ypix - radius,				radius + radius, radius + radius,				steerd+90, 90);		g.fillArc(xpix - radius, ypix - radius,				radius + radius, radius + radius,				steerd+270, 90);		/*--- draw steer ---*/		g.setColor(Color.black);		int dirx = xpix + (int)((double)radius * Math.cos(steer.t));		int diry = ypix + -(int)((double)radius * Math.sin(steer.t));		g.drawLine(xpix, ypix, dirx, diry);		}	/**	 * Clean up.	 */	public void quit()		{		}	/**	 * Gets time elapsed since the robot was instantiated. 	 * Since this is simulation, it may not match real elapsed time.	 */	public long getTime()		{		return(time);		}        private long    last_Opponentst = 0;        private Vec2    last_Opponents[] = new Vec2[0];        /**         * Get an array of Vec2s that point egocentrically from the         * center of the robot to the Opponents currently sensed by the         * robot.         * @param timestamp only get new information if 	 * 	timestamp > than last call or timestamp == -1 .         * @return the sensed Opponents.         */        public Vec2[] getOpponents(long timestamp)                {                if((timestamp > last_Opponentst)||(timestamp == -1))                        {                        if (timestamp != -1) last_Opponentst = timestamp;                        last_Opponents = kin_sensor.getOpponents(all_objects);                        }                return(last_Opponents);                }	private	long	last_Teammatest = 0;	private	Vec2	last_Teammates[] = new Vec2[0];	/**	 * Get an array of Vec2s that point egocentrically from the	 * center of the robot to the teammates currently sensed by the 	 * robot.	 * @param timestamp only get new information if 	 *	timestamp > than last call or timestamp == -1 .	 * @return the sensed teammates.	 */	public Vec2[] getTeammates(long timestamp)		{		if((timestamp > last_Teammatest)||(timestamp == -1))			{			if (timestamp != -1) last_Teammatest = timestamp;			last_Teammates = kin_sensor.getTeammates(all_objects);			}		Vec2[] retval = new Vec2[last_Teammates.length];		for (int i = 0; i<last_Teammates.length; i++)			retval[i] = new Vec2(last_Teammates[i].x,				last_Teammates[i].y);		return(retval);		}	public Vec2 getBall(long timestamp)		{		if (theball!=null) return(			new Vec2(theball.getCenter(position)));		else return(new Vec2());		}	long last_JustScoredt = 0;	int last_JustScored  = 0;        /**         * Get an integer that indicates whether a scoring event         * just occured.         * @param timestamp only get new information         *        if timestamp > than last call or timestamp == -1.         * @return 1 if team just scored, -1 if scored against,         *        0 otherwise.         */	public int getJustScored(long timestamp)		{		if((timestamp > last_JustScoredt)||(timestamp == -1))			{			if (timestamp != -1) last_JustScoredt = timestamp;			last_JustScored = 0;			if (theball==null) 				return(0);			else				{				if (on_east_team)					{					if (theball.eastKickOff())						last_JustScored = -1;					if (theball.westKickOff())						last_JustScored = 1;					}				else 					{					if (theball.eastKickOff())						last_JustScored = 1;					if (theball.westKickOff())						last_JustScored = -1;					}				}			}		return(last_JustScored);		}	public Vec2 getOurGoal(long timestamp)		{		Vec2 retval = new Vec2(team_goal.x, team_goal.y);		retval.sub(position);		return(retval);		}	public Vec2 getOpponentsGoal(long timestamp)		{		Vec2 retval = new Vec2(opponent_goal.x, opponent_goal.y);		retval.sub(position);		return(retval);		}	/**	 * Return an int represting the player's ID on the team.	 * This value may not be valid if the simulation has not	 * been "set up" yet.  Do not use it during initialization.         * @param timestamp only get new information if         *      timestamp > than last call or timestamp == -1 .         * @return the number.	 */	public int getPlayerNumber(long timestamp)		{		return(kin_sensor.getPlayerNumber(all_objects));		}	public boolean canKick(long timestamp)		{		boolean retval = false;		Vec2 kickpos = new Vec2(steer.x, steer.y);		kickpos.setr(RADIUS);		kickpos.add(position);		if (theball!=null)			{			Vec2 tmp = theball.getCenter(kickpos);			if (tmp.r<=KICKER_SPOT_RADIUS)				retval = true;			}		return(retval);		}	public void kick(long timestamp)		{		Vec2 d = new Vec2(0,0);		Vec2 v = new Vec2(KICKER_SPEED,0);		v.sett(steer.t);		if (theball != null)			theball.push(d,v);		}	private	long	last_Obstaclest = 0;	private	Vec2	last_Obstacles[];	private	int	num_Obstacles;	/**	 * Get an array of Vec2s that point egocentrically from the	 * center of the robot to the obstacles currently sensed by the 	 * bumpers and sonars.	 * @param timestamp only get new information if 	 *	timestamp > than last call or timestamp == -1 .	 * @return the sensed obstacles.	 */	public Vec2[] getObstacles(long timestamp)		{		if((timestamp > last_Obstaclest)||(timestamp == -1))			{			if (timestamp != -1) last_Obstaclest = timestamp;			Vec2 tmp_objs[] = new Vec2[all_objects.length];			num_Obstacles = 0;			/*--- check all objects ---*/			for(int i = 0; i<all_objects.length; i++)				{				/*--- check if it's an obstacle and not self ---*/				if (all_objects[i].isObstacle() &&					(all_objects[i].getID() != unique_id))					{					Vec2 tmp = all_objects[i].getClosestPoint(							position);					if (tmp.r<obstacle_rangeM)					tmp_objs[num_Obstacles++] = tmp;					}				}			last_Obstacles = new Vec2[num_Obstacles];			for(int i = 0; i<num_Obstacles; i++)			last_Obstacles[i] = new Vec2(tmp_objs[i].x,				tmp_objs[i].y);			}		Vec2[] retval = new Vec2[num_Obstacles];		for(int i = 0; i<num_Obstacles; i++)			retval[i] = new Vec2(last_Obstacles[i].x,				last_Obstacles[i].y);		return(retval);		}	private	double	kin_rangeM = 4.0;	/**	 * Set the maximum range at which a sensor reading should be considered	 * kin.  Beyond this range, the readings are ignored.	 * Also used by opponent sensor.	 * The default range on startup is 1 meter.	 * @param range the range in meters.	 */	public void setKinMaxRange(double range)		{		kin_rangeM = range;		kin_sensor.setKinMaxRange(range);		}	private	double	obstacle_rangeM = 4.0;	/**	 * Set the maximum range at which a sensor reading should be considered	 * an obstacle.  Beyond this range, the readings are ignored.	 * The default range on startup is 1 meter.	 * @param range the range in meters.	 */	public void setObstacleMaxRange(double range)		{		obstacle_rangeM = range;		}	/**	 * Get the position of the robot in global coordinates.	 * @param timestamp only get new information 	 * 	if timestamp > than last call or timestamp == -1.	 * @return the position.	 * @see Simple#resetPosition	 */	public Vec2 getPosition(long timestamp)		{		return(new Vec2(position.x, position.y));		}			/**	 * Get the position of the robot in global coordinates.	 * @return the position.	 * @see Simple#resetPosition	 */	public Vec2 getPosition()		{		return(new Vec2(position.x, position.y));		}			/**	 * Reset the odometry of the robot in global coordinates.	 * This might be done when reliable sensor information provides	 * a very good estimate of the robot's location, or if you	 * are starting the robot in a known location other than (0,0).	 * Do this only if you are certain you're right!	 * @param position the new position.	 * @see Simple#getPosition	 */	public void resetPosition(Vec2 posit)		{		position.setx(posit.x);		position.setx(posit.y);		}	/**	*/	public double getSteerHeading(long timestamp)		{		return(steer.t);		}		/**	*/	public void resetSteerHeading(double heading)		{		/* ensure in legal range */		heading = Units.ClipRad(heading); 		/* set the angle */		steer.sett(heading);		}	private double desired_heading;	/**	*/	public void setSteerHeading(long timestamp, double heading)		{		/* ensure in legal range */		desired_heading = Units.ClipRad(heading);		}			private double desired_speed = 0;	/**	 * @see SocSmallSim#setSpeed	 */ 	public void setSpeed(long timestamp, double speed)		{		/* ensure legal range */		if (speed > 1.0) speed = 1.0;		else if (speed < 0) speed = 0;		desired_speed = speed;		}	private double base_speed = MAX_TRANSLATION;	/**	 * @see SocSmallSim#setBaseSpeed	 */	public void setBaseSpeed(double speed)		{		if (speed > MAX_TRANSLATION) speed = MAX_TRANSLATION;		else if (speed < 0) speed = 0;		base_speed = speed;		}	/*--- Transceiver methods ---*/ 	public void multicast(int[] ids, Message m)		throws CommunicationException		{		transceiver.multicast(ids, m, all_objects);		}        public void broadcast(Message m)		{		transceiver.broadcast(m, all_objects);		}	public void unicast(int id, Message m) 		throws CommunicationException		{		transceiver.unicast(id, m, all_objects);		}	public CircularBufferEnumeration getReceiveChannel()		{		return(transceiver.getReceiveChannel());		}	public void setCommunicationMaxRange(double m)		{		transceiver.setCommunicationMaxRange(m);		}	public void receive(Message m)		{		transceiver.receive(m);		}        	public boolean connected()                {                return(true);                }	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -