CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 64
  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Compare 2 objects in 2 different classes

    i got 3 classes and i want to compare zombie class x and y with man class x and y

    what would be the best way to do it? because i want to do a kinda collision check when zombie will collide with the man.

    thanks

    here is my code:

    man.h
    Code:
    #pragma once
    #include"RandomNumber.h"
    
    class Man
    {
    private:
    	int x;
    	int y;
    public:
    	Man(void);
    	~Man(void);
    
    	void set_x(int);
    	void set_y(int);
    
    	void movement(void);
    	void show_position(void);
    };
    man.cpp
    Code:
    #include<iostream>
    #include<time.h>
    #include<stdlib.h>
    #include"RandomNumber.h"
    #include"console.h"
    #include"Man.h"
    
    
    Man::Man(void)
    {
    	int x = 0;
    	int y = 0;
    }
    
    void Man::set_x(int X)
    {
    	x = X;
    }
    
    void Man::set_y(int Y)
    {
    	y = Y;
    }
    
    void Man::movement(void)
    {
    	int move = 0;
    
    	std::cin>> move;
    
    	if(move == 8)
    	{
    		y = y - 1;
    	}
    	else if(move == 2)
    	{
    		y = y + 1;
    	}
    	else if(move == 6)
    	{
    		x = x + 1;
    	}
    	else
    	{
    		x = x - 1;
    	}
    }
    
    void Man::show_position(void)
    {
    	gotoxy(x,y);
    	{
    		std::cout<< "M";
    	}
    }
    
    Man::~Man(void)
    {
    }
    zombie.h
    Code:
    #pragma once
    #include"RandomNumber.h"
    
    class Zombie
    {
    private:
    	int x;
    	int y;
    	RandomNumber Random;
    
    public:
    
    	Zombie(void);
    	~Zombie(void);
    
    	void set_x(int);
    	void set_y(int);
    
    	int get_rand(int);
    	void movement(void);
    	void show_position(void);
    };
    zombie.cpp
    Code:
    #include<iostream>
    #include<time.h>
    #include<stdlib.h>
    #include"RandomNumber.h"
    #include"console.h"
    #include"Zombie.h"
    
    
    Zombie::Zombie(void)
    {
    	int x = 0;
    	int y = 0;
    }
    
    void Zombie::set_x(int X)
    {
    	x = X;
    }
    
    void Zombie::set_y(int Y)
    {
    	y = Y;
    }
    
    int Zombie::get_rand(int upper)
    {
    	return Random.rand_gen(upper);
    }
    
    void Zombie::movement(void)
    {
    	int choice;
    	choice = get_rand(4);
    
    	if(choice == 0)
    	{
    		y = y - 1;
    	}
    	else if(choice == 1)
    	{
    		y = y + 1;
    	}
    	else if(choice == 2)
    	{
    		x = x + 1;
    	}
    	else
    	{
    		x = x - 1;
    	}
    }
    
    void Zombie::show_position(void)
    {
    	gotoxy(x,y);
    	{
    		std::cout<< "Z";
    	}
    }
    
    Zombie::~Zombie(void)
    {
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Compare 2 objects in 2 different classes

    I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.

  3. #3
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by Lindley View Post
    I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.
    so i should just make a new class which would deal with coordinates? it would have 2 variables x and y.
    Would i then need my other two classes for the objects? because i will need 3 objects, man; and multiple instances of zombie and a hole.

  4. #4
    Join Date
    Aug 2007
    Posts
    135

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by Lindley View Post
    I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.
    I dont think this is good object oriented programming. Allow me to explain why i feel so?
    Well, a co ordinate does not define a person object. So it does not belong there. The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.

    Would like to hear your views on the same.

  5. #5
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by ChessMaster View Post
    I dont think this is good object oriented programming. Allow me to explain why i feel so?
    Well, a co ordinate does not define a person object. So it does not belong there. The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.

    Would like to hear your views on the same.
    i think i understand you.

    i am making a simple game, with a man (which you control) zombies which move randomly and holes which both man and zombies must avoid.

    and the only thing i could come up with was what you saw above, i must agree i am quite bad at planning. I would really like some kind advice "/

  6. #6
    Join Date
    Aug 2007
    Posts
    135

    Re: Compare 2 objects in 2 different classes

    From what I have read about your problem.....
    Well, you need a man cclass & a zombie class to do what they are supposed to do. If the men and zombies are in a room.....you can have an initial point and then positions where each one is. Question now is, who will keep track of the positions of the men & zombies. There is another class which is probably giving instructions to each of these objects. that class should probably keep track of who is where.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by ChessMaster
    I dont think this is good object oriented programming. Allow me to explain why i feel so?
    Well, a co ordinate does not define a person object. So it does not belong there.
    That depends on your point of view. You could say that a Man or Zombie has-a Coordinate, or you can say that a Map/Grid has-a Man or Zombie.

    Quote Originally Posted by ChessMaster
    The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.
    I do not see how this substantiates an objection to Lindley's suggestion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by laserlight View Post
    That depends on your point of view. You could say that a Man or Zombie has-a Coordinate, or you can say that a Map/Grid has-a Man or Zombie.
    You could even say both at the same time, although keeping the two notions coordinated would be a bit of extra complexity.

    The point is, these two things are equivalent:

    Code:
    struct Man
    {
        int x, y;
    };
    
    struct Man
    {
        Point position;
    }
    except that the latter better encapsulates the notion of a position in a single object rather than multiple ints, and is thus (in my view) a better design.

  9. #9
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by ChessMaster View Post
    From what I have read about your problem.....
    Well, you need a man cclass & a zombie class to do what they are supposed to do. If the men and zombies are in a room.....you can have an initial point and then positions where each one is. Question now is, who will keep track of the positions of the men & zombies. There is another class which is probably giving instructions to each of these objects. that class should probably keep track of who is where.
    so my code so far is ok? because i got random movement of zombie working just fine, using a loop in my main and the movement of the man "/

    and your saying i should have a controller class?

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Compare 2 objects in 2 different classes

    Using some kind of controller class is one option. It's certainly not the only one.

  11. #11
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by Lindley View Post
    Using some kind of controller class is one option. It's certainly not the only one.
    hmmm i see.

    I need to find the best way to check the collision between them all.

  12. #12
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    can anyone help me or give me some suggestions how to approach this problem???

  13. #13
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Compare 2 objects in 2 different classes

    so does anyone know how would i compare the x in zombie class with x in the man class?

  14. #14
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Compare 2 objects in 2 different classes

    I'd say that both Man's and Zombie's are Body's and that a Body has-a position :

    Code:
    class Body
    {
    public:
    	bool CollidesWith( const Body& ) const;
    
    protected:
    	// "move" functions ...
    
    private:
    	int x_;
    	int y_;
    };
    
    class Man: public Body { /*...*/ };
    class Zombie: public Body { /*...*/ };
    
    int main()
    {
    	Man 	a_man;
    	Zombie	a_zombie;
    
    	if( a_man.CollidesWith( a_zombie ) )
    	{
    		// a_zombie.Eats( a_man );
    	}
    }

  15. #15
    Join Date
    May 2009
    Posts
    2,413

    Re: Compare 2 objects in 2 different classes

    Quote Originally Posted by Mariusmssj View Post
    so does anyone know how would i compare the x in zombie class with x in the man class?
    The simplest way is to expose the x and y coordinates of both Man and Zombie via two accessor methods (like get_x and get_y).

    You then write a free function called collisionCheck, like
    Code:
    bool collisionCheck(const Man& man, const Zombie& zombie) {
       return man.get_x()==zombie.get_x() && man.get_y()==zombie.get_y();
    }

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured