Solved

How do I get objects to interact?

Posted on 1998-01-13
4
170 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
Comment Utility
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
Comment Utility
Ok so how do I query CTerain from CPerson?
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
Comment Utility
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
Comment Utility
Its certainly a good solution.
Thanks
Ian
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now