# from one objects local space to another

## Recommended Posts

Hi, so I've converted Tomas Möller's Triangle-Triangle Overlap Test code for babylonjs to do precise intersection for 2 meshes.

Currently the code I have looks like this which uses the triangles in world space. However I figured it'd be faster to convert mesh B into mesh As localspace instead.

``````
const Av = getTriangles( a );
const Bv = getTriangles( b );

function getTriangles ( mesh ) {

const vertices = mesh.getVerticesData( BABYLON.VertexBuffer.PositionKind );
const indices = mesh.getIndices();
const worldMatrix = mesh.getWorldMatrix();

let result = [];
// Get all triangles
for ( let i = 0; i < indices.length; ++i ) {
let index = indices[ i ] * 3;
let vertex = new BABYLON.Vector3( vertices[ index ], vertices[ index + 1 ], vertices[ index + 2 ] );
BABYLON.Vector3.TransformCoordinatesToRef( vertex, worldMatrix, vertex );

result.push( vertex );
}

return result;
}``````

Which for A would skip the transformCoordinates step and for B I would calculate a matrix that looks like this to move B to A's local space:

``matrix = bWorldMatrix * aInvertedWorldMatrix``

Is that the way to convert B to A's local space or is it some other way to do it? Or some other better way to get all the triangles for the tri to tri overlap test?

##### Share on other sites

Have you had a look at facetData? Each facet is a basic triangle that makes up a mesh. It allows you to get local position data

Jerome started it here as a way of bouncing small particles off a mesh and it needs version 3.0-alpha, which the playground uses or for external use you can get it here.

I did some work on using it to find where two meshes intersected but had never heard of Tomas Möller's Triangle-Triangle Overlap Test

My playground for this is at http://www.babylonjs-playground.com/#XEJLM with words of warning in the posting.

You will probably be better starting from scratch.

##### Share on other sites

Interesting, I think in my case it's better to use the current triangle getting logic because I only need the positions and nothing else.

Here's a link to the C or C++ implementation if you're interested:

Edit: Ended up going with the original solution and not using facetdata.
As for going from local B -> local A:

``````// Declared outside function as reusable matrix:
var _tmpMatrix = BABYLON.Matrix.Identity();

// To convert from localSpace B -> localSpace B
let finalMatrix = _tmpMatrix;
const aWorldMatrix = a.getWorldMatrix();
const bWorldMatrix = b.getWorldMatrix();

let tmpAInverseWorldMatrix = BABYLON.Tmp.Matrix[ 0 ];

aWorldMatrix.invertToRef( tmpAInverseWorldMatrix );

bWorldMatrix.multiplyToRef( tmpAInverseWorldMatrix, finalMatrix );

// Later on when iterating each vertex for mesh b
BABYLON.Vector3.TransformCoordinatesToRef( vertex, finalMatrix, resultVertex );``````

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.