• Advertisement
Sign in to follow this  

Classes, comparing data

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

Hi, im trying to add collisions between 2 classes, enemies and bullets, they are held in a vector but I am wondering, how would I go about checking collisions? (Well comparing their x&y variables)...

Share this post


Link to post
Share on other sites
Advertisement
You could override the == operator, have the bullet class as on side and the bad guy class as the other. In the override have it test the vectors and see if they are equal, if they are then a hit and it returns true...

theTroll

Share this post


Link to post
Share on other sites
Sorry not sure I understand, keep in mind you are talking to a C++ noob... so you may have to over explain things... ;)

Share this post


Link to post
Share on other sites
Do a search on operator overloading and c++, you will find tons on information, if you still have problems we can help ya out.

theTroll

Share this post


Link to post
Share on other sites
I would suggest that you do not overload an operator for collision detection. It is better to leave operators for intuitive operations. If I saw a test with enemy == bullet, it isn't obvious to me that you are performing collision detection. A better way would be to create a method in the Enemy class like canHit(Bullet) or isHit(Bullet), and perform the test there. This way your intention is clearer.

Share this post


Link to post
Share on other sites
Quote:
Original post by jjd
I would suggest that you do not overload an operator for collision detection. It is better to leave operators for intuitive operations. If I saw a test with enemy == bullet, it isn't obvious to me that you are performing collision detection. A better way would be to create a method in the Enemy class like canHit(Bullet) or isHit(Bullet), and perform the test there. This way your intention is clearer.


I second that, you should definitely, in my opinion, just make a function called Hit or isHit, or something like that. For example (pseudo-c++ code):


//this would be in the enemy class
bool isHit(bullet* projectile)
{
//this ptr used for clarity, not needed
if ( this->x == projectile->x && this->y == projectile->y )
{
return true; //collision
}
return false; //no collisions occured
}











with something like that, you could check collision for each bullet, against each enemy, by doing something like this:

//assuming a std::vector of enemies calles "enemies"
//and a std::vector of bullets called "bullets"
for (std::vector<enemy*>::iterator it = enemies.begin();
it != enemies.end();
++it )
{
for (std::vector<bullet*>::iterator blt = bullets.begin();
blt != bullets.end();
++blt )
{
(*it)->isHit(*blt); //check if bullet hit enemy
}

}







Share this post


Link to post
Share on other sites
Thanks for the replies, I am still having trouble getting my head around OOP, and I keep on getting countless errors, I tried the best to implement what you said...


#include <allegro.h>
#include <vector> // To use the vector class
#include <algorithm> // To use for_each
#include <functional> // To use mem_fun_ref

#include "siheader.h"
using namespace std;

void init();
void deinit();

int main() {
init();
vector<Ship> ships;
vector<bullet> bullets;
// Create a new ship and add it to the end of the vector
for (int x=50; x<750; x=x+35) {
for (int y=20; y<200; y=y+35) {
ships.push_back( Ship(x, y) );
}
}
//playervariables
int player_x=400;
int player_y=500;
BITMAP *playerImage;
playerImage = load_bitmap( "test.bmp", NULL);

BITMAP *backGround;
backGround = load_bitmap( "back.bmp", NULL);




while (!key[KEY_ESC]) {
acquire_screen();
draw_sprite(screen, backGround, 0,0);
if (key[KEY_RIGHT]) {
player_x=player_x+4;
}
if (key[KEY_LEFT]){
if (player_x > 0){
player_x=player_x-4;
}
}
if (key[KEY_SPACE]) {
bullets.push_back( bullet(player_x, player_y) );
}
for (int i = 0; i < ships.size(); i++) {
ships.update();
}
for (int i = 0; i < bullets.size(); i++) {
bullets.update();
bullets.isDestroyed();
}


for (std::vector<Ship*>::iterator it = ships.begin();
it != ships.end();
++it )
{
for (std::vector<bullet*>::iterator blt = bullets.begin();
blt != bullets.end();
++blt )
{
(*it)->isHit(*blt); //check if bullet hit enemy
}

}
//for_each(ships.begin(), ships.end(), mem_fun_ref(&Ship::update));
//for_each(bullets.begin(), bullets.end(), mem_fun_ref(&bullet::update));
//bullets.erase(remove_if(bullets.begin(), bullets.end(), mem_fun_ref(&bullet::isDestroyed)), bullets.end());
rotate_sprite(screen, playerImage, player_x+100, player_y-100, itofix(player_x));

draw_sprite(screen, playerImage, player_x, player_y);


textout_ex(screen, font, "BLAH", 10, 590,makecol(255, 255, 255), -1);
release_screen();
}

deinit();
return 0;
}
END_OF_MAIN()

void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}

install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}

void deinit() {
clear_keybuf();
/* add other deinitializations here */
}




And the header file...


class Ship {
private: // We don't want external code to access the coordinates directly
int x, y;
public: // We do want external code to be able to call these functions
// This creates a Ship object with the appropriate coordinates
Ship(int xpos,int ypos) :
x(xpos), // Sets the ship's x to xpos
y(ypos) // Sets the ship's y to ypos

{
// No code is currently required here.
// Things may change as your class grows.
}
void update() {
circlefill( screen, x, y, 15, makecol(255,255,255));
if (mouse_b & 1) {
y=y+1;
}
}
bool isHit(bullet* bullet) {
//this ptr used for clarity, not needed
if ( this->x == bullet->x && this->y == bullet->y ) {
return true; //collision
}
return false; //no collisions occured
}
};

class bullet {
private: // We don't want external code to access the coordinates directly
int x, y;
public: // We do want external code to be able to call these functions
// This creates a Ship object with the appropriate coordinates
bullet(int xpos,int ypos) :
x(xpos),
y(ypos)
{
// No code is currently required here.
// Things may change as your class grows.
}
bool isDestroyed() {
if (y<-10) {
return true;
}
return false;
}
void update() {
circlefill( screen, x, y, 15, makecol(255,255,255));
y=y-5;
//if (y<400) {
//x=0;
//y=0;
//}
}
};




How do I get it working? Ive been trying at this for a long time and if I always understand it better if I see a working example.

Share this post


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

  • Advertisement