简体   繁体   中英

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. the main 3D transformation is:

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. 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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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