I'm making a 3D maze Game in pyGame and made the environment in an OBJ file and used the OBJFileLoader found on the pyGame website to load it into my game. But when I implemented the movement controls the camera passes through the model. How do I detect collisions between my camera and the model so that I can fix that?
I thought about using rectangular collision detections but all the implementations I could find were in 2D and had a hard time trying to do it in 3D also using obj files didn't make it any easier. I also read somewhere that I can use raytracing but I couldn't find anything related to using raycasting in pyGame.
Below is the code I have in the main loop for controlling the in-game movement
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()
I'm very confused as to how I can actually block the user from moving through the walls and I need to do that for the maze game I'm making to actually work.
Generally speaking using a 3D geometry mesh as your collision detection data isn't a great idea. Usually the way we do this is to have a much simpler collision geometry (ideally composed of simple primitives such as boxes, cylinders, spheres, planes - although tri meshes can be used in some cases). The player/camera is then usually represented as a capsule, and we test the capsule against the geometry. Implementing this form of collision detection in 3D is REALLY hard.
If it is possible to boil the maze game down to a 2D problem (e.g. a top down view), then define a set of 2D lines that define the outline of the maze, and treat the player/camera as a circle.
Even the 2D scenario though is difficult. The best system would need to solve a load of simultaneous equations in order to determine the closest intersection. As the size of the maze increases, the complexity of solving this becomes extremely unpleasant (i.e. you'd need to use some form of spatial partition, such as BSP tree, quad trees, etc)
It is a very hard problem, which is why most sane people would simply make use of a physics engine to perform these calculations for them. I'd probably recommend trying to get your head around a simple 2D physics engine first, e.g. Box2D. If you absolutely need 3D collision detection, then you probably want to be looking at PhysX, bullet, or Havok.