x


Math - calculate position in world space from ray on infinite plane

Hey everyone,

I'm currently using the Unity Plane object to create an infinite plane, and to cast several rays against it. Like so:

Example

Plane.Raycast returns

1) whether it has hit the plane or not, and

2) the distance, in case it has a hit

So I cast a ray from my camera, and it hits my plane. However, with just the distance information I cannot calculate where in 3D space each ray hits.

What mathematical calculations can I use in order to calculate the 3D intersectionpoints of each ray?

more ▼

asked Jan 25 '11 at 11:05 PM

Jordii gravatar image

Jordii
158 9 11 17

Sorry just read the Plane.Raycast :D. wait, i'll add an example

Jan 25 '11 at 11:16 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Basically if you cast a ray you normally have a starting point and a direction vector. if you have the distance you hit something just add the normalized direction vector multiplied by the distance to your starting point. That's it. But the Ray class in Unity have a special function that will do that for you: Ray.GetPoint()

{
   Ray myRay = new Ray(startpoint, direction); // or use any other ray
   float dist;
   if (myPlane.Raycast(myRay,out dist))
   {
      Vector3 hitPoint = myRay.GetPoint(dist);
   }
more ▼

answered Jan 25 '11 at 11:13 PM

Bunny83 gravatar image

Bunny83
45.1k 11 48 206

That doesn't work, that is RaycastHit which is information extracted from a normal Raycast. I cannot use normal Raycasts since they only can be done on meshcolliders. My infinite plane is no mesh, it's a theoretical plane. Check Plane: http://unity3d.com/support/documentation/ScriptReference/Plane.html

Jan 25 '11 at 11:20 PM Jordii

yeah, sorry, I changed the answer already ;)

Jan 25 '11 at 11:27 PM Bunny83

Ah your answer has been editted :D So my last reply doesn't fit the bill. I will try this tomorrow Bunny. Using the Ray object was new to me, hope it'll work :) Thanks for adding the math too, I want to understand that part too!

Jan 25 '11 at 11:30 PM Jordii

Bunny thanks very much, just checked it, and it worked :)

Jan 26 '11 at 09:41 PM Jordii
(comments are locked)
10|3000 characters needed characters left

Use Plane.Raycast() then ray.GetPoint()

var ray : Ray;
var dist : float;

if( Plane.Raycast(ray, out dist) ) {
     var hitPoint : Vector3 = ray.GetPoint(dist);
}
more ▼

answered Jan 25 '11 at 11:26 PM

Peter G gravatar image

Peter G
15k 16 44 136

Great now we have both C# and Unityscript ;)

Jan 25 '11 at 11:34 PM Bunny83

Thank you sir, although Bunny pointed me in the right direction, your help is much appreciated

Jan 26 '11 at 09:42 PM Jordii
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1525
x354
x249
x151
x34

asked: Jan 25 '11 at 11:05 PM

Seen: 2185 times

Last Updated: Jan 25 '11 at 11:05 PM