# How to detect collisions between sprite and a user generated shape of some sort?

How to detect a collision between a sprite and a user generated shape of some sort.

For example. There are some objects on the screen. The user takes their finger and draws an circle shape around a object (The selection rule is painting circle around the sprite, but the painting shapes may be various). I need to detect which object selected, which just like:

(demo images) http://i52.tinypic.com/28h0t1g.png

-

If the shape is drawn primarily with line segments from the user (or Bezier curves with control points), you can implement a fairly widely used algorithm known as Separating-Axis-Theorem. Simply put: if there is an axis (unit-vector representing a direction) on which the shapes don't overlap (their projected values do not result in a positive difference), the objects are not overlapping. I have used this in the past and it has worked like a charm.

-

If shape is hand-drawn like in Crayon Physics, you can hold an array of straight lines and check collisions with each of them. Despite of straight lines you can use Bezier curves or something like that.

-
• Create a buffer of same size as screen (or surface where are Your shapes). At each position it contains boolean whether there is sprite; then, check all "pixels" of user shape whether at their position is the sprite (by checking value of that boolean). Alternatively, You can create more sprites there by storing their ID instead of boolean; but this was case of 1 sprite.
• If user generated shape can be represented by sequence of lines, then You can check whether each such line crosses sprite. Since sprite has rectangular shape and user shape is a line just Look for "line rectangle intersection" ... (separating axes algorithm si one way to do it)

Approaches depend on what data structures You choose, whether they are bitmaps or vectors.

First approach can handle arbitrary complex shapes, is simple to implement but uses more memory. Actually You can reduce memory overhead by using compression and speed it up by using hierachical data strutures (octrees) ...

Second approach , is not so simple to implement but uses more processing power.

In each case measure if it matters. I would try to do first one because it is simpler to implement. Good luck. :)

-