Difference between revisions of "Robocode/Game Physics"
m |
m (fix coordinates gif) |
||
(33 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
− | + | This page describes the game physics of [[Robocode]]. | |
− | + | == Coordinates and directions == | |
+ | [[File:RobocodeCoordinates.gif]] | ||
+ | <br /> | ||
− | + | ;Coordinates system | |
+ | :Robocode uses the [[wikipedia:Cartesian coordinate system|Cartesian coordinate system]], which means that that the (0, 0) coordinate is located at the bottom-left corner of the battlefield. | ||
− | + | ;Rotational direction system | |
− | + | : Robocode uses a clockwise direction convention where 0° & 360° is north, 90° is east, 180° is south, and 270° is west. | |
+ | : In radians it's 0 & 2𝜋 ~= 6.28 is north, 𝜋/2 ~= 1.57 is east, 𝜋 ~= 3.14 is south, and 3𝜋/2 ~= 4.71 is west. | ||
− | + | == Time and distance == | |
+ | ;Time measurement | ||
+ | :Robocode time is measured in "ticks". Each robot gets one turn per tick. 1 tick = 1 turn. | ||
− | + | ;Distance measurement | |
− | + | :Robocode's distance units are measured with double precision, so you can move a fraction of a unit. Generally, 1 Robocode distance unit = 1 pixel, except when Robocode automatically scales down battles to fit on the screen. | |
− | |||
− | |||
− | |||
− | |||
− | == | + | == Movement physics == |
+ | ;Acceleration (a) | ||
+ | :Robots accelerate at the rate of 1 pixel/turn every turn. Robots decelerate at the rate of 2 pixels/turn every turn. Robocode determines acceleration for you, based on the distance you are trying to move. | ||
− | === Robocode | + | ;Velocity (v) |
+ | :The velocity equation is: v = at. Velocity can never exceed 8 pixels/turn. Note that technically, velocity is a vector, but in Robocode we simply assume the direction of the vector to be the robot's heading. | ||
+ | |||
+ | ;Distance (d) | ||
+ | :The distance formula is: d = vt. That is, distance = velocity * time | ||
+ | |||
+ | == Rotation == | ||
+ | ;Robot base rotation | ||
+ | :The maximum rate of rotation is: (10 - 0.75 * abs(velocity)) deg/turn. The faster you're moving, the slower you turn. | ||
+ | |||
+ | ;Gun rotation | ||
+ | :The maximum rate of rotation is: 20 deg/turn. This is added to the current rate of rotation of the robot. | ||
+ | |||
+ | ;Radar rotation | ||
+ | :The maximum rate of rotation is: 45 deg/turn. This is added to the current rate of rotation of the gun. | ||
+ | |||
+ | == Bullets == | ||
+ | ;Bullet damage | ||
+ | :4 * firepower. If firepower > 1, it does an additional damage = 2 * (power - 1). | ||
+ | |||
+ | ;Bullet velocity | ||
+ | :20 - 3 * firepower. | ||
+ | |||
+ | ;Gun heat generated on firing | ||
+ | :1 + firepower / 5. You cannot fire if gunHeat > 0. All guns are hot at the start of each round. | ||
+ | |||
+ | ;Energy returned on hit | ||
+ | :3 * firepower. | ||
+ | |||
+ | == Collisions == | ||
+ | ;Collision with another robot | ||
+ | :Each robot takes 0.6 damage. If a robot is moving away from the collision, it will not be stopped. | ||
+ | |||
+ | ;Collision with a wall | ||
+ | :AdvancedRobots take damage = abs(velocity) * 0.5 - 1 (never < 0). | ||
+ | |||
+ | == Robocode processing loop == | ||
The order that Robocode runs is as follows: | The order that Robocode runs is as follows: | ||
− | # Battle view is (re)painted | + | # Battle view is (re)painted. |
− | # All robots execute their code until they take action (and then paused) | + | # All robots execute their code until they take action (and are then paused). |
− | # Time is updated (time | + | # Time is updated (time++). |
− | # All bullets move and | + | # All bullets move (including the bullet fired in the last tick) and are checked for collisions. |
− | # All robots move (heading, | + | # All robots move (gun, radar, heading, acceleration, velocity, distance, in that order. gun heat is also decreased in this step). |
− | # All robots perform scans (and collect team messages) | + | # All robots perform scans (and collect team messages). |
− | # All robots are resumed to take new action | + | # All robots are resumed to take new action. |
− | # Each robot is | + | # Each robot processes its event queue. |
+ | |||
+ | Most of this can be gleaned by following the method calls from [https://github.com/robo-code/robocode/blob/0eb93ba532bc6731b7ff8dd02da1308d92ccb2fd/robocode.battle/src/main/java/net/sf/robocode/battle/BaseBattle.java#L251 <code>BaseBattle.runRound()</code>] and [https://github.com/robo-code/robocode/blob/0eb93ba532bc6731b7ff8dd02da1308d92ccb2fd/robocode.battle/src/main/java/net/sf/robocode/battle/Battle.java#L411 <code>Battle.runTurn()</code>] in the <code>robocode.battle</code> module. | ||
+ | |||
+ | === Firing pitfall === | ||
+ | Because bullets are fired before the gun is moved, calling setFire() will cause the bullet to leave at the current gun heading. This may seem counter-intuitive if you are used to thinking in terms of pointing a gun, then shooting. It is also inconvenient because you can't call <code>setTurnGun(...)</code> and <code>setFire(...)</code> right after each other (not if you need perfect accuracy, anyway). Most of the time, the error will be so small you won't notice it, but if you're testing a pattern matcher against sample.Walls, you will occasionally spot the bug. | ||
+ | |||
+ | To get the bullet to leave after turning the gun, you will need to use code like this: | ||
+ | |||
+ | <syntaxhighlight> | ||
+ | long fireTime = 0; | ||
+ | void doGun() { | ||
+ | if (fireTime == getTime() && getGunTurnRemaining() == 0) { | ||
+ | setFire(2); | ||
+ | } | ||
+ | |||
+ | // ... aiming code ... | ||
+ | |||
+ | setTurnGunRight(...); | ||
+ | // Don't need to check whether gun turn will complete in single turn because | ||
+ | // we check that gun is finished turning before calling setFire(...). | ||
+ | // This is simpler since the precise angle your gun can move in one tick | ||
+ | // depends on where your robot is turning. | ||
+ | fireTime = getTime() + 1; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Event handling == | ||
+ | |||
+ | Event dispatch happens from within commands that take a turn. So the call stack when an event is delivered usually looks like this: | ||
+ | |||
+ | <blockquote>Robocode internals → Robot's run method → Robocode internals → Event handler</blockquote> | ||
+ | |||
+ | However, event handlers can themselves make calls that take turns. If one of these happens to generate an event, we might see a call stack like | ||
+ | |||
+ | <blockquote>Robocode internals → Robot's run method → Robocode internals → First event handler → Robocode internals → Second event handler</blockquote> | ||
+ | |||
+ | But this kind of nesting could lead to a stack overflow. Or—more commonly—cases where the first handler finishes up its actions long after the response-provoking situation has passed. Thus, Robocode takes special steps for events generated within event handlers; these measures are implemented in EventManager.processEvents(). In particular, the call stack will get as far as | ||
+ | |||
+ | <blockquote>Robocode internals → Robot's run method → Robocode internals (including processEvents) → First event handler → Robocode internals (including processEvents)</blockquote> | ||
+ | |||
+ | but then the inner processEvents will detect the impending nesting and throw an EventInterruptedException, which unwinds the stack to the catch block in the outer processEvents: | ||
+ | |||
+ | <blockquote>Robocode internals → Robot's run method → Robocode internals (including processEvents)</blockquote> | ||
+ | |||
+ | effectively canceling whatever the running event handler was up to. Next, the event-delivering loop in the outer processEvents resumes delivering events, letting the second event handler execute unnested: | ||
+ | |||
+ | <blockquote>Robocode internals → Robot's run method → Robocode internals → Second event handler</blockquote> | ||
+ | |||
+ | It is possible, though not usually useful, to catch and respond to EventInterruptedExceptions in the first handler instead. | ||
+ | |||
+ | == See also == | ||
− | + | {{RobocodeDocsList}} | |
[[Category:Robocode Documentation]] | [[Category:Robocode Documentation]] | ||
+ | [[Category:Tutorials]] |
Latest revision as of 05:06, 16 September 2021
This page describes the game physics of Robocode.
Contents
Coordinates and directions
- Coordinates system
- Robocode uses the Cartesian coordinate system, which means that that the (0, 0) coordinate is located at the bottom-left corner of the battlefield.
- Rotational direction system
- Robocode uses a clockwise direction convention where 0° & 360° is north, 90° is east, 180° is south, and 270° is west.
- In radians it's 0 & 2𝜋 ~= 6.28 is north, 𝜋/2 ~= 1.57 is east, 𝜋 ~= 3.14 is south, and 3𝜋/2 ~= 4.71 is west.
Time and distance
- Time measurement
- Robocode time is measured in "ticks". Each robot gets one turn per tick. 1 tick = 1 turn.
- Distance measurement
- Robocode's distance units are measured with double precision, so you can move a fraction of a unit. Generally, 1 Robocode distance unit = 1 pixel, except when Robocode automatically scales down battles to fit on the screen.
Movement physics
- Acceleration (a)
- Robots accelerate at the rate of 1 pixel/turn every turn. Robots decelerate at the rate of 2 pixels/turn every turn. Robocode determines acceleration for you, based on the distance you are trying to move.
- Velocity (v)
- The velocity equation is: v = at. Velocity can never exceed 8 pixels/turn. Note that technically, velocity is a vector, but in Robocode we simply assume the direction of the vector to be the robot's heading.
- Distance (d)
- The distance formula is: d = vt. That is, distance = velocity * time
Rotation
- Robot base rotation
- The maximum rate of rotation is: (10 - 0.75 * abs(velocity)) deg/turn. The faster you're moving, the slower you turn.
- Gun rotation
- The maximum rate of rotation is: 20 deg/turn. This is added to the current rate of rotation of the robot.
- Radar rotation
- The maximum rate of rotation is: 45 deg/turn. This is added to the current rate of rotation of the gun.
Bullets
- Bullet damage
- 4 * firepower. If firepower > 1, it does an additional damage = 2 * (power - 1).
- Bullet velocity
- 20 - 3 * firepower.
- Gun heat generated on firing
- 1 + firepower / 5. You cannot fire if gunHeat > 0. All guns are hot at the start of each round.
- Energy returned on hit
- 3 * firepower.
Collisions
- Collision with another robot
- Each robot takes 0.6 damage. If a robot is moving away from the collision, it will not be stopped.
- Collision with a wall
- AdvancedRobots take damage = abs(velocity) * 0.5 - 1 (never < 0).
Robocode processing loop
The order that Robocode runs is as follows:
- Battle view is (re)painted.
- All robots execute their code until they take action (and are then paused).
- Time is updated (time++).
- All bullets move (including the bullet fired in the last tick) and are checked for collisions.
- All robots move (gun, radar, heading, acceleration, velocity, distance, in that order. gun heat is also decreased in this step).
- All robots perform scans (and collect team messages).
- All robots are resumed to take new action.
- Each robot processes its event queue.
Most of this can be gleaned by following the method calls from BaseBattle.runRound()
and Battle.runTurn()
in the robocode.battle
module.
Firing pitfall
Because bullets are fired before the gun is moved, calling setFire() will cause the bullet to leave at the current gun heading. This may seem counter-intuitive if you are used to thinking in terms of pointing a gun, then shooting. It is also inconvenient because you can't call setTurnGun(...)
and setFire(...)
right after each other (not if you need perfect accuracy, anyway). Most of the time, the error will be so small you won't notice it, but if you're testing a pattern matcher against sample.Walls, you will occasionally spot the bug.
To get the bullet to leave after turning the gun, you will need to use code like this:
long fireTime = 0;
void doGun() {
if (fireTime == getTime() && getGunTurnRemaining() == 0) {
setFire(2);
}
// ... aiming code ...
setTurnGunRight(...);
// Don't need to check whether gun turn will complete in single turn because
// we check that gun is finished turning before calling setFire(...).
// This is simpler since the precise angle your gun can move in one tick
// depends on where your robot is turning.
fireTime = getTime() + 1;
}
Event handling
Event dispatch happens from within commands that take a turn. So the call stack when an event is delivered usually looks like this:
Robocode internals → Robot's run method → Robocode internals → Event handler
However, event handlers can themselves make calls that take turns. If one of these happens to generate an event, we might see a call stack like
Robocode internals → Robot's run method → Robocode internals → First event handler → Robocode internals → Second event handler
But this kind of nesting could lead to a stack overflow. Or—more commonly—cases where the first handler finishes up its actions long after the response-provoking situation has passed. Thus, Robocode takes special steps for events generated within event handlers; these measures are implemented in EventManager.processEvents(). In particular, the call stack will get as far as
Robocode internals → Robot's run method → Robocode internals (including processEvents) → First event handler → Robocode internals (including processEvents)
but then the inner processEvents will detect the impending nesting and throw an EventInterruptedException, which unwinds the stack to the catch block in the outer processEvents:
Robocode internals → Robot's run method → Robocode internals (including processEvents)
effectively canceling whatever the running event handler was up to. Next, the event-delivering loop in the outer processEvents resumes delivering events, letting the second event handler execute unnested:
Robocode internals → Robot's run method → Robocode internals → Second event handler
It is possible, though not usually useful, to catch and respond to EventInterruptedExceptions in the first handler instead.
See also
Robocode API
Beginner Guides
- Welcome to Robocode
- System requirements
- Download and install
- Getting started
- Frequently asked questions
- My First Robot tutorial
- Game physics
- The anatomy of a robot
- Scoring in Robocode
- Using the robot console
- Downloading other robots
- Learning from other robots
- Package your robot
- Articles about Robocode
- Starting Robocode from the command line
- Graphical debugging
External Editors
- Using Eclipse with Robocode
- Creating a project in Eclipse
- Creating a robot in Eclipse
- Running your robot from Eclipse
- Debugging your robot with Eclipse
- Using NetBeans with Robocode
- Using Gradle with Robocode