# Constructing min/max corners of a collision mesh

This topic is 1236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I'm having a bit of trouble with constructing the minimum and maximum corners of a collision mesh. I think it's best to explain what steps I'm going through:

1) load 3D mesh, assign a world matrix to it (translation, rotation), assign bounding box half extents
2) when a specific event happens in application, generate a 3D bounding box for the mesh using previously defined half extents. This is mainly for debugging as I'm filling the volume defined by this mesh with smaller cubes later. For rendering I'm  generating a cuboid mesh centered around (0,0,0) with the half extents and assign the same world matrix as the original mesh. Rendering this shows the mesh is in the correct place.
3) Now I need to check for collisions only on the xz-plane. To do this, I "construct" a cuboid from min/max corners and transform that to the position of the mesh. Now here is the problem though: I don't know how/where I'm going wrong but the min/max corners don't seem to be right. For example:
mesh half extents on xz (1.05, 0.5) -> constructed min = (-4, -3.5) and max = (-5, -1.45). How can min.x > max.x?

Here's the relevant code bit (broadCollisionMeshExtents == half extents):

D3DXMATRIX wMat = linkedObj.object->GetWorldMatrix();

D3DXVECTOR3 minimum = D3DXVECTOR3(-extents.x, 0.0f, -extents.y);
D3DXVECTOR3 maximum = D3DXVECTOR3(extents.x, 0.0f, extents.y);
D3DXVECTOR4 min4;
D3DXVec3Transform(&min4, &minimum, &wMat);
D3DXVECTOR4 max4;
D3DXVec3Transform(&max4, &maximum, &wMat);

minimum = D3DXVECTOR3(min4);
maximum = D3DXVECTOR3(max4);

This seems to work as long as the objects don't have any rotation on them. I can sort of force-fix it by doing:

float maxX = maximum.x;
float maxZ = maximum.z;
float minX = minimum.x;
float minZ = minimum.z;
maximum = D3DXVECTOR3(max(maxX, minX), 0.0f, max(maxZ, minZ));
minimum = D3DXVECTOR3(min(maxX, minX), 0.0f, min(maxZ, minZ));

... but that's just silly. I'm not entirely sure as to what is going on, any help would be greatly appreciated.

Edited by dr4cula

##### Share on other sites

General approach is to construct Axis Aligned Bounding Box, that gets transformed to world space if examination is needed. You compute the object space AABB only once, by a trivial aproach of finding minimum and maximum verticies on particular axises of object space. You are then left of transforming only 8 corners.

##### Share on other sites

How can min.x > max.x?

Rotation.

##### Share on other sites

General approach is to construct Axis Aligned Bounding Box, that gets transformed to world space if examination is needed. You compute the object space AABB only once, by a trivial aproach of finding minimum and maximum verticies on particular axises of object space. You are then left of transforming only 8 corners.

What exactly do you mean by trivial approach of finding min and max vertices? Not quite sure I'm following you...

How can min.x > max.x?

Rotation.

Hm... Is my rejection code then wrong?

auto InMesh = [minimum, maximum](const D3DXVECTOR3& cellCenter) -> bool {
if(cellCenter.x < minimum.x) return false;
if(cellCenter.x > maximum.x) return false;
if(cellCenter.z < minimum.z) return false;
if(cellCenter.z > maximum.z) return false;

return true;
};

CellCenter is a center point (in world space) of a quad on the xz-plane.

Thanks in advance once more.

Edited by dr4cula

##### Share on other sites

Apparently the world matrix you're using rotates the mesh. If you rotate x1 = -4 and x2 = -5 by some value, you shouldn't expect the values to remain the same. E.g., if x-values -4 and -5 are rotated 180 degrees about the y axis, they become +4 and +5 respectively. -4 is greater than -5, but +4 is less than +5. If you want mins and maxes to be world values, you have to evaluate them after they're placed in the world, not before.

##### Share on other sites

Apparently the world matrix you're using rotates the mesh. If you rotate x1 = -4 and x2 = -5 by some value, you shouldn't expect the values to remain the same. E.g., if x-values -4 and -5 are rotated 180 degrees about the y axis, they become +4 and +5 respectively. -4 is greater than -5, but +4 is less than +5. If you want mins and maxes to be world values, you have to evaluate them after they're placed in the world, not before.

'WorldMatrix' is concat of object matrix and world matrix but since world matrix is identity, then it is essentially an object matrix. How would I do these evaluations in world space then? I thought that my multiplication by the object matrix places it inside the world space - does it not? What I mean is, doesn't the multiplication put the min/max aligned with the mesh and centered around the mesh's origin?

Edited by dr4cula

##### Share on other sites

I thought that my multiplication by the object matrix places it inside the world space - does it not?

Yep, exactly. But that's after you've set the half-extent values, calling them min/max. Those min/max values describe the box in box space, not world space.

The code you posted before this comment:

but that's just silly.

is, in fact, evaluating the box after you've applied the rotation, as suggested, which is why that fixes the situation.

Think about the situation I mentioned in my previous post. If you rotate the box, the most negative (minimum) corner won't remain as the most negative. Just because you named it minimum, doesn't make it the minimum values. If you, for instance, just called the extent values "corners," and the minimum/maximum vectors "currentMin/Max," it might make more sense to you.

Edited by Buckeye

##### Share on other sites

snip

Ah right, I see what you mean now. Thanks a lot for your explanation! :)