AI for Games 1.1.1
Loading...
Searching...
No Matches
game2dai.World Class Reference

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< BuildinggetBuildings (MovingEntity m)
 
Set< MovingEntitygetMovers (MovingEntity m)
 
Set< MovingEntitygetMovers (MovingEntity m, double range)
 
Set< WallgetWalls (MovingEntity m)
 
Set< WallgetWalls (MovingEntity m, double px, double py)
 
Set< ObstaclegetObstacles (MovingEntity m)
 
Set< ObstaclegetObstacles (double x, double y)
 
Set< ObstaclegetObstacles (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, BaseEntityallEntities = 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< BaseEntitydrawSet = new TreeSet<BaseEntity> (new BaseEntity.DepthOrder())
 
ArrayList< ChanglingentityChangeList = new ArrayList<Changling>()
 
Set< Artefactartefacts
 
Set< Buildingbuildings
 
Set< Wallwalls
 
Set< Obstacleobstacles
 
Set< MovingEntitymovers
 
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
 

Detailed Description

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.

Author
Peter Lager

Constructor & Destructor Documentation

◆ World() [1/3]

game2dai.World.World ( )
private

Make the default constructor private so it can't be used

◆ World() [2/3]

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. 



Parameters
widththe physical screen display width in pixels
heightthe physical screen display height in pixels

◆ World() [3/3]

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.

Parameters
widththe physical screen display width in pixels
heightthe physical screen display height in pixels
partitionSizethe size of the partition in world distance
partitionOverlapthe overlap between partitions

Member Function Documentation

◆ add() [1/6]

void game2dai.World.add ( Artefact  artefact)
private

Add an artefact .
A artefact entity may or may not have a renderer and a state machine.

◆ add() [2/6]

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.

Parameters
entitythe entity to add.

◆ add() [3/6]

void game2dai.World.add ( Building  b)
private

Adds a building to the world.

Parameters
bthe building to add.

◆ add() [4/6]

void game2dai.World.add ( MovingEntity  me)
private

Add a moving object to the world.

Parameters
methe moving object to add

◆ add() [5/6]

void game2dai.World.add ( Obstacle  obstacle)
private

Adds an obstacle to the world.

Parameters
obstaclethe obstacle to add.

◆ add() [6/6]

void game2dai.World.add ( Wall  w)
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.

Parameters
w

◆ birth()

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.

Parameters
entity
timeToBirth

◆ cancelBirthsAndDeaths()

void game2dai.World.cancelBirthsAndDeaths ( )

Cancels outstanding birth and deaths scheduled with birth and death methods.

◆ checkPartitionMoves()

void game2dai.World.checkPartitionMoves ( )
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

◆ death() [1/2]

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.

Parameters
entitythe entity to die
timeToLivenumber of seconds of life left

◆ death() [2/2]

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.

Parameters
entityIDthe ID number of the entity to die
timeToLivenumber of seconds of life left

◆ draw() [1/2]

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.

◆ draw() [2/2]

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.

Parameters
elapsedTimethe elapsed time since the last update

◆ ensureNoOvelapInPartition()

void game2dai.World.ensureNoOvelapInPartition ( double  deltaTime,
Set< MovingEntity partSet 
)
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.

◆ ensureNoOverlap() [1/2]

void game2dai.World.ensureNoOverlap ( double  deltaTime)
private

Ensure no overlap between moving entities when using BSP ignoring entities that touch across partition barriers.

◆ ensureNoOverlap() [2/2]

void game2dai.World.ensureNoOverlap ( double  deltaTime,
MovingEntity  m0,
MovingEntity  m1 
)
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.

Parameters
deltaTime
m0
m1

◆ exists() [1/2]

boolean game2dai.World.exists ( BaseEntity  entity)

Determines whether the entity still exists in this world.

Parameters
entity
Returns
true if entity is still alive

◆ exists() [2/2]

boolean game2dai.World.exists ( int  id)

Determines whether the entity still exists in this world.

Parameters
idthe entity's ID number
Returns
true if entity is still alive

◆ getBuildings()

Set< Building > game2dai.World.getBuildings ( MovingEntity  m)

For a particular moving entity get all the buildings in the same partition.

Parameters
mthe moving entity
Returns
a set of nearby buildings

◆ getEntity()

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.

Parameters
entityIDunique ID number for the entity
Returns
null if the entity was not found else the entity that has the given ID number

◆ getMovers() [1/2]

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.

Parameters
mthe moving entity
Returns
a set containing all the moving entities in its partition

◆ getMovers() [2/2]

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.

Parameters
mthe mover we are interested in getting the neighbours
rangethe radius of the circle surrounding neighbours.
Returns
a set containing the neighbours

◆ getObstacles() [1/2]

Set< Obstacle > game2dai.World.getObstacles ( MovingEntity  m)

Get a list of obstacles that need to be tested for obstacle avoidance.

Parameters
mthe moving entity
Returns
a set containing the obstacles (maybe empty but not null)

◆ getObstacles() [2/2]

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

Parameters
mthe moving entity
px
py
Returns
a set containing the obstacles (maybe empty but not null)

◆ getPartition()

Point game2dai.World.getPartition ( double  x,
double  y 
)
private

For a given point get the partition key

◆ getWalls() [1/2]

Set< Wall > game2dai.World.getWalls ( MovingEntity  m)

Get a list of walls that need to be tested for wall avoidance.

Parameters
mthe moving entity
Returns
a set containing the walls (maybe empty but not null)

◆ getWalls() [2/2]

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

Parameters
mthe moving entity
px
py
Returns
a set containing the walls (maybe empty but not null)

◆ isCspOn()

boolean game2dai.World.isCspOn ( )
Returns
the bspOn

◆ isNoOverlapOn()

boolean game2dai.World.isNoOverlapOn ( )
Returns
the ensureZeroOverlap

◆ makeDrawSet()

void game2dai.World.makeDrawSet ( )
protected

Make a set of entities that are to be drawn in z order

◆ moveToWorldX()

void game2dai.World.moveToWorldX ( double  wx)

Move the world view so it is centred about the given world position.

Parameters
wxcentre horizontally about this value.

◆ moveToWorldXY()

void game2dai.World.moveToWorldXY ( double  wx,
double  wy 
)

Move the world view so it is centred about the given world position.

Parameters
wxcentre horizontally about this value.
wycentre vertically about this value.

◆ moveToWorldY()

void game2dai.World.moveToWorldY ( double  wy)

Move the world view so it is centred about the given world position.

Parameters
wycentre vertically about this value.

◆ noOverlap()

void game2dai.World.noOverlap ( boolean  noOverlap)
Parameters
noOverlapthe ensureZeroOverlap to set

◆ panPixelX()

void game2dai.World.panPixelX ( int  px)

Pan the world display by the given number of pixels.

Parameters
pxpixels to pan horizontally.

◆ panPixelXY()

void game2dai.World.panPixelXY ( int  px,
int  py 
)

Pan the world display by the given number of pixels.

Parameters
pxpixels to pan horizontally.
pypixels to pan vertically.

◆ panPixelY()

void game2dai.World.panPixelY ( int  py)

Pan the world display by the given number of pixels.

Parameters
pypixels to pan vertically.

◆ panWorldX()

void game2dai.World.panWorldX ( double  wx)

Pan the world display by the given world distance.

Parameters
wxworld distance to pan horizontally.

◆ panWorldXY()

void game2dai.World.panWorldXY ( double  wx,
double  wy 
)

Pan the world display by the given world distance.

Parameters
wxworld distance to pan horizontally.
wyworld distance to pan vertically.

◆ panWorldY()

void game2dai.World.panWorldY ( double  wy)

Pan the world display by the given world distance.

Parameters
wyworld distance to pan vertically.

◆ pixel2world()

Vector2D game2dai.World.pixel2world ( int  px,
int  py,
Vector2D  wxy 
)

Calculate the equivalent world position from a pixel position.

Parameters
pxdisplay screen pixel x position
pydisplay screen pixel x position
wxya Vector2D object to hold the world position
Returns
the calculated world position

◆ printForceData()

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(); 

◆ remove() [1/6]

void game2dai.World.remove ( Artefact  artefact)
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)

◆ remove() [2/6]

void game2dai.World.remove ( BaseEntity  entity)
private

Remove an entity from the world.

Parameters
entitythe entity to use

◆ remove() [3/6]

void game2dai.World.remove ( Building  b)
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)

◆ remove() [4/6]

void game2dai.World.remove ( MovingEntity  me)
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)

◆ remove() [5/6]

void game2dai.World.remove ( Obstacle  obstacle)
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)

◆ remove() [6/6]

void game2dai.World.remove ( Wall  wall)
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)

◆ scale() [1/2]

double game2dai.World.scale ( )

Get the current viewScale (zoom factor)

Returns
view scale value

◆ scale() [2/2]

void game2dai.World.scale ( double  scale)

Resize the world due to changes in magnification so that the image is centred in the display area.

Parameters
scalethe level of magnification (must be > 0)

◆ update()

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.

Parameters
deltaTimethe time in seconds since the last update.

◆ viewOnWorld()

Domain game2dai.World.viewOnWorld ( )

Get the extent of the world being drawn. It returns a Domain object

Returns
Domain object with the extent of the world being drawn.

◆ world2pixel() [1/2]

Point game2dai.World.world2pixel ( double  wx,
double  wy,
Point  pxy 
)

Calculate the pixel position from a given world position.

Parameters
wxworld x position
wyworld y position
pxyobject to hold the calculate pixel position
Returns
the calculated pixel position

◆ world2pixel() [2/2]

Point game2dai.World.world2pixel ( Vector2D  wxy,
Point  pxy 
)

Calculate the pixel position from a given world position.

Parameters
wxyworld x,y position
pxyobject to hold the calculate pixel position
Returns
the calculated pixel position

◆ xOffset()

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.

Returns
the amount to translate before drawing

◆ yOffset()

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.

Returns
the amount to translate before drawing

Member Data Documentation

◆ nbr_artefacts

int game2dai.World.nbr_artefacts = 0

Number of artefacts in this world

◆ nbr_buildings

int game2dai.World.nbr_buildings = 0

Number of buildings in this world

◆ nbr_movers

int game2dai.World.nbr_movers = 0

Number of moving entities in this world

◆ nbr_obstacles

int game2dai.World.nbr_obstacles = 0

Number of obstacles in this world

◆ nbr_walls

int game2dai.World.nbr_walls = 0

Number of walls in this world

◆ worldUpdateTime

double game2dai.World.worldUpdateTime = 0

The average time it takes for the world to update all the components in the world.


The documentation for this class was generated from the following file: