Accurate collision detection with animated meshes

Blitz3D Forums/Blitz3D Programming/Accurate collision detection with animated meshes

Matty(Posted 2005) [#1]
Hi, I was mucking around on my PC in blitz3d the other night and thought of a simple way to detect collisions between 'bullets' and animated meshes regardless of whether they are md2 or b3d or whatever.

I've tested it out at home and it works quite well so I thought I'd share it here - only 'pseudocode' below so it should be easy to follow:

Create animated character model as normal, with texture.

Create secondary texture for character model with bodily regions marked out in different colors, example white for face, green for left arm, red for right arm, yellow for upper torso etc etc

In main game loop when a bullet is fired and is within a small distance from the character being shot at perform this process:

Change texture on character to the secondary texture.

Place camera at bullet position with same orientation as bullet.

Set camera range to 'very small' near and a far distance of the distance that the bullet travels each frame.

Hide all entities other than the character being shot at (or simply move them and the bullet/camera to a location where the camera won't see anything else) .

Call 'renderworld'.

Call 'getcolor' at the coordinates of the centre of the camera's viewport.

Based on the pixel's color we can determine the damage location ie if it was black (background) then the bullet has not hit anything, if it was white (or close to white) (using secondary texture example above) then a head shot has been scored (play appropriate animation), it it was green (or close to green) then the left arm has been hit (play appropriate animation, perhaps have a particle effect spray of blood from the bullet's current location)

Prior to rendering the scene set all the textures back to normal and show all the entities again and proceed as normal.

If necessary I'll post some code when I can with an example model.


Beaker(Posted 2005) [#2]
Not a bad idea. If your character is rather complex: instead of changing texture you might find rendering a simpler proxy 'stickman' mesh of the character with the texture already attached is worth a try. I'm not sure how easy it is to keep 2 animated meshes in sync (particularly MD2s) but worth a try [EDIT: I found a way to keep them in sync if you're interested].

This method might also allow you to use vertex colors intead of a texture. I'm not sure how you would handle areas that are blended between different vertex colors tho.


IPete2(Posted 2005) [#3]
I 'love' a smart thinker! Nice one Matty!

IPete2.