Sprite Candy Collision Detection...

Blitz3D Forums/Blitz3D Programming/Sprite Candy Collision Detection...

NeilH(Posted 2009) [#1]
I've been playing around with collision detection in Sprite Candy, and I've now got to a point where I am completely stuck and just wondering if anybody has either come across the same, or has any advice on how I can get around the following...

All the main characters in my game are animated, and so I am handling this manually using the HUD_SetImageClip function to move around the texture to create the animation frames.

The problem I have got is that I just cannot get collision working to a suitable standard for the game.

There are two options by default; the first being to check if object A hits object B, however this is done on the 4 corners of the objects. If you have any transparency in the object it makes it look odd as the collision happens even when the blank spaces collide.

Second option is to use the shape editor and do shape collision, however you can have only 1 shape per object, even though the object is animated. So when the frame changes you are still on the original shape, even though the new animation frame does not reflect this shape anymore.

Has anybody come across anything to resolve this? Basically I suppose I am after a pixel perfect collision against the current clip of an object.

I'm sure there must be a way around this as I would be a fundamental gap in the Sprite Candy library!

Any help/advice appreciated!

NeilH


Nate the Great(Posted 2009) [#2]
make a simple oval shape around the players general area but be sure to make the part below the feet flat. That should do it.. you cant do pixel perfect collisions with animated images and have them look much more realistic... try a 2d physics engine instead.


Ian Martin(Posted 2011) [#3]
You can actually switch the collision shape with HUD_LoadCollisionShape or HUD_ClearCollisionShape and HUD_AddCollisionPoint.

I tried this with an animated object. First I would get the frame the object was on with HUD_GetAnimFrame and then load the collision shape to go with the frame. Since you are using HUD_SetImageClip, you already know the frame. To make the collision shape in the shape editor, load just the relevant frame, and make the shape as if it were just one image - using the second, third, etc. of the image strip will put the collision to the right of the object. So do each separately. Save each frame's collision shape as GuyFrame1.shp, GuyFrame2.shp, etc.

Check the collision shapes with HUD_DrawCollisionShapes turned on, in debug only as it kills performance! "Since this function uses common 2D drawing commands, it must be placed after RenderWorld and before Flip. Otherwise you won't see it's results. Use it for test purposes only."

I was using the switched shape for a 'spring' shaped enemy. It seemed to work OK, but I was looking through the objects twice so I decided I didn't really need to do it. I ended up setting the collision shape to a compromise between the three images, a medium size shape. It worked fine in the game because the spring was always compressing and decompressing, so you couldn't really tell what frame it was on.

I think the above suggestion to use an oval with a flat bottom would work well too. I have been using squares for a lot of the collision, using them slightly inside the object. Make the box bigger for enemies, smaller for the player's character. People won't mind if the bad guy collision is a tad off, if it's in their favor. They will REALLY mind if their character collision isn't good. Another option would be to use separate images for each leg, torso, etc. so each has it's own collision. It really matters what you are trying to accomplish. Do you just need collision against the feet, or are you needing to know where the torso, head, arms are? I'm planning on doing a game with a guy at some point and am thinking of having the torso, arms separate so I can do things like figure out if he is hanging by his arms or know when his body is crushed by two objects on either side.

Let us know if you come up with a good solution. I wish the collision shapes worked with strips too.