英文:
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:
答案1
得分: 1
只需将上向量与平面法线向量叉乘,这将给出旋转轴。然后将上向量与平面法线向量点乘,这将给出旋转角的余弦值。所以你有:
轴 = normalize(cross(up, normal))
角度 = acos(dot(up, normal))
然后,你可以使用轴和角度构建四元数或旋转矩阵。
参考:
https://en.m.wikipedia.org/wiki/Rodrigues%27_rotation_formula
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.
See:
https://en.m.wikipedia.org/wiki/Rodrigues%27_rotation_formula
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论