Sign in to follow this  

[API] Collision API proposition

Recommended Posts

ToohrVyk    1595
I am currently considering developing a collision detection system. I seek critics about its structure and layout, as well as usability in situations from your experience. My constraints for the API (due to the structure of my game) are: - The positions of objects are described to the collision system in terms of initial position and velocity, and duration1. - From there, the collision system determines (and makes available) a no-change duration: it is the longest duration (starting with the current time) during which no collision happens. - The collision system can be asked to compute all collisions that occur between the current time and an arbitrary time in the future. Therefore, my suggestion for an API is: Classes manager: the collision manager represents the core of the collision system. It integrates all the above functionality. group: a collision group represents a set of objects (such as "player bullets", "enemies", "player"). Instead of setting up manually which object can collide with which, and what the effects should be, collisions are configured on a group-group basis. hull: represents the volume occupied by an object. Object hull : hull class This class is implemented by several derived classes (sphere, AABB, OBB, convex polyhedron). The position( ) of a hull is determined by its initial_position and its velocity, as well as its start_time and end_time. All these properties are read-only (and set up at construction time). They may be changed with a change_movement( ) function : changes the velocity and end time(using the current value as the new start_time value, and the current position as the new initial_position). Collision groups : group class Each group is managed by a given manager (the owner), which is a read-only property of the group that is set upon creation of said group. Each group performs hull-controller associations, where hull is an object of type hull and controller is an arbitrary object (the type of which is a parameter of the group class) that responds to collisions involving its associated hull. Collision manager : manager class The manager monitors collisions between associated groups. Such an association can be created using a detect function, which takes as arguments two groups (the ownerof which is the current manager) and a callback binary function reaction( ). When a collision is detected between an object a of group A and b of group B, then the callback function that was passed to manage the collision between A and B is called with A[a] and B[b] as its arguments - where A[a] is a notation for the controller associated to a in group A. The manager also has the obvious nochangeduration( ) property, and the advance( nexttime ) function which computes all collisions until the specified time. Practical example : bouncing balls inside screen. I assume here that "bounce" is a physics function : a collision is detected between two hulls, it alters their movement based on their masses, velocities and relative positions.
// init
manager = new manager 
balls = new group
screen = new group

repeat 10 times
  sphere = new sphereHull (random position, radius, velocity)
  // also set up the sphere for rendering
  add the sphere-(sphere,mass=random) association to balls

rect = new screenHull
add the rect-(rect,mass=infinity) association to screen

make manager detect (balls,balls,bounce)
make manager detect (balls,screen,bounce)

// run

as often as possible:

every 15 milliseconds:
  make manager advance (15 milliseconds)
1: All durations and moments are described in arbitrary in-game time units. In-game time flow is independent from real-world time flow.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this