Jump to content
  • Advertisement
Sign in to follow this  
wazup92324

collision detection using list of objects in SDL

This topic is 4410 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

hello everyone, I’m having trouble with collision detection and finding a way to detect if a bullet is colliding with an enemy. The bullet object is pushed into a list. I created a SDL_Rect around the bullet and Enemy_A. And I am having trouble with it detecting the collision with Enemy_A and Bullet. Here is code of the bullet class: class Bullet { private: //Offsets of bullet int x, y; //The collisions of the box of the dot std::vector<SDL_Rect> box; //The const velocity of Bullet int yVel; //Move the boxes relative to the postion of the box void shift_boxes(); public: Bullet(); void set_xy(int player_x, int player_y); void show(); void move(std::vector<SDL_Rect>&rects); bool is_dead(); std::vector<SDL_Rect> &get_rects(); }; //Bullet Implementation Bullet::Bullet() { //Offsets for bullet x = 10; y = 9930; //Velocity for bullet yVel = -10; //Create the nesasarry SDL_Rects box.resize(1); //Init the collision boxes with the width and height box[0].w = 3; box[0].h = 5; shift_boxes(); } void Bullet::move(std::vector<SDL_Rect> &rects) { //Move the bullet y += yVel; //Move the rectangles shift_boxes(); } void Bullet::set_xy(int player_x, int player_y) { x = player_x; y = player_y; } void Bullet::show() { apply_surface(x - camera.x, y - camera.y, bullet, screen); } bool Bullet::is_dead() { if(y == 0) { return true; } else return false; } void Bullet::shift_boxes() { //The row offset int r = 0; //The set int set = 0; //Loop through all the rects for(set = 0; set<box.size();set++) { //Center the collision box box[set].x = x + (BULLET_WIDTH - box[set].w)/2; //Set the collision box at its row offset box[set].y = y + r; //Move the row offset down r += box[set].h; } } std::vector<SDL_Rect>&Bullet::get_rects() { //return the collision boxes return box; } here is the enemy class: class Enemy_A { private: //x and y offsets int x, y; //velocity of x and y int xVel; int yVel; //The collisions box of the player std::vector<SDL_Rect> box; //Moves the boxes relative to the postion of the box void shift_boxes(); public: //Constructor Enemy_A(); //Move the enemy void move(std::vector<SDL_Rect> &rects); //Show the enemy on the screen void show(); //Show the enemy dead body void show_body(); //Get the rectangles for the vector std::vector<SDL_Rect>&get_rects(); }; //Enemy_A implementations //New Enemy_A constructor Enemy_A::Enemy_A() { //init the offsets and dims x = 0; y = 9900;; //int the velocity xVel = 0; yVel = 0; //Create the nessasary SDL_Rects box.resize(4); //Init the collison box rects box[0].w = 3; box[0].h = 1; box[1].w = 5; box[1].h = 1; box[2].w = 7; box[2].h = 1; box[3].w = 9; box[3].h = 20; //move the collison boxes to their proper spot shift_boxes(); } //Enemy_A move() function void Enemy_A::move(std::vector<SDL_Rect> &rects) { //Move the dot left or right x += xVel; shift_boxes(); //If the dot went too far to the left or right or has collided with the other shapes if( ( x < 0 ) || ( x + ENEMY_A_WIDTH > SCREEN_WIDTH )) { //Move back x -= xVel; shift_boxes(); } //Move the dot up or down y += yVel; shift_boxes(); //If the dot went too far up or down or has collided with the other shapes if( ( y < 0 ) || ( y + ENEMY_A_HEIGHT > SCREEN_HEIGHT )) { //Move back y -= yVel; shift_boxes(); } } //Enemy_A show(); function void Enemy_A::show() { //show the Dot apply_surface(x - camera.x, y - camera.y, enemy_A, screen); } //Enemy_A show body function void Enemy_A::show_body() { apply_surface(x - camera.x, y - camera.y, deadBody, screen); } void Enemy_A::shift_boxes() { //The row offset int r = 0; //The set of rects int set = 0; //loop through all rects for(set = 0; set < box.size(); set++) { //Center the collision boxes box[set].x = x + (PLAYER_WIDTH - box[set].w)/2; //Set the collision box at its row offset box[set].y = y + r; //Move the row offset r += box[set].h; } } std::vector<SDL_Rect>&Enemy_A::get_rects() { //return the collision boxes return box; } Here is the game level //****************************************************************** //****************************************************************** //* NEW GAME FUNCTION * //****************************************************************** //****************************************************************** int new_game() { //Start the music Mix_PlayMusic(level_1M, -1); //Make the timer Timer myTimer; //Start the timer myTimer.start(); //keeps track of current bullet on screen int bullet_num = 0; //Game Over flag bool game_over = false; //Create all game objects Level1 level1; Player newPlayer; Enemy_A newEnemy; std::list<Bullet> activeBullets; //Create animations for sprites set_playerClips(); //Frame rate cap timer Timer fps; //While the user hasn't quit while( quit == false ) { fps.start(); while(SDL_PollEvent (&event)) { newPlayer.handle_input(); newPlayer.set_life(30); //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //******************************************* //* Display level1 * //******************************************* level1.show(); newPlayer.move(newPlayer.get_rects()); newPlayer.set_camera(); newPlayer.show(); newEnemy.show(); newEnemy.move(newEnemy.get_rects()); if(check_collision(newPlayer.get_rects(),newEnemy.get_rects())) { newEnemy.show_body(); } //Get the keystates Uint8 *keystates = SDL_GetKeyState(NULL); //If spacebar is pressed shoot bullet if(keystates[SDLK_SPACE]) { Bullet newBullet; newBullet.set_xy(newPlayer.get_x(), newPlayer.get_y()); activeBullets.push_back(newBullet); } std::list<Bullet>::iterator iBullet = activeBullets.begin(); while(iBullet != activeBullets.end()) { if(iBullet->is_dead()) { iBullet = activeBullets.erase(iBullet); } else { iBullet->show(); iBullet->move(iBullet->get_rects()); ++iBullet; } } //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //Cap the frame rate with fps while(fps.get_ticks() < 1000 / FRAMES_PER_SECOND) { //wait... } } return 0; }

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!