Notifications
Clear all

[Closed] Change face normals

Here is a “not-optimized” version which aligns an object on a vector.
Some hours of research! … hum… Where is your dvd bobo ? lol

fn alignObjectToVector theObj theAlignVector =
	(
	theStartZ=theObj.dir
	theFinalZ=theAlignVector
	theAngle=acos (dot (normalize theStartZ) (normalize theFinalZ))
	theAxis=normalize (cross theFinalZ theStartZ) 
	theQuat=quat theAngle theAxis
	thePos=theObj.transform.translationPart
	theRot=theObj.transform.rotationPart*theQuat
	theScale=theObj.transform.scalePart
	theMatrix=(scaleMatrix theScale)*(theRot as matrix3)*(transmatrix thePos)
	theObj.transform=theMatrix
	)

alignObjectToVector selection[1] (normalize [1,1,0])

I separated all the elements from calculation. The ideal would be to not rebuild a complete matrix… but how to do this ?

1 Reply
(@bobo)
Joined: 1 year ago

Posts: 0

If you want to align the .DIR (Z axis) of the object, the ONLY thing you want to do is


 $.dir = theVector

The DVD is progressing and this topic IS part of it…

 rdg

You could create a struct/or variable with the neverchanging parts of the matrix, if there are any.

I read this thread with great enthusiasm.

Georg

This dvd will be welcome
That will avoid hours of research. (for me indeed)

Very fast! That does not make exactly the same thing. The rotation of the axis Z is reset to zero with this method.

1 Reply
(@bobo)
Joined: 1 year ago

Posts: 0

Yes, it depends on whether you care about the Z axis only, or want to preserve all the information – the more you want, the more you have to do ;).

If you want to align the .DIR (Z axis) of the object, the ONLY thing you want to do is

Amazing, can’t believe I spent so many hours when this was all I had to do.

What was wrong with the method that I wrote to do this using matrices?

1 Reply
(@bobo)
Joined: 1 year ago

Posts: 0

It just did not work.

Here is the fixed code:

fn alignObjToNorm obj norm=
  (
   zaxis = obj.dir
   xaxis = normalize( cross zaxis norm )
   yaxis = normalize( cross xaxis zaxis )
   
   rotateCoords = matrix3 xaxis yaxis zaxis obj.pos
   angle = acos ( dot zaxis  (normalize norm) )
   in coordsys rotateCoords rotate obj (quat angle [1,0,0])
  )
  
  alignObjToNorm $ [0,1,0]

You don’t have to run through the vertices to find the center. You don’t even WANT the geometry center (which can be accessed using $.center), but the .pos which is the location of the pivot point as shown by the transform gizmo.

You build the matrix, find the angle, and in coordsys the matrix you rotate the object about the X axis of the custom coordinate system at the calculated angle…

I also added a normalize for the norm vector so you can now send ANY vector as the target direction, not only normal ones…

Page 3 / 3