【发布时间】:2019-07-10 16:51:20
【问题描述】:
我正在 pyGame 中制作 3D 迷宫游戏,并在 OBJ 文件中制作环境,并使用 pyGame 网站上的 OBJFileLoader 将其加载到我的游戏中。但是当我实现运动控制时,相机会穿过模型。如何检测我的相机和模型之间的碰撞以便修复?
我考虑过使用矩形碰撞检测,但我能找到的所有实现都是 2D 的,并且很难尝试在 3D 中进行,而且使用 obj 文件并没有让它变得更容易。我还在某处读到了可以使用光线追踪的文章,但找不到与在 pyGame 中使用光线投射相关的任何内容。
以下是我在主循环中用于控制游戏内移动的代码
while 1:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYUP and event.key == K_ESCAPE:
sys.exit()
time_passed = clock.tick()
time_passed_seconds = time_passed / 1000.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
pressed = pygame.key.get_pressed()
rotation_direction.set(0.0, 0.0, 0.0)
movement_direction.set(0.0, 0.0, 0.0)
if pressed[K_a]:
rotation_direction.y = +1.0
elif pressed[K_d]:
rotation_direction.y = -1.0
if pressed[K_w]:
movement_direction.z = -1.0
elif pressed[K_s]:
movement_direction.z = +1.0
# Calculate rotation matrix and multiply by camera matrix
rotation = rotation_direction * rotation_speed * time_passed_seconds
rotation_matrix = Matrix44.xyz_rotation(*rotation)
camera_matrix *= rotation_matrix
# Calcluate movment and add it to camera matrix translate
heading = Vector3(camera_matrix.forward)
movement = heading * movement_direction.z * movement_speed
camera_matrix.translate += movement * time_passed_seconds
# Upload the inverse camera matrix to OpenGL
glLoadMatrixd(camera_matrix.get_inverse().to_opengl())
glCallList(obj.gl_list)
pygame.display.flip()
我很困惑如何真正阻止用户穿过墙壁,我需要这样做才能让我正在制作的迷宫游戏真正工作。
【问题讨论】:
-
您也许可以根据迷宫的布局做出一些简化的假设。从自上而下的角度来看,它本质上是“2D”吗?它是否遵循网格?这是在第一人称视角(在这种情况下,相机可以被视为一个点或边界球体)还是第三人称视角(在这种情况下,您可以制作一个表示对象边界的简化网格,而不是测试实际的网格本身三角形按三角形)?
标签: python pygame pyopengl opengl-compat