So I felt inspired the last day or two and started to implement raycasting. (Hooray and all right?) Actually, it's not really raycasting, but a directed segment query. You can't actually raycast into a spatial hash due to it having an infinite number of cells that would need to be traversed. I think that having an an explicit maximum ray distance is not so bad of a limitation for a 2D game though.
What I have so far is a function cpShapeSegmentQuery() that you can use to query against individual shapes. Parameters are:
- a shape to query against
- a segment start and end point
- a layers bitmask and a group identifier (works just like with between collision shapes)
- a pointer to a query info struct (pass NULL to have it malloc() and return a new one)
The query info structure contains the following information:
- the shape hit by the segment
- the alpha value of the collision between the start and end point
- the distance from the start to the collision point (probably going to be removed, easy to calculate if you want it)
- the collision point (probably going to be removed, easy to calculate if you want it)
- the normal of the surface at the collision point
Does that seem reasonable? Am I missing anything obvious or useful?
Currently the point query API has two separate functions for querying static and active shapes. At the time it seemed like a good idea as I was thinking about it in terms of grabbing objects with the mouse. I figured one would want an easy way to only specify active shapes for selection. Now this seems like sort of a bad idea as it means that you need to call both functions if you want both. Not so bad with point queries where you just throw away the value you don't want, but it would be annoying to call a query-for-first-hit function for each set of objects, need to determine which is closer, and free the info for the other. I don't really want to write 6 functions for both point queries and segment queries (active, static, both)x(all, first) when they all practically do the same thing. Active/Static versions could be done away with using on of the layer bits to tag active/static status. What do people think?
At the moment, I'm leaning towards letting the user tag static objects with a layer bit if they want. Other than mouse selection of objects, it doesn't seem useful, and setting a layer bit to mark an object as grabbable would be more flexible anyway.