Jump to content
  • Advertisement
Sign in to follow this  
BlackJoker

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!