Jump to content

  • Log In with Google      Sign In   
  • Create Account


when 2 sprites collide - real newb. :)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Citizen666   Members   -  Reputation: 124

Like
0Likes
Like

Posted 25 September 2012 - 07:23 AM

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

Sponsor:

#2 JTippetts   Moderators   -  Reputation: 8408

Like
4Likes
Like

Posted 25 September 2012 - 07:29 AM

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.

#3 Citizen666   Members   -  Reputation: 124

Like
0Likes
Like

Posted 25 September 2012 - 07:41 AM

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, 25 September 2012 - 07:45 AM.


#4 BCullis   Crossbones+   -  Reputation: 1813

Like
1Likes
Like

Posted 25 September 2012 - 08:03 AM

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
DeviantArt :: Because right-brain needs love too

#5 Haps   Members   -  Reputation: 1315

Like
2Likes
Like

Posted 25 September 2012 - 08:23 AM

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

#6 Zolomon   Members   -  Reputation: 322

Like
3Likes
Like

Posted 25 September 2012 - 09:39 AM

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.

#7 superman3275   Crossbones+   -  Reputation: 2011

Like
0Likes
Like

Posted 25 September 2012 - 10:06 AM

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, 25 September 2012 - 10:07 AM.

I'm a game programmer and computer science ninja ph34r.png!

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 smile.png!


#8 Citizen666   Members   -  Reputation: 124

Like
0Likes
Like

Posted 27 September 2012 - 01:32 AM

Thanks for the replies all!!

Will now spend time trying to actually implement. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS