Package pajammin :: Module physics :: Class PhysicsEngine
[show private | hide private]
[frames | no frames]

Class PhysicsEngine


A physics engine for platform games.

The physics engine manages a collection of bodies. These bodies are moved automatically, based on their velocities, by calling update at regular intervals; and can also be moved explicitly (e.g., with move).
Method Summary
  __init__(self)
  add(self, body)
  bodies(self, include_nonsolids)
Return a list of all bodies managed by the physics engine
  bodies_intersecting_point(self, x, y, include_nonsolids)
Return a list of all bodies intersecting the given point.
  bodies_intersecting_rect(self, rect, include_nonsolids)
Return a list of all bodies intersecting the given rect.
  jump(self, body, dx, dy, check_collisions)
If it is possible to move the given body by the specified distance without causing a collision at the destination, then do so.
  jump_to(self, body, x, y, check_collisions)
  move(self, body, dx, dy, report_collisions)
Move the given body (and any supported bodies) by the specified distance.
  move_to(self, body, x, y, report_collisions)
  remove(self, body)
  set_bbox(self, body, bbox, check_collisions)
Attempt to change the given body's bounding box to bbox.
  update(self, interval, pos)
Update the positions of all bodies in the world.
  _check_collide(self, body, dx, dy)
Check if moving the given body by (dx,dy) would cause a collision with any other body; and if so, then raise a _Collision exception describing it.
  _check_collide_path(self, body, dx, dy)
Check if moving the given body by (dx,dy) would cause a collision with any other body.
  _do_move(self, body, dx, dy)
Move the given body by the specified distance.
  _get_collision_side(self, body, obs_bbox, dx, dy)
Given that moving body by (dx,dy) causes a collision with the bounding box obs_bbox, figure out which side it collided with it on.
  _move_to_collision(self, body, collision, dx, dy, report_collisions)
Try to move the given body up to be adjacent with the body specified by the given collision.
  _move_with_supporteds(self, body, dx, dy, report_collisions)
  _move_without_supporteds(self, body, dx, dy, report_collisions)
Move the given body (but not supported bodies) by the specified distance.
  _moves_before(self, body, dx, dy)
Given that body is a body supported by a support, and that the support is moving by the given distance, return True if this body should be moved before the support (to prevent the support from colliding with it).
  _update_mobilebody(self, body, interval, pos)

Instance Variable Summary
  _body_grid: A grid containing all bodies managed by the physics engine.
  _pseudobody_grid: A grid containing 'pseudobodies.' These don't interact with other bodies in any way, but act as place markers for some objects (e.g., background objects like trees).

Class Variable Summary
bool debug = False
int _GRID_SIZE = 60                                                                    

Method Details

__init__(self)
(Constructor)

add(self, body)

bodies(self, include_nonsolids=True)

Returns:
A list of all bodies managed by the physics engine

bodies_intersecting_point(self, x, y, include_nonsolids=True)

Returns:
A list of all bodies intersecting the given point.

bodies_intersecting_rect(self, rect, include_nonsolids=True)

Returns:
A list of all bodies intersecting the given rect.

jump(self, body, dx, dy, check_collisions=True)

If it is possible to move the given body by the specified distance without causing a collision at the destination, then do so. Otherwise, just return False. Do not move supported objects.
Parameters:
dx, dy - The distance to move.
Returns:
True if the given body was successfully moved.

jump_to(self, body, x, y, check_collisions=True)

move(self, body, dx, dy, report_collisions=True)

Move the given body (and any supported bodies) by the specified distance. If the movement results in a collision (by this body -- not supported ones), then generate collision messages for both colliding bodies, and return False. Otherwise, return True.
Parameters:
dx, dy - The distance to move.
report_collisions - If False, then collision reporting is supressed.

move_to(self, body, x, y, report_collisions=True)

remove(self, body)

set_bbox(self, body, bbox, check_collisions=True)

Attempt to change the given body's bounding box to bbox. If that would cause a collision, then just return false; do not perform the move or generate any collision messages.

update(self, interval, pos)

Update the positions of all bodies in the world. Bodies that are close to the given pos (within 1000 pixels or so) will be updated at a much higher frequency than bodies that are farther away.

_check_collide(self, body, dx, dy)

Check if moving the given body by (dx,dy) would cause a collision with any other body; and if so, then raise a _Collision exception describing it.

_check_collide_path(self, body, dx, dy)

Check if moving the given body by (dx,dy) would cause a collision with any other body. If dx>body.width or dy>body.height, then also check for collisions at intermediate locations on the path. If moving by (dx,dy) would cause a collision, then raise a _Collision exception describing it.

_do_move(self, body, dx, dy)

Move the given body by the specified distance. Do not check for collisions. Do not move supported objects.

_get_collision_side(self, body, obs_bbox, dx, dy)

Given that moving body by (dx,dy) causes a collision with the bounding box obs_bbox, figure out which side it collided with it on.

_move_to_collision(self, body, collision, dx, dy, report_collisions)

Try to move the given body up to be adjacent with the body specified by the given collision. If this movement causes a new collision, then the new collision takes precedence.

When possible, the body will 'slide' along the obstacle (rather than just sticking to it). I.e., if it collides in the x direction, then it will still attempt to move the specified in the y direction; and vice versa.
Parameters:
report_collisions - If False, then collision reporting is supressed.
Returns:
True iff we were able to move up to be adjacent with the body we collided with (i.e., if we didn't collide with any intermediate bodies).

_move_with_supporteds(self, body, dx, dy, report_collisions)

_move_without_supporteds(self, body, dx, dy, report_collisions)

Move the given body (but not supported bodies) by the specified distance. If the movement results in a collision (by this body -- not supported ones), then generate collision messages for both colliding bodies, and return False. Otherwise, return True.
Parameters:
dx, dy - The distance to move.
report_collisions - If False, then collision reporting is supressed.

_moves_before(self, body, dx, dy)

Given that body is a body supported by a support, and that the support is moving by the given distance, return True if this body should be moved before the support (to prevent the support from colliding with it).

_update_mobilebody(self, body, interval, pos)


Instance Variable Details

_body_grid

A grid containing all bodies managed by the physics engine.

_pseudobody_grid

A grid containing 'pseudobodies.' These don't interact with other bodies in any way, but act as place markers for some objects (e.g., background objects like trees).

Class Variable Details

debug

Type:
bool
Value:
False                                                                  

_GRID_SIZE

Type:
int
Value:
60