Determining exact point of collision

BlitzPlus Forums/BlitzPlus Programming/Determining exact point of collision

Murilo(Posted 2004) [#1]
I'm using Blitz's pixel perfect collision detection functions, and they're working fine and danady. The trouble is, I now need to know the exact point a collision took place. For example:

I have two images; a sword and a ball. I'm checking to see when the sword has touched (punctured?) the ball. ImagesCollide tells me when this has taken place, but I now need to know the point where the sword connected with the ball (or at least one of the points where they touch). With me?

Any ideas how I could achieve this?


semar(Posted 2004) [#2]
One way could be, while the sword and the ball collide, to check for collision again, this time between special points you want to test.

Say your sword collides with the ball. Now, in this case, you can do any other collision test between other images - or rectangles - placed where you like on the sword, and the ball. Since images don't need to be drawn on the screen in order to perform the collision, you can achieve this with few other collision tests.

Anyway; are you sure you need such a collision detail ? If a sword hits a ball, the ball would be broken in any case, right ? Wouldn't be easyer just destroy the ball if a sword has hit it ? Just a thought...

Sergio.


QuietBloke(Posted 2004) [#3]
Another possible way might be :

If your sword is just a straight line then you can imagine a straight line going along the centre of the length. If you know the points from the tip and the base of the sword then a you can quickly calculate points along the length. Depending on how accurate you want to be you then split the length into points. So.. you may decide you only need 6 points along the length of the sword. For each point along the line of the sword you find the x and y distance to the centre of the ball and then find the ditance squared ( xdistance * xdistance + ydistance*ydistance ). Do this for each point and the point with the shortest distance to the ball centre is the closest to the point of contact. It might not be pixel perfect but might be good enough.


Murilo(Posted 2004) [#4]
The sword and ball scenario is just one example. I also need to test projectiles (bullets, rocks, fire) against player sprites. Why do I need to know the point of contact? I want to add a visual effect (animated blood, popping animation etc) at the point of impact.

Thanks for the suggestions.


QuietBloke(Posted 2004) [#5]
ok.. I dont have access to my machine so its going to be vague again.

You going to have to do the pixel perfect check again yourself.

First calculate the rectangle in each sprite for the overlap.

+---+
| |
| +-+--+
| | | |
+-+-+ |
| |
+----+

for example you might find the overlap on the first sprite is x=5, y = 5, x=16, y=16 and sprite 2 is x=0, y= 0, x = 11, y=11.

Now you can check each pixel within the rectangle in sprite 1 with sprite 2. If both are solid colours then that is one of the pixels where the collision occurs.

Of course you might find that simply using the centre of the overlay rect might be sufficiently close to use as the point of contact depending on the sprites and speeds.


Murilo(Posted 2004) [#6]
Yeah, it look like I'll be doing it manually. I just wondered if anyone had written an algorithm that simplified the process. Thanks.