Detect Stacked Objects

Topics: User Forum
Jan 29, 2008 at 1:50 PM
Hi there,

Can anybody suggest how to detect if geometryA is resting on top of geometryB?

I saw a post that suggested looking at the contactPoints between A and B, which I know how to do...But I'm not sure what math to use to say, "Ah, based on these contact points, I can deduce that A is resting on B!" If it helps, assume that A and B are just squares for now...

Any thoughts?
Jan 29, 2008 at 4:30 PM
Simple enough. Loop through the geometry verticies calling Collide() on the other geometry. I don't remember what information the Collide() method requries on hand (local or world point?) so if you don't respond by the time I get home, I'll look it up and post some code.

I suggest looping through the geometry with the fewest verticies.
Jan 29, 2008 at 5:21 PM
Hi, thanks for the quick answer but I should be more clear about what I mean: if A and B are (for example) square boxes, and A is sitting on top of B (i.e. pressing down from above on B), is there any way to tell? I mean specifically sitting on B as opposed to whether A is brushing up against B from the left or right side.

I was trying to do something like averaging the coords of the contact points, and then comparing the avg Y coordinate to A's lowest Y coordinate...But I think this is conceptually wrong and don't know the proper way to do it.

Thanks...

Jan 29, 2008 at 9:08 PM
Edited Jan 29, 2008 at 10:28 PM
If you have a contact point between 2 squares, one of which is aligned with the axes (not at an angle), then you can check the normal of the contact point (Contact.normal) to see if it is pointing up or down (or reasonably close). You could do this by dividing the normal.Y by the normal.X, thus giving the slope of the vector. If it was > 1, then the normal is closer to vertical than horizontal. Of course, if the objects are, in fact, resting one on top of the other, then the normal should be very close to vertical, and the slope should be much larger.

Then you can just check the position.Y of the two objects to see which is on top.

This method would work fine as long as the top body was colliding with one of the edges of the lower body, but if a corner of the lower body hits a face (or corner?) of the top body, the slope could be more difficult to interpret. I think the difficulty here depends on exactly what you mean by "resting on top of." Maybe you could check the position of the 2 squares to see if their X values were close enough to say that the top one was 'centered' on the top of the lower square.

Anyway, these are my ideas on this problem. There might be better ways of doing this.
Jan 31, 2008 at 9:26 AM
Edited Jan 31, 2008 at 9:31 AM
Upon collision, I'd check if A is positioned within a rect above B, rect size depending on A's aabb height and B's aabb width, if it is, I'd also check if the bodies linear and rotational velocities and are low enough, that is A is not moving much. perhaps on B aswell. perhaps monitor these values for some time, and if they are within limits, they are stacked.