How do I write properly this function?

Hello everyone, first of all I want to thank you for trying to help me.
I was working on a simple project, trying to program something similar to the game Pong.
I defined these two classes (you don't need to pay too much attention to the constructor):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const int HEIGHT= 128, 
             WIDTH= 160;
class Settings
{
  friend class Game;
  int id,
      x,
      y,
      width,
      height;
  public:
  int sp;
}batl, batr, top, bot, ball;
class Game
{
  public:
  Game()
  {
   batl.height=HEIGHT/4;
   batl.width=WIDTH/32;
   batl.x=0;
   batl.y=HEIGHT/4;
   batl.id=3;
   
   batr=batl;
   batr.x=WIDTH-batr.width;
   batr.id=4;
   
   top.height=WIDTH/64;
   top.width=WIDTH;
   top.x=0;
   top.y=0;
   top.id=1
   
   bot=top;
   bot.y=HEIGHT-bot.height;
   bot.id=2
   
   ball.x=WIDTH/2;
   ball.y=HEIGHT/2;
   ball.width=WIDTH/10;
   ball.id=5;
  }
};

I have created the parameter ID for each element because I was going to create a bool function to understand when two different objects are touching or not.
Now that I have to write it I have a problem: knowing the ID of an object, can I know what object it belongs to and make the function work for it?
What I mean is: do I have to write this function like
1
2
3
4
5
6
7
8
9
10
11
12
bool contact (int id1, int id2)
{
 if (id1==batl.id && id2== top.id)
 {
   //some code
 }
else if (id1==batl.id && id2==bot.id)
 {
   // some code
 }
 //and so on
}

?
Or is there a better way to do it? Thank you for your attention!
Last edited on
Your question is not clear. What do you mean by "find the right variable"?
Sorry, my question should have been "How do I write properly this function?"
Last edited on
For your code:
What is top and bot?
Why is x and y set to 0 for top?
I also see some undefined values - for example where is batr.y?

You should make some fixed vector position (x,y) = (0,0) for example left bottom corner of your window and make all "pixel" coordinates (that you will ever define) relative to this position.


I would do something like this: (not actual code - just a hint)
Considering that everithing is some sort of rectangle (even the ball).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Obstacle
{
public:
   Obstacle(int x, int y, int width, int height); //Constructor
   //For comparison of collision - overload operator==
   //If colision occurs then True else False
   bool operator==(const Obstacle& secondObstacle);
private:
   Obstacle(); //Block default constructor from public access - not entirely needed
   int x; //you can measure distance in pixels so int is better than double - my opinion
   int y;
   int width;
   int height;
};

int main()
{
   Obstacle leftBar(int numbers for x, y, width, height);
   Obstacle rightBar(again 4 int numbers...);
   Obstacle ball(4 int numbers...);

   while(true){
      if(leftBar == ball) //Left bar is colliding with the ball
         //... Do stuff
      else if(rightBar == ball) //Right bar is colliding with the ball
         //... Do some other stuff
      else      //No collision
         //... Continue game or check for game exit and break infinite while loop
   }

    //You could also check (leftBar == rightBar) but that case should never happen
    //At least in classic Pong :-)
}


Then content of overloaded operator== would be something like this:
(different bounds may be set by different <, >, <=, >=; it's all up to your preferences)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool Obstacle::operator==(const Obstacle& secondObstacle)
{
//"this" means left hand side of operator ==. So "a==b" meand that "this" is "a"
//Probably you don't even need to write words "this" in the first time
   if (this.x              < secondObstacle.x &&
      (this.x+this.width)  > secondObstacle.x && //X coordinate checked
      this.y               < secondObstacle.y &&
      (this.y+this.height) > secondObstacle.y) //Y coordinate checked
         return true; //We have collision
//Now we checked secondObstacle corned at coordinates (X,Y)

   else if(this.x < (secondObstacle.x + secondObstacle.width) &&
             //... Other checks for other corners of secondObstacle
             //... I don't really want to write it all
            //... We have corners secondObject-> (x,y), (x+width,y), (x, y+height), (x+w, y+h)

    else
          return false; //No collision
}


You may also include several checks if for example is one object Obstacle inside the other one. "secondObstacle" can be bigger that "this" and the code I posted only checks if some corner of "secondObstacle" is inside of "this" object (left hand side argument of ==).

I hope I didn't make it too confusing. If you didn't get some part from this code snippet just ask.
Okay, I think I got it even though it don't know very well how to use the
this
pointer and operators, but I will try!
By the way I just realized I haven't said it before, but I had to say that I was making this program for an Arduino connected to a small display (160x128) so yes, there actually is a fixed vector and measures are in pixel! Thanks anyway for your help!
this isn't even necessary in that bit of code. The only reason I think think to use it is if you wish to return a pointer to the current object or you have two variables with the same name.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class SomeClass
{
    int x;
    public:
        SomeClass returnObject(void);
        void setX(int x); //same name as private
};

SomeClass SomeClass::returnObject(void)
{
    return *this;
}

void SomeClass::setX(int x)
{
    this->x = x;
}



BTW vlkon this is a pointer to the current object so you either need *this.something or this->something Though in your case there is no point.
Last edited on
@giblit: Thank you for a correction. I didn't pay too much attention when I wrote this code - it was only for a demonstration and I wanted to somehow distinguish secondObstacle (right side of ==) from the left side (this).
Other way would be to overload it as a friend function with Obstacle& lhs and Obstacle& rhs as arguments.

@Shishka: If you remove all this from my example you should still be fine as giblit said.
Topic archived. No new replies allowed.