Solved

How do I get objects to interact?

Posted on 1998-01-13
4
176 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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