# 基于平面法线计算物体旋转。 go评论18阅读模式

Calculating object rotation based on plane normal

# 问题

I am currently working on procedurally generated terrain using simplex noise and the marching cubes algorithm. I've completed the process of creating a ground mesh which different entities like plants will lie on. However the models rendered will always point upwards instead of the direction which the triangular face they lie on is pointing. This renders them into the ground which does not look good. I have already calculated the normals of each triangle so I am wondering how I would convert the normal of the triangular face to a 3D XYZ rotation for the model.

The image below shows my current problem:

Clipped plant models I am currently working on procedurally generated terrain using simplex noise and the marching cubes algorithm. I've completed the process of creating a ground mesh which different entities like plants will lie on. However the models rendered will always point upwards instead of the direction which the trianglular face they lie on is pointing. This renders them into the ground which does not look good. I have already calculated the normals of each triangle so I am wondering how I would convert the normal of the triangular face to a 3D XYZ rotation for the model.

The image below shows my current problem:

Clipped plant models # 答案1

``````轴 = normalize(cross(up, normal))

``````

https://en.m.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle

https://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm

Just take the cross product of up vector with plane normal. That will give you the rotation axis. Then take the dot product of the up vector with plane normal, that will give you the cosine of the rotation angle. So you have:

``````Axis = normalize(cross(up, normal))

Angle = acos(dot(up, normal))
``````

Then you can construct a quaternion or a rotation matrix from axis and angle.

https://en.m.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle

https://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm • 本文由 发表于 2020年8月2日 11:36:48
• 转载请务必保留本文链接：https://go.coder-hub.com/63212143.html
• 3d
• java
• lwjgl
• rotation