AI for Games 1.1.1
|
Classes | |
class | Changling |
Public Member Functions | |
World (int width, int height) | |
World (int width, int height, double partitionSize, double partitionOverlap) | |
Point | world2pixel (double wx, double wy, Point pxy) |
Point | world2pixel (Vector2D wxy, Point pxy) |
Vector2D | pixel2world (int px, int py, Vector2D wxy) |
void | scale (double scale) |
double | scale () |
void | panPixelX (int px) |
void | panPixelY (int py) |
void | panPixelXY (int px, int py) |
void | panWorldX (double wx) |
void | panWorldY (double wy) |
void | panWorldXY (double wx, double wy) |
void | moveToWorldX (double wx) |
void | moveToWorldY (double wy) |
void | moveToWorldXY (double wx, double wy) |
double | xOffset () |
double | yOffset () |
Domain | viewOnWorld () |
BaseEntity | getEntity (int entityID) |
Set< Building > | getBuildings (MovingEntity m) |
Set< MovingEntity > | getMovers (MovingEntity m) |
Set< MovingEntity > | getMovers (MovingEntity m, double range) |
Set< Wall > | getWalls (MovingEntity m) |
Set< Wall > | getWalls (MovingEntity m, double px, double py) |
Set< Obstacle > | getObstacles (MovingEntity m) |
Set< Obstacle > | getObstacles (double x, double y) |
Set< Obstacle > | getObstacles (MovingEntity m, double px, double py) |
boolean | exists (int id) |
boolean | exists (BaseEntity entity) |
void | add (BaseEntity entity) |
void | death (BaseEntity entity, double timeToLive) |
void | death (int entityID, double timeToLive) |
void | birth (BaseEntity entity, double timeToBirth) |
void | cancelBirthsAndDeaths () |
void | update (double deltaTime) |
void | draw () |
void | draw (double elapsedTime) |
boolean | isCspOn () |
boolean | isNoOverlapOn () |
void | noOverlap (boolean noOverlap) |
void | printForceData () |
Public Attributes | |
int | nbr_artefacts = 0 |
int | nbr_buildings = 0 |
int | nbr_obstacles = 0 |
int | nbr_walls = 0 |
int | nbr_movers = 0 |
double | worldUpdateTime = 0 |
Static Public Attributes | |
static HashMap< Integer, BaseEntity > | allEntities = new HashMap<Integer, BaseEntity>() |
Protected Member Functions | |
void | makeDrawSet () |
Private Member Functions | |
World () | |
void | remove (BaseEntity entity) |
void | add (Artefact artefact) |
void | add (Building b) |
void | add (Obstacle obstacle) |
void | add (Wall w) |
void | add (MovingEntity me) |
void | remove (Artefact artefact) |
void | remove (Obstacle obstacle) |
void | remove (Wall wall) |
void | remove (Building b) |
void | remove (MovingEntity me) |
void | ensureNoOverlap (double deltaTime) |
void | ensureNoOvelapInPartition (double deltaTime, Set< MovingEntity > partSet) |
void | ensureNoOverlap (double deltaTime, MovingEntity m0, MovingEntity m1) |
void | checkPartitionMoves () |
Point | getPartition (double x, double y) |
Private Attributes | |
Set< BaseEntity > | drawSet = new TreeSet<BaseEntity> (new BaseEntity.DepthOrder()) |
ArrayList< Changling > | entityChangeList = new ArrayList<Changling>() |
Set< Artefact > | artefacts |
Set< Building > | buildings |
Set< Wall > | walls |
Set< Obstacle > | obstacles |
Set< MovingEntity > | movers |
HashMap< Point, HashSet< Artefact > > | artefact_parts |
HashMap< Point, HashSet< Building > > | building_parts |
HashMap< Point, HashSet< Wall > > | wall_parts |
HashMap< Point, HashSet< Obstacle > > | obstacle_parts |
HashMap< Point, HashSet< MovingEntity > > | moving_parts |
boolean | noOverlap = true |
final boolean | cspOn |
final double | partSize |
final double | partOverlap |
Domain | viewOnWorld |
double | viewScale = 1.0 |
final double | displayWidth |
final double | displayHeight |
Static Private Attributes | |
static StopWatch | timer = new StopWatch() |
static double | cumTime |
static int | count = 0 |
This class represents the '2D game play area' on which all the game entities are placed. Internally all world coordinates are stored as doubles (rather than floats) to minimise rounding errors, particularly when using steering behaviours.
This means the size of the world is only limited by the range of numbers that can be stored in the double data type ( i.e. +/- 1.7976931348623157e+308)
This class provides the core functionality for maintaining collections of all the game entities.
When there are a large number of game entities, cell space partitioning (CSP) can significantly reduce the CPU time needed to update all the moving entities. It will also reduce the rendering time by not attempting to draw entities that fall outside of the viewable area. This is normally at the expense of additional memory needed for the data structures.
Depending on which constructor is used when creating the 'world' object determines whether CSP is used or not. The CSP implementation is totally transparent to the user - so apart from changing the constructor used no other changes are needed in the users code.
|
private |
Make the default constructor private so it can't be used
game2dai.World.World | ( | int | width, |
int | height | ||
) |
Create a world without cell space partitioning.
The height and width are the physical display area size in pixels. It maybe smaller than the width and height of the application's window.
The portion of the world to be displayed will be a initialised to a rectangle (0,0) to (width, height).
(0,0) ___________________________________ X axis | | | | | | | | | | | | |__________________________| (width, height) | Y axis
<br<blockquote>
This gives a view scale of 1. (i.e. 1 pixel = 1 world unit)
The actual part of the world to be displayed cane be changed with the
moveTo, panPixel, panWorld & setScale methods.
width | the physical screen display width in pixels |
height | the physical screen display height in pixels |
game2dai.World.World | ( | int | width, |
int | height, | ||
double | partitionSize, | ||
double | partitionOverlap | ||
) |
Create a world with cell space partitioning.
The height and width are the physical display area size in pixels. It maybe smaller than the width and height of the applications window.
The portion of the world to be displayed will be a initialised to a rectangle (0,0) to (width, height).
(0,0) ___________________________________ X axis | | | | | | | | | | | | |__________________________| (width, height) | Y axis
<br<blockquote>
This gives a view scale of 1. (i.e. 1 pixel = 1 world unit)
The actual part of the world to be displayed cane be changed with the
moveTo, panPixel, panWorld & setScale methods.
This constructor will cause cell-space partitioning to be used. Use the following guide when deciding on the values for the partition size and overlap parameters
partitionSize >= 5 x MCR
partitionOverlap >= 2 x MCR
partitionSize >= 2 x partitionOverlap
partitionOverlap > whisker length if using steering behaviours with wall avoidance enabled
<br<blockquote>
where MCR is the maximum collision radius for your moving entities. The values used will have a significant effect on performance so it is worth experimenting with different values for your game.
width | the physical screen display width in pixels |
height | the physical screen display height in pixels |
partitionSize | the size of the partition in world distance |
partitionOverlap | the overlap between partitions |
|
private |
Add an artefact .
A artefact entity may or may not have a renderer and a state machine.
void game2dai.World.add | ( | BaseEntity | entity | ) |
This method must only be used before the main program starts. If it is called during a world update then the program is likely to crash, in this case use the birth() method instead.
entity | the entity to add. |
|
private |
Adds a building to the world.
b | the building to add. |
|
private |
Add a moving object to the world.
me | the moving object to add |
|
private |
Adds an obstacle to the world.
obstacle | the obstacle to add. |
|
private |
Add a wall to the world.
When looking to see whether the wall passes through any particular partition then it will consider the extended partition i.e. as if it had been extended on all sides by 'partSize'.
partSize should be greater than the whisker length used in wall direction.
w |
void game2dai.World.birth | ( | BaseEntity | entity, |
double | timeToBirth | ||
) |
This is the preferred method to add an entity to the world once the game is in progress i.e. outside the
setup()
method.
entity | |
timeToBirth |
void game2dai.World.cancelBirthsAndDeaths | ( | ) |
Cancels outstanding birth and deaths scheduled with birth and death methods.
|
private |
If we are using space partitioning then check if movers have crossed a partition boundary and if so move it to the correct hash set
void game2dai.World.death | ( | BaseEntity | entity, |
double | timeToLive | ||
) |
This is the preferred method to remove an entity from the world once the game is in progress i.e. outside the
setup()
method.
entity | the entity to die |
timeToLive | number of seconds of life left |
void game2dai.World.death | ( | int | entityID, |
double | timeToLive | ||
) |
This is the preferred method to remove an entity from the world once the game is in progress i.e. outside the
setup()
method.
entityID | the ID number of the entity to die |
timeToLive | number of seconds of life left |
void game2dai.World.draw | ( | ) |
DO NOT USE THIS METHOD
This method is left to ensure backward compatibility with library versions prior to 1.0
Use the draw(elapsedTime) method instead.
void game2dai.World.draw | ( | double | elapsedTime | ) |
Any entity that requires to be rendered will have its own render object. This method ensures that the draw method of each render object is executed.
It will not skip entities that are completely outside the visible area of the world.
elapsedTime | the elapsed time since the last update |
|
private |
Ensure no overlap between moving entities within a partition. If BSP is not being used then the 'whole world' is a single partition so this method will be used for a set of all movers.
Entities that touch across partition barriers are ignored.
|
private |
Ensure no overlap between moving entities when using BSP ignoring entities that touch across partition barriers.
|
private |
Checks to see if there is overlap between the entities. If there is it moves both entities slightly apart along the collision plane normal. The distance an entity moves is proportional to the mass of the other entity.
deltaTime | |
m0 | |
m1 |
boolean game2dai.World.exists | ( | BaseEntity | entity | ) |
Determines whether the entity still exists in this world.
entity |
boolean game2dai.World.exists | ( | int | id | ) |
Determines whether the entity still exists in this world.
id | the entity's ID number |
Set< Building > game2dai.World.getBuildings | ( | MovingEntity | m | ) |
For a particular moving entity get all the buildings in the same partition.
m | the moving entity |
BaseEntity game2dai.World.getEntity | ( | int | entityID | ) |
Get the entity associated with a particular ID number.
If we are not using state machines or if the id number has not been used then the entity will not be found.
entityID | unique ID number for the entity |
Set< MovingEntity > game2dai.World.getMovers | ( | MovingEntity | m | ) |
For a particular moving entity get all the moving entities in the same partition. It will include itself.
m | the moving entity |
Set< MovingEntity > game2dai.World.getMovers | ( | MovingEntity | m, |
double | range | ||
) |
Get a list of moving entities in partitions within a given range of a moving entity.
m | the mover we are interested in getting the neighbours |
range | the radius of the circle surrounding neighbours. |
Set< Obstacle > game2dai.World.getObstacles | ( | MovingEntity | m | ) |
Get a list of obstacles that need to be tested for obstacle avoidance.
m | the moving entity |
Set< Obstacle > game2dai.World.getObstacles | ( | MovingEntity | m, |
double | px, | ||
double | py | ||
) |
Get a list of obstacles that are in the box made by the moving entity's position and the position px,py
m | the moving entity |
px | |
py |
|
private |
For a given point get the partition key
Set< Wall > game2dai.World.getWalls | ( | MovingEntity | m | ) |
Get a list of walls that need to be tested for wall avoidance.
m | the moving entity |
Set< Wall > game2dai.World.getWalls | ( | MovingEntity | m, |
double | px, | ||
double | py | ||
) |
Get a list of walls that are in the box made by the moving entity's position and the position px,py
m | the moving entity |
px | |
py |
boolean game2dai.World.isCspOn | ( | ) |
boolean game2dai.World.isNoOverlapOn | ( | ) |
|
protected |
Make a set of entities that are to be drawn in z order
void game2dai.World.moveToWorldX | ( | double | wx | ) |
Move the world view so it is centred about the given world position.
wx | centre horizontally about this value. |
void game2dai.World.moveToWorldXY | ( | double | wx, |
double | wy | ||
) |
Move the world view so it is centred about the given world position.
wx | centre horizontally about this value. |
wy | centre vertically about this value. |
void game2dai.World.moveToWorldY | ( | double | wy | ) |
Move the world view so it is centred about the given world position.
wy | centre vertically about this value. |
void game2dai.World.noOverlap | ( | boolean | noOverlap | ) |
noOverlap | the ensureZeroOverlap to set |
void game2dai.World.panPixelX | ( | int | px | ) |
Pan the world display by the given number of pixels.
px | pixels to pan horizontally. |
void game2dai.World.panPixelXY | ( | int | px, |
int | py | ||
) |
Pan the world display by the given number of pixels.
px | pixels to pan horizontally. |
py | pixels to pan vertically. |
void game2dai.World.panPixelY | ( | int | py | ) |
Pan the world display by the given number of pixels.
py | pixels to pan vertically. |
void game2dai.World.panWorldX | ( | double | wx | ) |
Pan the world display by the given world distance.
wx | world distance to pan horizontally. |
void game2dai.World.panWorldXY | ( | double | wx, |
double | wy | ||
) |
Pan the world display by the given world distance.
wx | world distance to pan horizontally. |
wy | world distance to pan vertically. |
void game2dai.World.panWorldY | ( | double | wy | ) |
Pan the world display by the given world distance.
wy | world distance to pan vertically. |
Calculate the equivalent world position from a pixel position.
px | display screen pixel x position |
py | display screen pixel x position |
wxy | a Vector2D object to hold the world position |
void game2dai.World.printForceData | ( | ) |
Used to display all collected steering force data by Vehicle
A vehicle must have it's force logger activated with
vehicle.forceLoggerOn();
|
private |
Removes an artefact from the world.
The only safe way to remove this artefact is to use one of these methods -
void die(BaseEntity entity, double timeToLive)
void die(int entityID, double timeToLive)
|
private |
Remove an entity from the world.
entity | the entity to use |
|
private |
Removes a building from the world.
The only safe way to remove an entity is to use one of these methods -
void die(BaseEntity entity, double timeToLive)
void die(int entityID, double timeToLive)
|
private |
Removes a moving entity or vehicle from the world.
The only safe way to remove an entity is to use one of these methods -
void die(BaseEntity entity, double timeToLive)
void die(int entityID, double timeToLive)
|
private |
Removes an obstacle from the world.
The only safe way to remove an entity is to use one of these methods -
void die(BaseEntity entity, double timeToLive)
void die(int entityID, double timeToLive)
|
private |
Removes a wall from the world.
The only safe way to remove an entity is to use one of these methods -
void die(BaseEntity entity, double timeToLive)
void die(int entityID, double timeToLive)
double game2dai.World.scale | ( | ) |
Get the current viewScale (zoom factor)
void game2dai.World.scale | ( | double | scale | ) |
Resize the world due to changes in magnification so that the image is centred in the display area.
scale | the level of magnification (must be > 0) |
void game2dai.World.update | ( | double | deltaTime | ) |
This is the core method which will loop through entity state machines (if any) then it will update the positions and velocities of all moving entities (applying any steering behaviours used).
It also calculates and stores the average time taken to perform this update.
deltaTime | the time in seconds since the last update. |
Domain game2dai.World.viewOnWorld | ( | ) |
Get the extent of the world being drawn. It returns a Domain object
Point game2dai.World.world2pixel | ( | double | wx, |
double | wy, | ||
Point | pxy | ||
) |
Calculate the pixel position from a given world position.
wx | world x position |
wy | world y position |
pxy | object to hold the calculate pixel position |
Point game2dai.World.world2pixel | ( | Vector2D | wxy, |
Point | pxy | ||
) |
Calculate the pixel position from a given world position.
wxy | world x,y position |
pxy | object to hold the calculate pixel position |
double game2dai.World.xOffset | ( | ) |
Get the world x position equivalent to the left hand side of the display area.
This is used when rendering the world view.
double game2dai.World.yOffset | ( | ) |
Get the world x position equivalent to the left hand side of the display area.
This is used when rendering the world view.
int game2dai.World.nbr_artefacts = 0 |
Number of artefacts in this world
int game2dai.World.nbr_buildings = 0 |
Number of buildings in this world
int game2dai.World.nbr_movers = 0 |
Number of moving entities in this world
int game2dai.World.nbr_obstacles = 0 |
Number of obstacles in this world
int game2dai.World.nbr_walls = 0 |
Number of walls in this world
double game2dai.World.worldUpdateTime = 0 |
The average time it takes for the world to update all the components in the world.