1

So, i decided to create a collision detection function in my game and i put it into the class of enemies. Then i put it to "enemy.cpp" and i used "this" pointer. The code:

if(((this->sprite.getPosition().y + this->sprite.getTextureRect().height) >= blocklist[i].sprite.getPosition().y) &&
(this->sprite.getPosition().y  <= blocklist[i].sprite.getPosition().y))

This gives me SIGSEGV segmentation error. I can't find what could be the problem in this line, here is the enemyclass for reference.

class enemyclass
{
public:
    sf::Sprite sprite;
    sf::Sprite bubble;
    //PROPERTIES
    float xspeed, yspeed;
    int x, y;
    float health;
    bool colR, colL, colU, colD;
    int skin;
    void detectCollisions(int blocksize);
};

and the blockclass that i made a vector of to use as a list of enemies:

class blockclass
{
public:
    sf::Sprite sprite;
    int x, y;
};

I would be very thankfull for an answer since i can't find out what's wrong.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • 2
    How do you *call* the function? – Some programmer dude Dec 19 '13 at 17:32
  • 2
    The implication to me is that either `this->sprite` or `blocklist[i].sprite` is a null pointer. – Scott Mermelstein Dec 19 '13 at 17:36
  • 2
    I would check that your value of `i` is always valid with respect to `blocklist`. – Turix Dec 19 '13 at 17:36
  • Try removing `this->` as it is not necessary in member functions. If you still have a problem, your issue lies elsewhere. – Thomas Matthews Dec 19 '13 at 17:39
  • 1
    @ThomasMatthews this may not be necessary, but it should never cause an error :-/. Whats happening here is as others have pointed out, either the pointer being used to call the class is not initialized, block list is not being accessed correctly, or something in sprite is not being initialized. See http://stackoverflow.com/questions/20409005/what-actually-happens-when-calling-a-getter-function-for-an-instance-variableth/20409363#20409363 – IdeaHat Dec 19 '13 at 17:47
  • What does getPosition() return? – pauluss86 Dec 19 '13 at 17:48
  • I call the function by "void playerclass::detectCollisions(int blocksize) {" – user3120147 Dec 19 '13 at 17:56
  • And getPosition().x will return a int, x position of sprite and ().y will do same for y position. – user3120147 Dec 19 '13 at 17:58
  • 1
    We need to see how you're *calling* detectCollisions. For example, if you did this: `enemyclass *e = NULL; e->detectCollisions(0);`, the `this` pointer would be NULL... – Taylor Brandstetter Dec 19 '13 at 18:05
  • 1
    for(int a = 0; a < enemysize; a++) { enemylist[a].detectCollisions(enemysize); } – user3120147 Dec 19 '13 at 18:13
  • After removing this-> it still gives me a SIGSEGV error. – user3120147 Dec 19 '13 at 18:51
  • Could it be that my collision detection function is in another file and to fix some problems i forward declared blocklist vector in that file, which is empty ? – user3120147 Dec 19 '13 at 18:53
  • This isn't the problem, but in an expression like `a + b >= c && d <= e` you don't need parentheses around the expressions on either side of the `&&`. – Pete Becker Dec 19 '13 at 21:19
  • It's not very easy to solve this with the given information. You should chop this statement up to see where the segfault comes from. For example, before this line, make a call to blocklist[i].sprite.getPosition() and see if you get a segfault on that line. – dodo Jan 07 '14 at 22:18

1 Answers1

0

As others have said, if some complicated instruction causes an error, split it into smaller parts and see, in which part the error arises:

if( this->x == 0) // changes nothing
    x = 0;        // but validates 'this'
int thisy = sprite.getPosition().y;
int thish = sprite.getTextureRect().height;
sf::Sprite &that = blocklist[i].sprite;
if( that.x == 0) // validate 'that'
    that.x = 0;
int thaty = that.getPosition().y;
if(thisy + thish >= thaty && thisy <= thaty) {
    // ......
}
CiaPan
  • 9,381
  • 2
  • 21
  • 35