[Closed] Seting orientation from object and intersectRay
Hi, Im currently writing a script that needs to create an object on a ground surface when another object pass through it.
Im using the intersectRay function to get the object to align to the surface fine. But while I want the z direction to point in the direction of the surface normal, I also want the z rotation to match the object that passes through the ground.
Im not very good with matrix maths but im pretty sure the answer lies in doing something with both the objects TM and the rays Direction.
The other issue it that I want the initial ray direction to be flipped as I want the object to be align to the negative of the surface normal. What do i need to do to flip the rays dir.
Cheers,
Jordan
For the orientation, take the normal as Z and pick one of the axes of the source object you want to align to – either X or Y. If you pick X, then your second object will have its X axis aligned to match the X of the source object, and the Y axis will be rotated in the YZ plane of the source at the angle defined by the two Z axes. If you pick Y, the same will happen to the X axis.
So you would have
theHit = intersectRay theGround theRay
theZ = theHit.dir --this is the new Z axis oriented along the surface normal
theX = normalize theSourceObject.transform.row1 --this is the X axis from the source
theY = normalize (cross theX theZ) --this is the new Y perpendicular to both X and Z
--Now transform the object using the new X, Y and Z and set the position to the hit
theTargetObject.transform = matrix3 theX theY theZ theHit.pos
To invert the normal direction, simply take the vector with negative sign, -theHit.dir
You might want to play with the order of the Cross product operands to flip the orientation of the Y if it turns out it is pointing in the opposite direction… So cross (theX theZ) gives you the opposite Y to (cross theZ theX). You have to normalize the vectors to avoid strange scaling.
Thanks Bobo, It works well for positioning and orientation, but the scaling ended up being a bit weird.
I got the following scales like [-1.00356,-1,-0.996431] on the objects.
Hmm, normally I would expect the .dir portion of the hit to be normalized and we normalized the other two. Try normalizing theZ too before you build your matrix. Building a matrix from 3 normalized orthogonal vectors should give you a scaling of 1,1,1. Unless this is the result of some floating point imprecision…
I tried to normalize theZ too, but didnt make a difference.
I ended up just applying [1,1,1] to the scale and rotating it 180. Seemed to work ok.
Thanks for the help Bobo.
Jordan