Jump to content
  • Advertisement
Sign in to follow this  
Bladelock

Collision Detection using Color of Sprite in SFML?

This topic is 2604 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 have a character sprite and a background sprite(which is just black and white).

I was wondering since my background sprite is a bunch of black lines with white background,
is it possible to set the collision detection by checking whether my character sprite "collides" with the black parts of my background sprite?
I'm using SFML.
Thanks!

PS:
-The background sprite covers the whole window of the SFML application.

Share this post


Link to post
Share on other sites
Advertisement

Hi, I have a character sprite and a background sprite(which is just black and white).

I was wondering since my background sprite is a bunch of black lines with white background,
is it possible to set the collision detection by checking whether my character sprite "collides" with the black parts of my background sprite?
I'm using SFML.
Thanks!

PS:
-The background sprite covers the whole window of the SFML application.




You should be able to modify http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection]this code[/url], but use color instead of alpha. Not sure how the performance will be, per pixel operations tend to be quite slow.

[color=#0000FF][font=monospace]bool[/font][font=monospace] Collision[/font][font=monospace]::[/font][font=monospace]PixelPerfectTest[/font][font=monospace]([/font][color=#0000FF][font=monospace]const[/font][font=monospace] sf[/font][font=monospace]::[/font][font=monospace]Sprite[/font][font=monospace]&[/font][font=monospace] Object1, [/font][color=#0000FF][font=monospace]const[/font][font=monospace] sf[/font][font=monospace]::[/font][font=monospace]Sprite[/font][font=monospace]&[/font][font=monospace] Object2, sf[/font][font=monospace]::[/font][font=monospace]Uint8[/font][font=monospace] AlphaLimit[/font][font=monospace])[/font][font=monospace] [/font][font=monospace]{[/font] [color=#008000]//Get AABBs of the two sprites sf::IntRect Object1AABB = GetAABB(Object1); sf::IntRect Object2AABB = GetAABB(Object2); sf::IntRect Intersection; [color=#0000FF]if (Object1AABB.Intersects(Object2AABB, &Intersection)) { [color=#008000]//We've got an intersection we need to process the pixels [color=#008000]//In that Rect. [color=#008000]//Bail out now if AlphaLimit = 0 [color=#0000FF]if (AlphaLimit == [color=#808000]0) [color=#0000FF]return true; [color=#008000]//There are a few hacks here, sometimes the TransformToLocal returns negative points [color=#008000]//Or Points outside the image. We need to check for these as they print to the error console [color=#008000]//which is slow, and then return black which registers as a hit. sf::IntRect O1SubRect = Object1.GetSubRect(); sf::IntRect O2SubRect = Object2.GetSubRect(); sf::Vector2i O1SubRectSize(O1SubRect.GetWidth(), O1SubRect.GetHeight()); sf::Vector2i O2SubRectSize(O2SubRect.GetWidth(), O2SubRect.GetHeight()); sf::Vector2f o1v; sf::Vector2f o2v; [color=#008000]//Loop through our pixels [color=#0000FF]for ([color=#0000FF]int i = Intersection.Left; i < Intersection.Right; i++) { [color=#0000FF]for ([color=#0000FF]int j = Intersection.Top; j < Intersection.Bottom; j++) { o1v = Object1.TransformToLocal(sf::Vector2f(i, j)); [color=#008000]//Creating Objects each loop :( o2v = Object2.TransformToLocal(sf::Vector2f(i, j)); [color=#008000]//Hack to make sure pixels fall withint the Sprite's Image [color=#0000FF]if (o1v.x > [color=#808000]0 && o1v.y > [color=#808000]0 && o2v.x > [color=#808000]0 && o2v.y > [color=#808000]0 && o1v.x < O1SubRectSize.x && o1v.y < O1SubRectSize.y && o2v.x < O2SubRectSize.x && o2v.y < O2SubRectSize.y) { [color=#008000]//If both sprites have opaque pixels at the same point we've got a hit [color=#0000FF]if ((Object1.GetPixel(static_cast<[color=#0000FF]int> (o1v.x), static_cast<[color=#0000FF]int> (o1v.y)).a > AlphaLimit) && (Object2.GetPixel(static_cast<[color=#0000FF]int> (o2v.x), static_cast<[color=#0000FF]int> (o2v.y)).a > AlphaLimit)) { [color=#0000FF]return true; } } } } [color=#0000FF]return false; } [color=#0000FF]return false;
[font=monospace]}[/font]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!