Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I get objects to interact?

Posted on 1998-01-13
4
Medium Priority
?
180 Views
Last Modified: 2010-04-10
Hi, My question is...
I have 2 classes. CRock and CPerson. How do I detect when the person object hits a rock object?
(I would like a OO solution please)
Ive included the sample code to make things clearer.

#include <stdio.h>
#include <iostream.h">

class CPerson
{
private:
      int x,y;//location
public:
      CPerson()
      {
            x=0;
            y=0;
      }
      ~CPerson(){}
      void move(int nChar);
      void show();
};

void CPerson::show()
{
      printf("person[%d,%d]\n",x,y);
}

void CPerson::move(int nChar)
{
      int oldx=x;
      int oldy=y;
      switch(nChar)
      {
      case 1: x++;  break; //right
      case 2: x--;  break; //left
      case 3: y++; break; //down
      case 4: y--; break; //up
      default : cout<<"Unknown\n";
      }
      printf("[%d,%d]",x,y);
}

/////////////////////////////////////////////////////////
class CRock
{
private:
      int x,y; //location
public:
      CRock(int X, int Y):x(X),y(Y){}
      ~CRock(){}
      void show();
//      void move(int nChar);
};

void CRock::show()
{
      printf("rock[%d,%d]\n",x,y);
}

///////////////////////////////////////////////////////////
main()
{
      CRock rock(1,0);
      CPerson person;
      person.show();
      rock.show();


      person.move(1);

      return 0;
}
0
Comment
Question by:yakky
  • 2
4 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1178223
You need to have a CTerrain that knows about all the CRock instances.CPerson should query the CTerain about his new location when he moves. The CTerrain is either a singleton, or there is a global CTerrain object.

0
 

Author Comment

by:yakky
ID: 1178224
Ok so how do I query CTerain from CPerson?
0
 
LVL 32

Accepted Solution

by:
jhance earned 150 total points
ID: 1178225
I'd do it like this with a base class for the two objects and a class that can check for a collision:

#include <iostream.h>

class CThing
{
 protected:
  int x, y;

 public:
  CThing()
  {
   x=0;
   y=0;
  }

  ~CThing()
  {
  }

  virtual void move(int nChar)
  {
   switch(nChar){
    case 1: x++; break;
    case 2: x--; break;
    case 3: y++; break;
    case 4: y--; break;
    default: cout << "ERROR" << endl;
   }
   show();
  }

  virtual void show(void){
    cout << "thing: " << x << " : " << y << endl;
  }

  virtual bool collision(CThing &ouch){
   if(ouch.x == x && ouch.y == y){
    cout << "collision!" << endl;
    return true;
   }
   else{
    return false;
   }
  }
};

class CPerson : public CThing
{
 public:
   void show(void){
    cout << "person: " << x << " : " << y << endl;
   };
};

class CRock : public CThing
{
 public:
   void show(void){
    cout << "rock: " << x << " : " << y << endl;
   }
};

void main()
{
 CPerson person;
 CRock rock;

 person.show();
 rock.show();
 person.collision(rock);

 person.move(1);
 person.collision(rock);

 person.move(2);
 person.collision(rock);
}

0
 

Author Comment

by:yakky
ID: 1178226
Its certainly a good solution.
Thanks
Ian
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question