Solved

How do I get objects to interact?

Posted on 1998-01-13
4
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Industry Leaders: 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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