Determine if an object coincide with other objects

I have a question:

How can you determine if an objects coincides with other objects?

My solution is this:

I stored the xy positions of the other objects in an array.
If the next xy positions of the object created is equal to xy positions of the other objects then add the current xy positions of the objects to the xy positions of the other objects.
then create another object determine if xy positions coincides and so on..

In java programming language the solution is simply:

if object.instanceof(oldObjects)
createNewObject();
just like this...

so what is the equivalent of instanceof() method of java in C++?

or is there a more simple solution to this algo?

Thanks.
http://www.cplusplus.com/reference/std/typeinfo/type_info/
BTW arrays can only be of homogeneous types
what do you mean by homogeneous types?

I will store the xy positions in an array.
so typeinfo is not applicable?

to be specific..

i will do a tetris game..

I want to know if the current object is possible to move or rotate.

Thanks.
what do you mean by homogeneous types?
They are all of the same type, so you would know what the type is.
You may not know it only when you are using an array of pointers to a base class.

to be specific..

i will do a tetris game..

I want to know if the current object is possible to move or rotate.
How does your object look like?
How does the type of the object affect its ability to move?
My objects look like a normal tetris block, L shape, I shape, S shape, Z shape and T shape.

I have a set of xy coordinates...

What I am going to put there are blocks of object with same type like LShape type.
So basically I would create 4 objects of same type to four xy coordinates.
With the four present objects let say type LShape, I would like to know if these four objects would intercept or coincide with other objects of different type or same type.
If it coincides, the movement is not allowed.

So for example:

The coordinates are (3,4),(4,4),(5,4),(5,5)
note: this is an Lshape block
If I move it to the left the coordinates would be
(2,4),(3,4),(4,4),(4,5) respectively.
Now what I am going to do is to check if these coordinates already have an objects of same or different type...

1
2
3
  if(typeid(/*get type of object at coordinate (2,4)*/)!=typeid(/*presentblock*/) &&
     (typeid(/*get type object at coordinate (2,4)*/)==typeid(/*presentblock*/)))
      movement = false; //not possible 

Thanks. I am thinking now an algorithm in getting the type of object at a given coordinates..
Is there a more simple way to do this? get typeid at location x,y??
Edit: by the way I am doing this in console window
Last edited on
Can I see how your array looks like?

BTW:

You wrote:
1
2
3
if(typeid(/*get type of object at coordinate (2,4)*/)!=typeid(/*presentblock*/) &&
     (typeid(/*get type object at coordinate (2,4)*/)==typeid(/*presentblock*/)))
      movement = false; //not possible 
Which translates into:
1
2
3
4
a = typeid(/*get type of object at coordinate (2,4)*/);
b = typeid(/*presentblock*/);
if ( a != b && a == b )
   movement = false;
How can this ever be true?
oops sorry its not && it must be ||

my array looks like this

1
2
3
4
5
		int lBlock[4][4][3] = //assigns the xy positions of four blocks, 10 is green
			{{{x-1,y,10},{x,y,10},{x+1,y,10},{x+1,y+1,10}},//stateL = 0 default position ™™|
			{{x,y-1,10},{x,y,10},{x,y+1,10},{x-1,y+1,10}},//stateL = 1  
			{{x+1,y,10},{x,y,10},{x-1,y,10},{x-1,y-1,10}},//stateL = 2  
			{{x,y+1,10},{x,y,10},{x,y-1,10},{x+1,y-1,10}}};//stateL = 3  

That code is for shape L.
BTW I already made an alternate algorithm for the coinciding of the block.

my problem now is if any horizontal line is completed, it will be deleted and the blocks above it will go down.

Thanks.
I still don't understand why you want to compare the objects types. If they are all int arrays they would have the same type.


oops sorry its not && it must be ||
1
2
3
4
a = typeid(/*get type of object at coordinate (2,4)*/);
b = typeid(/*presentblock*/);
if ( a != b || a == b )
   always true
Hmmm.... Well actually, its always true....

but I must state that if the location of the block is not occupied
1
2
3
4
if(xyloc == 0)//0 means no objects found at the specified xy position
    movement = true;
else
    movement = false; 


Actually, I don't need to use typeid...

I was thinking if I can use typeid for future use...

by the way I can change my definition to this so that the object types are different

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
		int lBlock[4][4][3] = //assigns the xy positions of four blocks, 10 is green
			{{{x-1,y,10},{x,y,10},{x+1,y,10},{x+1,y+1,10}},//stateL = 0 default position ™™|
			{{x,y-1,10},{x,y,10},{x,y+1,10},{x-1,y+1,10}},//stateL = 1  
			{{x+1,y,10},{x,y,10},{x-1,y,10},{x-1,y-1,10}},//stateL = 2  
			{{x,y+1,10},{x,y,10},{x,y-1,10},{x+1,y-1,10}}};//stateL = 3
//different type
		unsigned int tBlock[4][4][3] = //assigns the xy positions of four blocks, 10 is green
			{{{x-1,y,10},{x,y,10},{x+1,y,10},{x+1,y+1,10}},//stateL = 0 default position ™™|
			{{x,y-1,10},{x,y,10},{x,y+1,10},{x-1,y+1,10}},//stateL = 1  
			{{x+1,y,10},{x,y,10},{x-1,y,10},{x-1,y-1,10}},//stateL = 2  
			{{x,y+1,10},{x,y,10},{x,y-1,10},{x+1,y-1,10}}};//stateL = 3

		short zBlock[4][4][3] = //assigns the xy positions of four blocks, 10 is green
			{{{x-1,y,10},{x,y,10},{x+1,y,10},{x+1,y+1,10}},//stateL = 0 default position ™™|
			{{x,y-1,10},{x,y,10},{x,y+1,10},{x-1,y+1,10}},//stateL = 1  
			{{x+1,y,10},{x,y,10},{x-1,y,10},{x-1,y-1,10}},//stateL = 2  
			{{x,y+1,10},{x,y,10},{x,y-1,10},{x+1,y-1,10}}};//stateL = 3 

This solves the problem right? now I can use typeid?

By the way...

I already finished my Tetris program...
Thanks a lot Bazzy, you're a big help.
Topic archived. No new replies allowed.