views:

94

answers:

1

If I have a sprite, how would I check collision between two points? For example, in a game I am making, I would like to draw multiple lines that my sprite collides against. I'm thinking that this is more flexible than other collision systems if I had a lot of platforms.

+2  A: 

Some simple logic can help reduce wasted calculation, ie if the top of the sprite is lower then both of the points, you can't have a collision. Sort of a simple bounding box collision check.

Once you have done that, I would suggest that you get a 'formula' for your line, then check which of the corners of your sprite lay above or below that line. If they do not all lie on the same side, you have a collision.

for instance, if your line was of y=x/2+2, starting at x=-20 ending at x=20 and you had square sprite 3 wide/high at (3,-1) then you have the four points of your spirte (3,-1)(3,2)(6,2)(6,-1). You work out the y value of the line at those two x positions, which give you y=3.5 at x=3 and y=5 at x=6 all of those y values are greater then the y values of the cube, thus the cube is below the line.

thecoshman
Thanks for the answer!
Alu
What if the line was straight up?
Grue
I think my method will still work. It basically considers the line as a box and checks if any of the corners of the sprite fall within this box that the line makes. It is a very crude method, but for a simple thing it will work.
thecoshman