when 2 sprites collide - real newb. :)

Started by
6 comments, last by Citizen666 11 years, 7 months ago
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.
:)
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.
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....

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

These examples from the education catalog are a good place to start, they should show you what you're looking for:

Rectangle collision

Per-Pixel collision

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.
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++)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Thanks for the replies all!!

Will now spend time trying to actually implement. :)

This topic is closed to new replies.

Advertisement