- Home /
calculate Vector3 offset relative to surface normal
Hi ! I'm trying to calculate four relative points from a raycast hit point, according to the face normal that was hit.
Now the offsets in the code are obviously wrongly hardcoded, just to show the intended result, but of course it just works on the plane. I guess what i need, is some trig calculation based on hit.normal to keep the red marked offsets at the same distance from center, according to the face normal that was hit.
function Update ()
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
Debug.DrawRay(hit.point, hit.normal, Color.green);
Debug.DrawRay(hit.point+Vector3(1,0,1), hit.normal*2, Color.red);
Debug.DrawRay(hit.point+Vector3(-1,0,1), hit.normal*2, Color.red);
Debug.DrawRay(hit.point+Vector3(-1,0,-1), hit.normal*2, Color.red);
Debug.DrawRay(hit.point+Vector3(1,0,-1), hit.normal*2, Color.red);
}
}
I can think of a dirty solution like:
creating a dummy plane, and keeping it centered to the raycast hit point
adjusting it's size to match the desired distance (the red offsets)
aligning it with the surface normal it is on
then picking my surface-orientation-relative points from the plane i'm using
But i'm actually more interested in understanding the trig that has been passing over my head in the last few days, or even some transformation function I may have missed from the documentation. So, i would really appreciate any hint! Thanks !
Answer by robertbu · Mar 05, 2014 at 01:16 AM
There are a lot of different ways of solving this problem. One issue is that you have a degree of freedom here you did not define. That is the four red lines may be rotate to any arbitrary angle with the green normal as the axis. If you don't care about that rotation, then a really simple approach is to use an empty game object (with a scale of (1,1,1)). Then you can do:
empty.transform.position = hit.point;
transform.up = hit.normal;
var pos1 = empty.transform.TransformPoint(Vector3(1,0,1));
var pos2 = empty.transform.TransformPoint(Vector3(-1,0,1));
var pos3 = empty.transform.TransformPoint(Vector3(-1,0,-1));
var pos4 = empty.transform.TransformPoint(Vector3(1,0,-1));
Thanks for your time, robertbu!
You may have missed the part where i already thougth about using an empty object, aligning it, and using it's points to cast rays.
What I'm interested in understanding, is how to achieve this, working with the vectors i have, being hit.point and hit.normal
But you're right, I may have not defined completely the conditions: I want the lines to be parallel to hit normal, "sliding" over the surface with a given offset.
You may have missed the part where i already thougth about using an empty object, aligning it, and using it's points to cast rays.
Didn't miss it, but thought that you were talking about a real object of some sort rather than an empty game object with the reference to 'resizing'. Let me throw out another solution. For it to work you need either 1) one additional point on the plane, or 2) any vector that is not the same as the normal (and can be generated randomly). Using the second, you can do:
var dir = Vector3.Cross(hit.normal, otherVector).normalized;
var pos1 = hit.point + dir;
var pos2 = hit.point + Quaternion.AngleAxis(90,hit.normal) * dir;
var pos3 = hit.point + Quaternion.AngleAxis(180,hit.normal) * dir;
var pos4 = hit.point + Quaternion.AngleAxis(270,hit.normal) * dir;
Or this should work:
Quaternion q = Quaternion.FromToRotation(Vector3.up, hit.normal);
var pos1 = hit.point + q * Vector3(1,0,1);
var pos2 = hit.point + q * Vector3(-1,0,1);
var pos3 = hit.point + q * Vector3(-1,0,-1);
var pos4 = hit.point + q * Vector3(1,0,-1);
yes ! the second solution is more like it ! :) $$anonymous$$aybe since the raycast is from the camera, camera transform could be taken into account. Anyway thanks again, i'm testing your hints right now !
Your answer
Follow this Question
Related Questions
How to get the angle between an object and a plane based on the plane's normal? 3 Answers
Change orientation / rotation of NavMeshAgent, Trig question? 0 Answers
Check if object is inside a viewing radius 1 Answer
LineRender and Gizmos.DrawRay are showing different results for the same input values 0 Answers