简体   繁体   English

自制 3D 引擎中的奇怪旋转

[英]weird rotations in self made 3D engine

Hello and sorry if my question is repetitive, Im building my first 3D engine using python and pygame for the graphics.您好,抱歉,如果我的问题是重复的,我正在使用 python 和 pygame 构建我的第一个 3D 引擎作为图形。 the main 3D transformation is:主要的 3D 转换是:

def param(x,y,z):
vec_rx = rotate_x((x,y,z), angle_x)
vec_ry = rotate_y(vec_rx , angle_y)
vec = rotate_z(vec_ry , angle_z)
return ((zoom * vec[0])/(70-vec[2]) + win_width/2,( (zoom * vec[1])/(70-vec[2]) ) + win_height/2)

70 is for the distance from the origin. 70 是与原点的距离。 the rotation is by multiplying matrices:旋转是通过矩阵相乘:

def rotate_x(vec,angle):
    a = vec[0]
    b = vec[1]*math.cos(angle) - vec[2]*math.sin(angle)
    c = vec[1]*math.sin(angle) + vec[2]*math.cos(angle)
    return (a,b,c)
def rotate_y(vec,angle):
    a = vec[0]*math.cos(angle) + vec[2]*math.sin(angle)
    b = vec[1]
    c = -vec[0]*math.sin(angle) + vec[2]*math.cos(angle)
    return (a,b,c)
def rotate_z(vec,angle):
    a = vec[0]*math.cos(angle) - vec[1]*math.sin(angle)
    b = vec[0]*math.sin(angle) + vec[1]*math.cos(angle)
    c = vec[2]
    return (a,b,c)

the angles are 3 global parameters changing with keyboard/mouse input.角度是 3 个全局参数,随键盘/鼠标输入而变化。 when the angles are zero the rotation is perfect around each axis but when not zero the object is not rotating around the axis but some weird offset.当角度为零时,围绕每个轴的旋转是完美的,但当不为零时,对象不会围绕轴旋转,而是有一些奇怪的偏移。 that might be gimbal lock though im not sure.这可能是万向节锁虽然我不确定。

here is an example of the 3d engine in my project made in desmos: https://www.desmos.com/calculator/8by2wg0cek you can play with the angles and see a similar effect.这是我在 desmos 中制作的项目中的 3d 引擎示例: https ://www.desmos.com/calculator/8by2wg0cek 您可以玩转角度并看到类似的效果。

is there anything im missing in order to make perfect rotations around the axis?为了围绕轴进行完美的旋转,我缺少什么吗?

thank you very much!非常感谢!

Nice job.不错的工作。 From what I can see the object is rotating around the axes, there is no offset, but the axes stay in place.从我所看到的对象围绕轴旋转,没有偏移,但轴保持原位。 In other words, the object is rotating around the coordinate system axes, not its own axes.换句话说,对象围绕坐标系轴旋转,而不是它自己的轴。

I am afraid your math is a bit simplified.恐怕你的数学有点简化了。 It works for fixed axes, but when it comes to arbitrary ones, it fails.它适用于固定轴,但当涉及任意轴时,它会失败。 And when you start stacking transformations, it is going to get yet more complex.当您开始堆叠转换时,它会变得更加复杂。 You will need to learn about quaternions, etc. I would suggest you using OpenGl instead of implementing it all from scratch.您将需要了解四元数等。我建议您使用 OpenGl 而不是从头开始实现。

But I don't want to scare you, it's definitely doable.但我不想吓到你,这绝对是可行的。 A good starting point could be this post: https://stackoverflow.com/a/14609567/9090751一个很好的起点可能是这篇文章: https : //stackoverflow.com/a/14609567/9090751

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM