• Advertisement
Sign in to follow this  

when 2 sprites collide - real newb. :)

This topic is 2038 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 all.

I'm trying to do something very simple as part of my learning XNA and am making a hash of it as usual. :)

Any help would be greatly appreciated.

So I've got a player controlled sprite that can move around the screen using the KB. All good.
I've now drawn a second stationary sprite on the right hand side of the screen.

What I'm trying to do is display a text message when the player controlled sprite moves over the stationary sprite.

I'm fine with the actual drawing of the text (I think) I just need to work out how to register that moveable Sprite A has indeed moved over stationary sprite B.

Thanks in advance,

Gigi.
:)

Share this post


Link to post
Share on other sites
Advertisement
After reading your title, I now have that Powerman 5000 song When Worlds Collide in my head.

You need to first check for intersection between the bounding boxes of A and B. Each sprite can be envisioned to be enclosed by a box, that it fits tightly inside. Each time a sprite moves, you can use an algorithm such as the separating axis test to determine if the boxes overlap, and if they do then you can return a true value and in your main loop if this true value is returned you can display the text.

Share this post


Link to post
Share on other sites
yeh, I kind of understand the principle and logic behind the test etc - I just don't know the commands and the correct syntax to use.... Edited by Citizen666

Share this post


Link to post
Share on other sites

I just don't know the commands and the correct syntax to use....

I'll try to not read that as "please write my code for me" ;p

There isn't a specific command. You implement the algorithm in c#. The separating axis theorem is pretty heavy for what you're talking about though if you're still in an early learning stage. XNA has built in "Bounding_______" structures, you probably just want to create a BoundingBox for each sprite, and then use the built-in BoundingBox.Intersects() method to check for collision/overlap. This can result in some wierd effects if the sprite has a detailed or convex shape that leaves a lot of space in the BoundingBox, but it's a start, and the size of the box can always be tweaked.

Just remember to update the bounding boxes' locations in addition to the sprites' locations when you do your update logic.

Share this post


Link to post
Share on other sites

After reading your title, I now have that Powerman 5000 song When Worlds Collide in my head.


Sorry, just have to share that I also got the song in my head immediately after reading the title.

Share this post


Link to post
Share on other sites
Well, in SFML you set floatrects (Rectangles that can have coordinates that are floating point values) up around the sprite.
Something along the lines of:

sf::floatrect floatrect1;
sf::floatrect floatrect2;
//
// SET FLOATRECTS COORDS TO SPRITES
//
if (floatrect1.Intersects(floatrect2))
{
//COLLISION
}

if there isn't an intersect function for bounding boxes, write your own using the coordinates of the floatrects.
(I didn't write down the specific code because I didn't have time, also you might not know SFML/C++) Edited by superman3275

Share this post


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

  • Advertisement