Suface Orientation for Raycasting

Hi all,

Is there a way to get the suface orientation from a raycast hit?

I basically want to create an object at a raycast hit location (which i’m already doing) but then I want the object to be the same rotation as the surface. So the object is flat against a surface (such as a bullet hole).

Thanks,
-iDev

I believe what I’m looking for is called surface normal vector. I did notice one of the properties which is returned is called hitNormal. Although rotating or rotating from eular using the hitNormal vector didn’t seem to work :thinking:

Any ideas or am I looking at the wrong thing? In an old engine I used to use you would tell it the object and the vector of the point you would like to know the surface normal.

what you mean of hitNormal vector didn’t work? hitNormal is the normal of the hit plane, in world space.

I tried to use set rotation from eular but that didn’t work and I’m not sure why, it actually messed it up so the node just didn’t show when I tried it. As the output is a vector and not a quatronion I presume from eular would need to be the way I use the hitNormal?

Can you show the results you have achieved so far?

As soon as I get home I’ll get as much information as I can togeather and do some more testing. Thanks for your time so far.

So the results of this is no rotation.

this.node.setRotationFromEuler(result.hitNormal);

or

this.node.setRotationFromEuler(new Vec3(result.hitNormal.x,result.hitNormal.y,result.hitNormal.z));

I do how ever get consistent vectors from the hitNormal depending on the orientation of the suface that is hit such as:

Vec3 {x: 1, y: 0, z: 0}

or

Vec3 {x: 0, y: -1.1920928955078125e-7, z: -0.9999999403953552}

which does suggest the hitNormal is working correctly. The issue must be with how I’m trying to turn a vector into a rotation?

I hope someone can help me with how I can rotate the object to be the same angle as the hitNormal.

Thanks

hitNormal is not the same as Eulerian angle, I think you need to find the initial vector and then derive the rotation angle you want based on the angle between the two vectors.

Ok, at least that’s good to know. When you say original vector do you mean the original position of where the ray was casted from or the hitPoint? Also any suggestions on how to go about it would be great. I.E a function under Quat, Vec3 or Math is where I should be looking. Thanks for your help.

i mean the world-coordinate, (0, 1, 0), (0, 0, 1) and (1, 0, 0)
https://docs.cocos.com/creator/manual/en/concepts/scene/coord.html#world-coordinate

So you mean the original rotation of the object? Which should be (0,0,0).

Something like:

Var rot = Quat.rotationTo(new Vec3(0,0 0), result.hitNormal);

this.node.setRotation(rot);

?

I mean to find the angle with these three vectors separately : (0, 1, 0), (0, 0, 1) and (1, 0, 0)

So are you saying I need to work out each XYZ axis individually?

Ok, am I getting warmer?

var rot = new Quat;
Quat.fromViewUp(rot,result.hitNormal,Quat.UP);
this.node.setRotation(rot);

yeah, you have slowly found the solution.

Thanks for your help so far, I’ll try this later and see how I get on and I’ll post back if I’m still missing something, which I’m sure I am :sweat_smile:

Fantastic, it works. Thanks for your help!