Jump to content
  • Advertisement
Sign in to follow this  
jagguy2

collison algorthim

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
to answer your question...


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




in the most simplest form.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by jagguy2
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?


Say you have two overlapping rectangles.

Free Image Hosting at www.ImageShack.us


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 this post


Link to post
Share on other sites
Quote:
Original post by AnthonyS
Quote:
Original post by jagguy2
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?


Say you have two overlapping rectangles.

Free Image Hosting at www.ImageShack.us


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 this post


Link to post
Share on other sites
Quote:
Original post by jagguy2

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


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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!