Public Group

# collison algorthim

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I want a collision algorithm to implement in vb.net. I have 2 images and I can use intersect Rect to determine if the images hit . After that I need to work out how to get a more accurate hit on the images because the image itself is not a perfect rectangle. The image can be an odd shape. What is way I can a accurate test to see if images collide after I know that both images are touching from their rectangle bounds?

##### Share on other sites
I think you'll want to actually check with the source image data, to see if solid pixels (ie not your transparent color) on image 1 is touching solid pixels on image 2. This is normally called "per pixel collision detection."

##### Share on other sites

if (obj1Right > obj2Left && obj1Left < obj2Right && obj1Top > obj2Bottom && obj1Bottom < obj2Top){ // collision code}

in the most simplest form.

##### Share on other sites
My question is AFTER I have detected a collision with 2 rectangle images, how do I get a more accurate test.
I have a spaceship on 1 image and it doesnt cover the whole rectangle it is placed in. I have a missile which also doesnt take up the whole rectangle it is placed in.

I test to see if the rectangles overlap which I can do.

I need a further test to see if actual pixels are touched within the image and this i want help with "per pixel collision detection. How do i do this?

##### Share on other sites
I'll give it a try in pseudocode...

bool CheckOverlap(image1, image2, position1, position2){    relativePosition = position2 - position1    for x = 0 to image1.width    {        for y = 0 to image1.height        {            if image1.pixelIsSolid(x, y) &&                image2.pixelIsSolid(x - relativePosition.x, y - relativePosition.y)            {                return true            }        }    }    return false}bool Image::pixelIsSolid(int x, int y){    if x >= 0 && x < width && y >= 0 && y < height        return pixel[x + y * width] != transparentColor    else        return false}

For these sorts of problems, you can usually make it much simpler by imagining that one of the objects in question is placed at the origin, and just use the relative position of the other to do the comparisons.

##### Share on other sites
Quote:
 Original post by jagguy2My question is AFTER I have detected a collision with 2 rectangle images, how do I get a more accurate test.I have a spaceship on 1 image and it doesnt cover the whole rectangle it is placed in. I have a missile which also doesnt take up the whole rectangle it is placed in.I test to see if the rectangles overlap which I can do.I need a further test to see if actual pixels are touched within the image and this i want help with "per pixel collision detection. How do i do this?

Say you have two overlapping rectangles.

You'll need to create two bitmaps of the area that's Xed in where the x,y coordinates correspond, you don't actually have to create a new bitmap, just calculate the correct pixels.

With most bitmaps you have an r,g,b,a value. The 'a' or alpha value should be 0 when an area is transparent. Simply iterate through that box and if the alpha values are both greater than 0 you know you have a collision.

[Edited by - AnthonyS on June 29, 2008 9:36:43 PM]

##### Share on other sites
Quote:
Original post by AnthonyS
Quote:
 Original post by jagguy2My question is AFTER I have detected a collision with 2 rectangle images, how do I get a more accurate test.I have a spaceship on 1 image and it doesnt cover the whole rectangle it is placed in. I have a missile which also doesnt take up the whole rectangle it is placed in.I test to see if the rectangles overlap which I can do.I need a further test to see if actual pixels are touched within the image and this i want help with "per pixel collision detection. How do i do this?

Say you have two overlapping rectangles.

You'll need to create two bitmaps of the area that's Xed in where the x,y coordinates correspond, you don't actually have to create a new bitmap, just calculate the correct pixels.

With most bitmaps you have an r,g,b,a value. The 'a' or alpha value should be 0 when an area is transparent. Simply iterate through that box and if the alpha values are both greater than 0 you know you have a collision.

I am using silverlight so i dont have a alpha value I believe. Correct me if i am wrong but it only uses jpg,png with transparent backgrounds

##### Share on other sites
Quote:
 Original post by jagguy2I am using silverlight so i dont have a alpha value I believe. Correct me if i am wrong but it only uses jpg,png with transparent backgrounds

I believe JPEG is a bad choice for images with an alpha channel, however most image editors export to RGBA format for both jpg and png.

If you can't use an alpha channel just tag some color as your official transparency code, perhaps white (0x00) for most formats. If you're using a compressed (lossy) format you may need to compare the values within a certain range.

if( max(value, transparency) - min(value, transparency) < someSmallValue )

##### Share on other sites
>With most bitmaps you have an r,g,b,a value. The 'a' or alpha value should be 0 when an area is transparent. Simply iterate through that box and if the alpha values are both greater than 0 you know you have a collision.

The instructions on images are limited.
q1)I cant find out how to do this in silverlight?

q2)Also if i use my own color for background how do I stop the background from displaying ?

[Edited by - jagguy2 on June 29, 2008 11:03:09 PM]

##### Share on other sites
Maybe this article could be of any help? First do the raw rectangle collision detection, then if that returns a collision, do the per pixel test, using the ideas described in the article on whatever image you're using. You might need a mask of your images.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015437
×