Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created April 20, 2012 17:33
Show Gist options
  • Save slembcke/2430501 to your computer and use it in GitHub Desktop.
Save slembcke/2430501 to your computer and use it in GitHub Desktop.
Nearest Point Queries
// ** (vanilla) Point Queries:
// For the shape specific function, you give it a point and it returns true/false if the point is inside:
cpBool cpShapePointQuery(cpShape *shape, cpVect p);
// The "easy" space query function takes a point and some filter parmeters and returns the shape at the point or NULL:
cpShape *cpSpacePointQueryFirst(cpSpace *space, cpVect point, cpLayers layers, cpGroup group);
// The callback based iterator version looks like this:
typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);
void cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data);
// ** Segment Queries
// The shape specific function returns a bool if the segment hits the shape as well as an out parameter for the info struct.
// The returned bool is redundant, but useful.
cpBool cpShapeSegmentQuery(cpShape *shape, cpVect a, cpVect b, cpSegmentQueryInfo *info);
// The "easy" segment query works like cpSpacePointQuery above.
cpShape *cpSpaceSegmentQueryFirst(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo *out);
// Same for the iterator version
typedef void (*cpSpaceSegmentQueryFunc)(cpShape *shape, cpFloat t, cpVect n, void *data);
void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void *data);
// ** Nearest Point Queries
// The shape specific queries return nothing. (should it at least return the distance?)
// All the data info is passed back through the out parameter.
void cpShapeNearestPointQuery(cpShape *shape, cpVect p, cpNearestPointQueryInfo *out);
// The "easy" query function takes a point and a maximum distance.
// The maxDistance makes it a little different than the others, but is necessary.
cpShape *cpSpaceNearestPointQueryNearest(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpNearestPointQueryInfo *out);
// The iterator version isn't too surprising.
typedef void (*cpSpaceNearestPointQueryFunc)(cpShape *shape, cpFloat distance, cpVect point, void *data);
void cpSpaceNearestPointQuery(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpSpaceNearestPointQueryFunc func, void *data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment