Collision detection makes the world feel solid

Published November 21, 2010
Advertisement
One of the larger problems with the 3D engine for the TI-83+ calculator series I have been working on is that it's possible to move the camera through walls. This doesn't make the world feel especially solid, so I've started working on some collision detection routines.

Work commitments have left me with little time to spend on this project over the last couple of weeks so progress has been very slow, but I've got a basic collision detection system mostly working.

Nostromo settings screen

Click to view an animated GIF of the collision detection routines in action.

I spend most of the above screenshot running into walls. The code seems to work relatively well and quite quickly, though it's far from perfect. The still image shows the new settings screen, which is hopefully a little easier to use than remembering which keys do what. It also has the advantage of displaying the state of the current settings.

The walls are stored as line segments between two 2D vertices, and the collision detection has to ensure that the player does not get too close to any of these walls. The technique I have used starts by calculating the closest point on the line to the player.

Closest point to a line segment
The above image shows a wall (the solid line segment) and three possible player positions (the heavy dots). The arrows point to the closest point on the wall's line. The closest point on the line to the top player position is past the end of the line segment, so it is ignored. The other two closest points lie on the line segment, so these are checked in more detail.

Threshold distance between the line segment and player position
The distance between the closest point on the line and the player position is then calculated and compared to a threshold value (the radius of the player). The above image highlights the out-of-bounds region in tan. The lower player position is outside this region so is ignored, but the upper player position is inside it and needs to be corrected.

Corrected player position
The correction is quite straightforward. We know the closest point on the wall to the player. The angle of the wall's normal is stored in the level file, so we can easily calculate a vector from that to push the player a fixed distance away from the wall.

In addition to the above 2D checks, a very simple height check is performed for "upper and lower"-type walls. These are walls with a central hole so you can pass over or under them, and are used to connect sectors with varying floor and ceiling heights. The top of the player's head is used to check the ceiling height. Rather than use the height of the player's feet to check the floor height their knee height is used. This is to allow the player to climb low walls (such as the edges of steps).

When I first implemented these collision detection techniques I checked every wall in the map. This halved the framerate in places, and as the framerate is not particularly high in the first place I needed to find a way to reduce the number of tests. Taking further inspiration from DOOM I implemented a "blockmap". This breaks the map down into square blocks and each block contains a list of which walls pass through it. To perform collision detection I look up which block the player is in and from that I can retrieve a reduced list of which walls they may end up walking into. The original implementation had to check well over a hundred walls for each movement; the blockmap reduces this to 26 in the worst case scenario for the current level design.

Sadly, this additional blockmap enlarged the size of the map quite a bit, so I've attempted to reduce it a little. For simplicity and performance most structures referred to other structures by pointer (for example a sub sector contained a list of pointers to walls and each wall contained pointers to a front and back sector). I've changed most of these to now refer to other structures by index, which shaved a few hundred bytes off the map at the cost of a few hundred clock cycles. Overall performance still isn't great, though I haven't found it noticeably slower than the previous demos.

I added very primitive physics for moving the player up and down relative to the floor to complement the collision detection. This retrieves the floor height from the sector directly under the centre of the player and compares it to the current player height. If the new floor height is higher than the old floor height then the player's foot height is set to a point half way between the two; this smoothes the animation slightly when climbing up stairs (rather than just snapping to the new floor height). When moving from a higher floor to a lower floor the player's downward speed is increased to roughly simulate gravity.

A demo for the TI-83+ series and TI-83 can be found in Nostromo.zip. As always, this is a piece of software in development and there may be calculator-crashing bugs, so please back up any important files before running it.
0 likes 2 comments

Comments

Extrarius
Does your collision handling allow you to slide along walls by walking into it? For spatial partitioning, you could use the already-existing concept of sectors - generally, objects can only collide with the walls of the sector it is in, unless it is crossing a line between sectors, in which case it can collide with walls in both (or more) sectors.
November 25, 2010 04:02 PM
benryves
Quote: Original post by Extrarius
Does your collision handling allow you to slide along walls by walking into it?
Yes, though it wasn't intentional. When the player is pushed away from a wall they collided with I use a vector perpendicular to the wall and push them away from the closest point on the wall. Sliding seems to work automatically. The technique isn't ideal, as it's possible to walk through walls if the framerate drops low enough. I may need to break movements into multiple shorter movements if they are detected as being long enough to potentially walk through a wall.

Quote: For spatial partitioning, you could use the already-existing concept of sectors - generally, objects can only collide with the walls of the sector it is in, unless it is crossing a line between sectors, in which case it can collide with walls in both (or more) sectors.
It's a good plan, but I only know how to look up convex subsectors from a point, and I'd need to find any subsectors that intersect the player's bounding circle. The blockmap seemed like the easiest way to go.
November 26, 2010 08:43 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement