# Ray-triangle intersection on scaled model

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

## Recommended Posts

Hi. I faced with one issue while make test on intersection of ray with triangles.

It works quite well with non-scaled model, but with scaled - ray intersection returns false.

Does anyone know which additional manipulations with the ray I must do to have fully correct results with scaled models?

In general, even without code. I beleive it must be quite common thing that I am missing.

Edited by BlackJoker

##### Share on other sites

Do the ray/triangle test in the local space of the mesh.

##### Share on other sites

But when ray is creating, I thought it already in local space, because intersection with non-scaled model returns correct results.

##### Share on other sites

if your point by scaling model is using a matrix that scales the model, then if you test ray/triangle intersection, you have to transform each triangle vertex by this transfomration.

##### Share on other sites

I tryied that with no luck, intersection still incorrect:

var transform = Matrix.Scaling(possibleEntity.GetComponent<ScaleComponent>().Scale);
for (int i = 0; i<indices.Length; i+=3)
{
var vertex1 = vertecies[indices[i]].Position;
var vertex2 = vertecies[indices[i+1]].Position;
var vertex3 = vertecies[indices[i+2]].Position;
vertex1 = Vector3.TransformCoordinate(vertex1, transform);
vertex2 = Vector3.TransformCoordinate(vertex2, transform);
vertex3 = Vector3.TransformCoordinate(vertex3, transform);
var intersects = tRay.Intersects(ref vertex1, ref vertex2, ref vertex3, out intersectPoint);
if (intersects)
{

intersectPoint = Vector3.TransformCoordinate(intersectPoint, transform);
point = intersectPoint;
return true;
}
}


##### Share on other sites

But when ray is creating, I thought it already in local space, because intersection with non-scaled model returns correct results.

Non-scaled model returning correct result does not imply you are doing the comparison in local space....

you have to transform each triangle vertex by this transfomration.

It's much more efficient to transform the ray instead of each vertex.

That is what "doing the comparison in local space" means.

you transform the ray from world space into local space of the mesh, instead of the other way around.

I'm guessing the transformation of the ray is where it goes wrong.

It would be easier to help if you posted your original code.

##### Share on other sites

OK, here is my code, but its SharpDX specific a little:

public Ray CalculateRay(Vector2 mouseLocation, CameraComponent camera, Matrix worldMatrix)
{
Matrix worldviewProj = worldMatrix * camera.ViewMatrix * camera.ProjectionMatrix;

return Ray.GetPickRay((int)mouseLocation.X, (int)mouseLocation.Y, new ViewportF(0, 0, camera.Width, camera.Height, 0, 1), worldviewProj);
}

ray = CalculateRay(MouseManager.RelativeCoordinates, cameraController.UserControlledCamera,
entity.GetActualMatrix());


and here is my triangle intersection method:

for (int i = 0; i<indices.Length; i+=3)
{
var vertex1 = vertecies[indices[i]].Position;
var vertex2 = vertecies[indices[i+1]].Position;
var vertex3 = vertecies[indices[i+2]].Position;
var intersects = tRay.Intersects(ref vertex1, ref vertex2, ref vertex3, out intersectPoint);
if (intersects)
{

intersectPoint = Vector3.TransformCoordinate(intersectPoint, transform);
point = intersectPoint;
return true;
}
}


Works perfect with non-scaled models.

Inside GetPickRay() calculate Vector3.Unproject()

You can view code here https://github.com/sharpdx/SharpDX/blob/master/Source/SharpDX.Mathematics/Ray.cs

Any thoughts?

##### Share on other sites

My guess would be to transform the ray by the inverse of the "transform" matrix, instead of transforming each vertex by "transform"..

##### Share on other sites
vinterberg

What do you mean under "transform"? Its WVP matrix or only object`s world matrix?

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633741
• Total Posts
3013624
×