# Point to Plane question

## Recommended Posts

My question has to do with how to generate a vector from a point to the nearest point on a plane.  If I have a mesh moving inside a box, I want to know the distance and direction from the mesh position to each side of the box.  I understand that would be a vector perpendicular to the plane that intersects the point.

Does Babylon have a method that will do that for me?  TIA

##### Share on other sites

Hiya K!

Um... I have some quick thoughts.  Imagine that your box is made from 6 planes, positioned to form a box shape.  Then...

distance = BABYLON.Vector3.distance(mesh.position, oneWallOfTheBox.position);

and direction... umm... you get that by... umm...

direction = mesh.position.subtract(oneWallOfTheBox.position)   // I think.  Or maybe it's...

direction = oneWallOfTheBox.position.subtract(mesh.position)  // not sure.

How to determine which is the nearest wall of all 6?  I dunno.  Perhaps, whenever mesh is moved, you need to check .distance() on all 6 box walls.  The lowest distance as the nearest wall.  ??  hmm.

I think there may be fancier and/or easier ways to attain these values.  Let's listen for more/better replies.  Using a "ray" (similar to a picking ray)... might give you direction and distance all in one little piece of code, but I am not a ray master.

There is some "ray" activities happening in the line 62 area.  Not sure about the story, there.  It looks as-if SOMEBODY almost knew what they were doing, though.

That PG was found via a playground search for 'distance'.

##### Share on other sites

Planes have normals, which are perpendicular, so you can work from there backwards (computeWorldMatrix updates everything, if you move the planes around).
The normal, if you are inside a box will probably be facing outwards, so you may not need to .negate() the Vector3 as I did.
http://playground.babylonjs.com/#QZH25V

If you don't want to write any math, you can use rays like in the PG.  Otherwise the normal of a plane can be calculated as the Cross from subtracting the corners (see comment in PG).

## 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.