Module pajammin.physics
The physics engine, which implements the basic physics for the bodies
of each entity. In particular, the physics engine handles:
-
detecting & reporting collisions during movement
-
updating position based on velocity
-
updating velocity based on acceleration
-
moving 'supported' objects when their support moves. E.g., if a
turtle is standing on a platform, and the platform moves, then the
turtle should also be moved.
Bodies
Each body has a set of basic properties defining its position and
movement:
-
Bounding
box: a rectangle which indicates the space currently occupied
by the body.
-
velocity:
the number of pixels that the body should move per second, in both
the x dimension (
xvel
) and the y dimension
(yvel
). (Only MoblieBody
s have
velocity.)
-
acceleration: the amount that the body's
velocity should be increased per second, in both the x dimension
(
xaccel
) and the y dimension (yaccel
).
(Only MoblieBody
s have acceeration.)
Solid Flags
Additionally, each body defines a set of flags which determine
when it will collide with other bodies.
First, the is_solid
flag determines whether the body
can collide with other bodies at all. If it false, then the body will
never collide with anything.
For solid bodies (i.e., bodies where is_solid=True
),
the solid_sides
flags indicates which sides of the body
are solid. If a given side of a body is solid, then any solid body
that moving into or through the body from that side will collide with
it. When two solid objects pass into or through each other, only
one of the objects needs to be solid on the relevant side to
cause a collision. Some examples should help clarify:
-
A wall is solid on all sides, since no solid bodies can pass
through it from any direction.
-
A platform is solid only on the top: if you land on it, you
will collide with it, but if you walk past it or jump up through
it, you will not.
-
Most monsters are not solid on any side, allowing the player
to walk past them.
Support
A body can be 'supported' by another body that it is adjacent to.
For example, when the player stands on a platform, it is supported by
that platform. If body B is supported by body S, then:
-
Whenever S moves, B is also moved (with the same
displacement).
-
If either of B's velocity components (xvel or yvel) would
cause it to collide with S, then that component is set to
zero.
If the supported body moves to a position where it is no longer
adjacent to the supporting body, then it automatically becomes
unsupported.
Classes |
Body |
An object in the physics engine. |
PhysicsEngine |
A physics engine for platform games. |
COLLISION
-
- Type:
-
int
- Value:
|
LOSE_SUPPORT
-
- Type:
-
int
- Value:
|
MOVEMENT
-
- Type:
-
int
- Value:
|