简体   繁体   English

在 pygame 中的列表上旋转

[英]Rotation over a list in pygame

If I can get some help I will post more of this code.如果我能得到一些帮助,我会发布更多此代码。 It is a bit weird.这有点奇怪。 I picked it up from a video and cleaned it up a bit.我从视频中捡到它并清理了一下。 Basically it captures all the x coordinates in cam.pos[0] , all the y coordinates in cam.pos[1] , and all the z coordinates in cam.pos[2] .基本上,它捕获所有的X坐标cam.pos[0]所有的y坐标在cam.pos[1]和所有的Z坐标在cam.pos[2] My 3D environment has a few dozen cubes of which I can go towards, away, above, below, to the left and to the right.我的 3D 环境有几十个立方体,我可以朝着、远离、上方、下方、左侧和右侧移动。 But I cannot turn.但是我不能转。 This is my attempt at a corkscrew:这是我对开瓶器的尝试:

while True:
 dt = clock.tick()/1000
 rot = clock.tick()/1000
 r = rot*36
 for event in pygame.event.get():
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_x:
             cam.pos[0] = cam.pos[0]*np.cos(r)-cam.pos[1]*np.sin(r)
             cam.pos[1] = cam.pos[0]*np.sin(r)-cam.pos[1]*np.cos(r)
             cam.pos[2] = cam.pos[2]
     if event.type == pygame.QUIT: pygame.quit(); sys.exit()
     if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_ESCAPE: pygame.quit(); sys.exit()

     cam.events(event)
 screen.fill((0,0,0))
 face_list = []; face_color = []; depth = []
 for obj in cubes:

     vert_list = []; screen_coords = []
     for x,y,z in obj.verts:
         x-=cam.pos[0]; y-=cam.pos[1]; z-=cam.pos[2]

I can post more if anyone is interested.如果有人感兴趣,我可以发布更多。 It does not return any errors but neither is it corkscrewing.它不会返回任何错误,但也不会开瓶器。 Thanks.谢谢。

Dropping in the code is not working out for some reason.由于某种原因,插入代码不起作用。 You can look at it here: python_flyer你可以在这里查看: python_flyer

You can see I have made changes:您可以看到我进行了更改:

datax = cam.pos[0]
datay = cam.pos[1]
for i in range(datax):
for j in range(datay):
cam.pos[0] = i * np.cos(r) - j * np.sin(r)
cam.pos[1] = i * np.sin(r) - j * np.cos(r)

The error is now:现在的错误是:

for i in range(datax):
TypeError: 'float' object cannot be interpreted as an integer

I call that progress我称之为进步

Be aware, after the line请注意,上线后

cam.pos[0] = cam.pos[0]*np.cos(r)-cam.pos[1]*np.sin(r)

the value in cam.pos[0] is changed. cam.pos[0]的值已更改。 So the result of所以结果是

cam.pos[1] = cam.pos[0]*np.sin(r)-cam.pos[1]*np.cos(r)

depends on the calculation of the former expression.取决于前一个表达式的计算。

Store the content of cam.pos to local variables, before the rotation is computed.在计算旋转之前,将cam.pos的内容cam.pos到局部变量。
Further the formula for the rotation is wrong.此外,旋转公式是错误的。 SeeRotation matrix - In two dimensions :请参阅旋转矩阵 - 在二维中

 x` = x * cos(theta) - y * sin(theta) y` = x * sin(theta) + y * cos(theta)

eg例如

x, y, z = cam.pos    
cam.pos[0] = x * np.cos(r) - y * np.sin(r)
cam.pos[1] = x * np.sin(r) + y * np.cos(r)
cam.pos[2] = z   

Don't call clock.tick() twice in a row.不要连续两次调用clock.tick() This function returns the number of milliseconds which have passed since the previous call.此函数返回自上次调用以来经过的毫秒数。 So the 2nd call will always return 0.0.所以第二次调用将始终返回 0.0。

Your camera position is (0, 0, -5).您的相机位置是 (0, 0, -5)。 If this position is rotated around the z axis by any angel, then the result is still (0, 0, -5).如果这个位置绕 z 轴旋转任意角度,那么结果仍然是 (0, 0, -5)。 Rotate it around the y axis.将其绕 y 轴旋转。

Try the following code, which will animate your scene:尝试以下代码,它将为您的场景设置动画:

while True:
    ticks = clock.tick()
    dt = ticks/1000
    rot = ticks/1000

    r = rot
    x, y, z = cam.pos    
    cam.pos[0] = x * np.cos(r) - z * np.sin(r)
    cam.pos[1] = y 
    cam.pos[2] = x * np.sin(r) + z * np.cos(r)   

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

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