Skip to content

movement and collision

Gguidini edited this page May 13, 2021 · 3 revisions

The Movement and Collision systems are closely related. They are both esper systems, and enable 2D simulations only. Components used are:

  • Position
  • Velocity
  • Collidable

Movement System

The Movement system should be only system that effectively changes the Position component. All other systemas that intent to cause movement to entities should add Velocity to it. The Movement system also divides the simulation space in tiles, to optimize the Collision system. The tile size can be configured on system initialization.

Movement system init function has the following signature: __init__(self, minx, maxx, miny, maxy, sector_size=50). The 4 first parameters are the simulation size, than can be retrieved from Simulator.window_dimensions after creating the simulator instance. sector_size is the tile size, the unit measure is the point from JGraph map.

Collision System

The Collision system will only test entities that can move (e.g. have all 3 listed components) against any other entity with a Collidable component. Only entities in the same or adjacent sectors are tested.

💡
The sector_size should be slightly bigger than the largest entity in the simulation to avoid bugs.

Only convex shapes can be used, but a Collidable component can have an array of shapes, so you may approximate non-convex shapes with several convex shapes. To create a Collidable component, pass a list of ShapeDefinitions (declared in typehints). A ShapeDefinition is a tuple with a point, the center of the figure, and a list of points, the vertexes of the shape.

For example, a square of size 5 with corners in the coordinates [(0,0), (0,5), (5,5), (5,0)] whould be defined as `((2.5, 2.5), [(0,0), (0,5), (5,5), (5,0)]).

When a collision is detected a new event is added to EVENT_STORE. The payload is defined as below. The event tag can be defined in the Collidable component. To change the collision tag from the map add collision_tag annotation to the entity. The default collision tag is "genericCollision". You have to create your own system to handle collision events.

CollisionPayload = NamedTuple('CollisionEvent', [('ent', int), ('other_ent', int)])