Permalink
Browse files

Sprite-to-Sprite Collision detection

  • Loading branch information...
1 parent d5516da commit 00eb9ef719791598c90fc0ece2599f91e0dbce04 @mallardtheduck mallardtheduck committed Jan 28, 2012
Showing with 68 additions and 4 deletions.
  1. +4 −0 src/Revival/SpriteBase.cpp
  2. +1 −0 src/Revival/SpriteBase.h
  3. +56 −0 src/Revival/Universe.cpp
  4. +7 −4 src/Revival/Universe.h
View
4 src/Revival/SpriteBase.cpp
@@ -23,4 +23,8 @@ bool SpriteBase::IsKeyDown(SDLKey key){
CoreColor SpriteBase::GetPixel(CorePosition pos){
return gEngine->getPixelColor(texture->GetSurface(), pos.GetX(), pos.GetY());
+}
+
+bool SpriteBase::GetVisible(){
+ return _visible;
}
View
1 src/Revival/SpriteBase.h
@@ -33,4 +33,5 @@ class SpriteBase{
CorePosition GetPosition() {return _pos;}
CoreSize GetSize() {return _size;}
+ bool GetVisible();
};
View
56 src/Revival/Universe.cpp
@@ -3,6 +3,37 @@
#include "TestMap.h"
#include "Player.h"
+SDL_bool SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
+{
+ int Amin, Amax, Bmin, Bmax;
+
+ /* Horizontal intersection */
+ Amin = A->x;
+ Amax = Amin + A->w;
+ Bmin = B->x;
+ Bmax = Bmin + B->w;
+ if (Bmin > Amin)
+ Amin = Bmin;
+ if (Bmax < Amax)
+ Amax = Bmax;
+ if (Amax <= Amin)
+ return SDL_FALSE;
+
+ /* Vertical intersection */
+ Amin = A->y;
+ Amax = Amin + A->h;
+ Bmin = B->y;
+ Bmax = Bmin + B->h;
+ if (Bmin > Amin)
+ Amin = Bmin;
+ if (Bmax < Amax)
+ Amax = Bmax;
+ if (Amax <= Amin)
+ return SDL_FALSE;
+
+ return SDL_TRUE;
+}
+
Universe* Universe::pinstance = 0;
Universe* Universe::Instance () {
if(pinstance == 0) { pinstance = new Universe; pinstance->Init(); }
@@ -57,4 +88,29 @@ void Universe::KeyUp(SDLKey key){
bool Universe::IsKeyDown(SDLKey key){
return _keys.count(key);
+}
+
+SpriteSet Universe::CollisionDetect(CorePosition pos, CoreSize size, SpriteBase *ignore){
+ SpriteSet ret;
+ SDL_Rect priRect;
+ priRect.x=pos.GetX();
+ priRect.y=pos.GetY();
+ priRect.w=size.GetWidth();
+ priRect.h=size.GetHeight();
+ for(SpriteItr i=_sprites.begin();i!=_sprites.end();++i){
+ if(*i!=ignore){
+ SpriteBase *sprite=*i;
+ if(sprite->GetVisible()){
+ SDL_Rect secRect;
+ secRect.x=sprite->GetPosition().GetX();
+ secRect.y=sprite->GetPosition().GetY();
+ secRect.w=sprite->GetSize().GetWidth();
+ secRect.h=sprite->GetSize().GetHeight();
+ if(SDL_HasIntersection(&priRect, &secRect)){
+ ret.insert(sprite);
+ }
+ }
+ }
+ }
+ return ret;
}
View
11 src/Revival/Universe.h
@@ -6,13 +6,20 @@
class CoreGraphics;
+typedef std::set<SpriteBase*> SpriteSet;
+typedef SpriteSet::iterator SpriteItr;
+typedef std::set<SDLKey> KeySet;
+
class Universe {
public:
static Universe* Instance();
void Update(float fTime);
void Draw();
void AddSprite(SpriteBase *sprite);
+ void RemoveAndDeleteSprite(SpriteBase *sprite);
+ void RemoveAllSprites(bool preserverPlayer=false);
+ SpriteSet Universe::CollisionDetect(CorePosition pos, CoreSize box, SpriteBase *ignore=NULL);
void KeyDown(SDLKey key);
void KeyUp(SDLKey key);
@@ -28,10 +35,6 @@ class Universe {
Universe(const Universe&);
Universe& operator=(const Universe&);
private:
- typedef std::set<SpriteBase*> SpriteSet;
- typedef SpriteSet::iterator SpriteItr;
- typedef std::set<SDLKey> KeySet;
-
static Universe* pinstance;
void Init();
float rotation;

0 comments on commit 00eb9ef

Please sign in to comment.