Skip to main content
gamedev.net
🔒 Locked

collision detection

Started by scottholland Apr 14, 2007 at 9:23 AM 0 replies 350+ views
Original Post
scottholland
scottholland
hi, im making a very simple space invader game using SDL and c++, im trying to detect if the bullet sprite overlaps with the invader sprite, and using a bool return true. Notting fancy just if the boxs in which images of the sprites are overlap then return true. The position of the Sprites are stored as Vector2D objects. My bullet Class and Invader class inherite from a general sprite class. The coordinates of the indivual sprites (x,y) are the top left corner of the box. (x1,y1) .|---------| |---------| |---------| |---------| |---------| |---------| (x2,y2) .|---------| |---------| |---------| |---------| |---------| |---------| i wrote a unction in the sprite class(which the bullet and invader inherite from), its called get Sprite::getBoundingbox() #include "Vector2D.h" #include "Invader.h" #include using namespace std; SDL_Rect Sprite::getBoundingbox() { SDL_Rect rect; rect.x = m_position.x; rect.y = m_position.y; rect.w = m_image ->w; rect.h = m_image ->h; return rect; } now i want to see if they overlap. this is where i need help. i wrote function in the sprite class called Sprite::checkCollision(Sprite a). its doesnt work and its not a bool. But i want to turn it into a bool that does work. void Sprite::checkCollision(Sprite a) { SDL_Rect rect1; SDL_Rect rect2; x1 = rect1.x; x2 = rect2.x; y1 = rect1.x; y2 = rect2.x; double distx; double disty; rect.x1 - rect.x2 = distx; if(distx < rect.w) { cout << " collied " << endl; } rect.y1 - rect.y2 = disty; if(disty < rect.h) { cout << " collided" << endl; } ok so in the main() .cpp file i have: Invader inv[50]; Bullet b[50]; I want to check if they overlap so i thought this might work. for( int i; i < 50; i++) { rect1 = b.getBoundingbox(); } for ( int i; i < 50 ; i++) { rect2 = inv.getBoundingbox(); } for (int i; < 50; i++) { b.checkCollision( inv); } for (int i; < 50; i++) { inv.checkCollision( b); } but it doesnt seem to. i know its long but any1 has any ideas it would be much apprecited because the project is due soon.thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.