# Is there a 3D object intersection math library that is not a part of a physics framework?

## Recommended Posts

Posted (edited)
22 minutes ago, Zakwayda said:

If you're not using the geometric centers for your boxes but the test still seems to be working, I suspect it's only working incidentally, and that given different input you'd find that the results are not in fact correct. I can't be more specific without knowing more about the circumstances, but just because your code seems to be working under certain circumstances, I wouldn't assume that it's actually correct.﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿

Let me just rephrase what I understood it to be. You're saying that in your example mCenter was not meant to be passed into the SAT algorithm, it was just a reference point that you chose for the box. You're saying that the true geometric center has to be passed into the SAT. I think that is what I was confused about, because I thought that mCenter in your example was one of the parameters I needed to pass into the algorithm, whereas it was just a reference point that you chose for the box that just happen to lie between its edges and which I can change to lie anywhere on the box or outside of it if I choose so?

In other words I should always be passing

XMVECTOR mCenter = point0 + xAxis*xExtent + yAxis*yExtent + zAxis*zExtent;

into the algorithm and have another variable, that I can define to be anywhere I like

XMFLOAT3 mReferencePoint = point0; //making one corner of the box the reference point

that I use in order to move the box etc.

Edited by VanillaSnake21

##### Share on other sites
Posted (edited)
Quote

I think that is what I was confused about, because I thought that mCenter in your example was one of the parameters I needed to pass into the algorithm, whereas it was just a reference point that you chose for the box that just happen to lie between its edges and which I can change to lie anywhere on the box or outside of it if I choose so.

Yeah, we're definitely confused and talking past each other I don't think there was an 'mCenter' in any of my examples, so I think we've probably gotten a little off track.

It may be that the confusion is about my use of the term 'reference points'. By 'reference points' I mean the two points you pass in to define the box - 'xzStart' and 'xzEnd' in your original example.

For clarity, let's just start over. An oriented box is [edit: in the context of this discussion at least] represented by the following parameters: center, 3 axes, and 3 extents. That's what you need for the version of the SAT you're using.

You're building your boxes using two reference points (called 'xzStart' and 'xzEnd' in the first example you posted). From your drawings, it's clear that you want these points to correspond to two 'ground-level' corners lying along an edge.

Given that, here's how to compute the center, axes, and extents (this is the same as what I posted earlier, with minor adjustments to account for the reference points lying at the corners):

p0 = your 'xzStart'
p1 = your 'xzEnd'

// Vector from one endpoint to the other.
vector3 diff = p1 - p0;

// Length of box.
float length = diff.length();

// Y axis is always the world y axis.
vector3 yAxis(0, 1, 0);

// Normalize to get an x axis for the box (or z - you can do it either way).
vector3 xAxis = diff.normalized();

// Use cross product to get z axis.
vector3 zAxis = cross(xAxis, yAxis);

// X extent is half the length.
float xExtent = length * 0.5;

// Y extent is half the height (whatever that is).
float yExtent = height * 0.5;

// Z extent is half the width (whatever that is).
float zExtent = width * 0.5;

// The center can be computed as follows.
vector3 center = p0 + xAxis * xExtent + yAxis * yExtent + zAxis * zExtent;

Hopefully the above will help bring things into focus, but let me know if not.

Edited by Zakwayda

##### Share on other sites
Posted (edited)
13 minutes ago, Zakwayda said:

Yeah, we're definitely confused and talking past each other I don't think there was an 'mCenter' in any of my examples, so I think we've probably gotten a little off track.

I meant to say center, I think in your examples you just referred to it as center

Just to clarify, in your first example you computed the center like this

// The center can be computed as follows (as﻿suming the endpoints lie on the
// ground plane as in your images).
vector3 center = p0 + xAxis * xExtent + yAxis * yExtent;

In the example you just gave me you added the zAxis*zExent, but all other things are the same, so why did you use the two different equations for the center, I think that is what's kind of spinning me out a bit.

Edited by VanillaSnake21

##### Share on other sites
Quote

In the example you just gave me you added the zAxis*zExent, but all other things are the same, so why did you use the two different examples for the center, I think that is what's kind of spinning me out a bit.

I explained the addition of 'zAxis * zExtent' previously, but at this point I suggest not worrying about any of our previous posts and just focusing on my most recent post instead. It should have all the info you need.

If there are still uncertainties, I suggest just (re)posting all of your relevant code (including the function that builds a box from the 'start' and 'end' points), and maybe I or someone else can check over it to see if there are any obvious issues.

##### Share on other sites
2 minutes ago, Zakwayda said:

I explained the addition of 'zAxis * zExtent' previously, but at this point I suggest not worrying about any of our previous posts and just focusing on my most recent post instead. It ﻿should have all the info you need﻿.

﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿ If there are still uncertainties, I suggest just (re)posting all of your relevant code (including the function that builds a box from the 'start' and 'end' points), and﻿ maybe I or someone else can check over it to see if there are any obvious issues.﻿﻿﻿﻿

This is the code I have based on your pseudocode

void CollisionBox::init(DirectX::XMFLOAT2 startXZ, DirectX::XMFLOAT2 endXZ, float width, float height)
{
XMVECTOR startXZv = XMLoadFloat3(&XMFLOAT3(startXZ.x, 0.0f, startXZ.y));
XMVECTOR endXZv = XMLoadFloat3(&XMFLOAT3(endXZ.x, 0.0f, endXZ.y));

//find the x axis
XMFLOAT3 temp;
temp.x = endXZ.x - startXZ.x;
temp.z = endXZ.y - startXZ.y;
temp.y = 0.0f;
XMVECTOR xAxis = XMLoadFloat3(&temp);

//get the length
XMVECTOR xAxisLength = XMVector3Length(xAxis);

//make it a unit vector
xAxis = XMVector3Normalize(xAxis);

//y is always up
XMVECTOR yAxis = { 0.0f, 1.0f, 0.0f };

//get the z axis by crossing x and y
XMVECTOR zAxis = XMVector3Cross(xAxis, yAxis);

XMFLOAT3 xAxisF, yAxisF, zAxisF;
XMFLOAT3 xAxisLengthF;
XMStoreFloat3(&xAxisF, xAxis);
XMStoreFloat3(&yAxisF, yAxis);
XMStoreFloat3(&zAxisF, zAxis);
XMStoreFloat3(&xAxisLengthF, xAxisLength);

//get the x-extent (half length of the axis)
float xExtent = xAxisLengthF.x * 0.5f;
float yExtent = height * 0.5f;
float zExtent = width  * 0.5f;

//get the center
XMVECTOR center = startXZv + xAxis * xExtent + yAxis * yExtent + zAxis * zExtent;
XMFLOAT3 centerF;
XMStoreFloat3(&centerF, center);

//set all the member variables
mXAxis = xAxis;
mYAxis = yAxis;
mZAxis = zAxis;
mXExtent = xExtent;
mYExtent = yExtent;
mZExtent = zExtent;
mCenter = center;

mCollisionBoxList.push_back(this);
}

##### Share on other sites

It's possible I missed something (the code is a little hard to follow due to the DX math library, and there are some aspects of that that look like they might be superfluous), but it looks correct, at least based on a quick read-through

##### Share on other sites
3 minutes ago, Zakwayda said:

It's possible I missed something (the code is a little hard to follow due to the DX math library, and there are some aspects of that that look like they might be superfluous), but it looks correct, at least based on a quick read-through

Thanks! Thanks for all the help. I don't think I could have done this without you. Everything works beautifully. Just one last thing I wanted to ask you, if you don't mind, can you talk about what you do, your education etc and if possible can I see some of your work/code maybe on github or someplace else? It just seems like you really know your stuff and I'd love to learn more from you or at least get some idea of how to keep in touch. Thanks again.

##### Share on other sites

Thanks for the kind words

Honestly I don't have any projects right now that are 'followable', as it were. As for educational background, it's in unrelated fields, but fortunately one can learn a lot just from the wealth of material that's publicly available.

Anyway, glad you got it working! And if you run into other challenges, I'm sure someone here will be able to help out...

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• 9
• 24
• 9
• 13