Followers 0

# Ray tracing

## 2 posts in this topic

Hello.

Since I'm new for 3d gamedev, I decided to start with a relatively simple framework and after words transform my knowledge in something low-level, like OpenGl or DirectX. I learned how to use matrices and quaternions for transformation, it wasn't that hard. What, apparently stays hard to me is understanding of global(world coordinates) and local (mesh coordinates) coordinates systems. And, of course, I got a problem because of that misunderstanding. The problem appears in ray tracing. First one was with transformation mouse coordinates to 3D world coordinates with invisible plane.

I used this code for it and it seems, that this code doesn't work properly. Please, point out to me my mistakes:

[source lang="csharp"]
MouseState state = Mouse.GetState();
Vector3 nearSource = new Vector3((Single)state.X, (Single)state.Y, 0);
Vector3 farSource = new Vector3((Single)state.X, (Single)state.Y, 1);
Vector3 nearDest = this.graphics.GraphicsDevice.Viewport.Unproject(nearSource, this.projection, this.view, this.world);
Vector3 farDest = this.graphics.GraphicsDevice.Viewport.Unproject(farSource, this.projection, this.view, this.world);

Vector3 direction = farDest - nearDest;
direction.Normalize();
Ray ray = new Ray(nearDest, direction);
Vector3 norm = new Vector3(1.0f, 0f, 1.0f);

Plane plane = new Plane(norm, 0.0f);

//Since the ray equation is P = P0 + t*P1 and the plane equation is n.P = -d

Single t = -(plane.D + Vector3.Dot(ray.Position, plane.Normal)) / (Vector3.Dot(ray.Direction, plane.Normal));
this.resultVector = nearDest + direction * t;[/source]

And the second one was with getting intersection coordinates on a bounding box(I want to know precisely where the ray goes through and draw in that place a small dot ).

Here is my code for the ray and my understanding of transformation the ray into the mesh's coordinates system:

[source lang="csharp"]Ray ray = new Ray(Vector3.Transform(nearDest, Matrix.Invert(this.cubeModel.Meshes[0].ParentBone.Transform)), Vector3.Transform(direction, Matrix.Invert(this.cubeModel.Meshes[0].ParentBone.Transform))); [/source]

As a result for this code:
{X:-1.527575E-07 Y:-0.1127879 Z:-3.105072}
{X:-1.527575E-07 Y:-0.1127879 Z:-3.105072}
{X:10.00013 Y:-1.509978E-06 Z:10.00018}
{X:10.00013 Y:-1.509978E-06 Z:10.00018}
{X:2.369367 Y:-0.08606504 Z:-1.035683E-07}
{X:2.369367 Y:-0.08606504 Z:-1.035683E-07}

In other words for some very strange reasons, the ray intersects all box's planes...

Could you, please, point out to me my mistakes and explain why it doesn't work properly?

Thank you in advance. Edited by DmitryNik
0

##### Share on other sites
Problem solved. There are no problems with code at all. Problems were with my math =) Sorry [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
0

##### Share on other sites
Hi,

Ray tracing has the potential to really put a hit on performance if used much in a game, just so you know.

Clinton
1

## Create an account

Register a new account