Fixed formatting issues

Kayne Ruse
2014-12-27 04:15:03 +11:00
parent a12e844103
commit 3df30a2b77
+40 -39
@@ -1,4 +1,4 @@
Abstract: ## Abstract
The goal of this pseudocode is to create a collision system that brings several The goal of this pseudocode is to create a collision system that brings several
colliding bodies to a near-contact state (no space between the bodies, but not colliding bodies to a near-contact state (no space between the bodies, but not
@@ -6,7 +6,7 @@ overlapping), while preserving the motion of said bodies. For simplicity, I've
decided to use a square box (BoundingBox, or BBox for short) for the collidable decided to use a square box (BoundingBox, or BBox for short) for the collidable
bodies. bodies.
Example: ## Goal
My current game has a tiled-map, where the tiles are arranged on a 2D grid. The My current game has a tiled-map, where the tiles are arranged on a 2D grid. The
tiles each have a flag indicating if they are solid (i.e. collidable) or not. I tiles each have a flag indicating if they are solid (i.e. collidable) or not. I
@@ -22,19 +22,21 @@ Obviously, there are many in depth issues that I will need to take into
account when writing this logic, that have been glossed over or omitted in this account when writing this logic, that have been glossed over or omitted in this
article. article.
------------------------- ## Design
### Basic Example
``` ```
velocity = motion + speed velocity = motion + speed
if (collision(position + velocity)) then if (collision(position + velocity)) then
if (collision(position + {velocity.x, 0})) then if (collision(position + {velocity.x, 0})) then
velocity.x = 0 velocity.x = 0
end
if (collision(position + {0, velocity.y})) then
velocity.y = 0
end
end end
position = position + velocity if (collision(position + {0, velocity.y})) then
velocity.y = 0
end
end
position = position + velocity
``` ```
This code is a basic outline for a collision system that preserves the object's This code is a basic outline for a collision system that preserves the object's
@@ -42,15 +44,15 @@ motion, but it still leaves several pixels of space between the bounding boxes.
Notably, it also treats "collision" as an abstract concept, rather than as an Notably, it also treats "collision" as an abstract concept, rather than as an
event that could happen multiple times per frame. event that could happen multiple times per frame.
------------------------- ### Better Example
``` ```
velocity = motion + speed velocity = motion + speed
if (collisionSimple(BOXSET, position + velocity)) then if (collisionSimple(BOXSET, position + velocity)) then
velocity.x = collisionX(BOXSET, velocity.x) velocity.x = collisionX(BOXSET, velocity.x)
velocity.y = collisionY(BOXSET, velocity.y) velocity.y = collisionY(BOXSET, velocity.y)
end end
position = position + velocity position = position + velocity
``` ```
Here, collisions are still abstract, but "BOXSET" is defined externally Here, collisions are still abstract, but "BOXSET" is defined externally
@@ -62,17 +64,17 @@ If there are any collisions between the player object and the given box set,
then collisionX() and collisionY() are called to calculate the new distance then collisionX() and collisionY() are called to calculate the new distance
that the character will move. that the character will move.
------------------------- ### Simple Collision Check
``` ```
bool collisionSimple(BOXSET, newPos): bool collisionSimple(BOXSET, newPos):
for_each box in BOXSET do for_each box in BOXSET do
if (box.overlap(PLAYER.box + newPos)) then if (box.overlap(PLAYER.box + newPos)) then
return true return true
end
end end
return false
end end
return false
end
``` ```
collisionSimple() first runs through the BOXSET, checking if any of the given collisionSimple() first runs through the BOXSET, checking if any of the given
@@ -90,24 +92,23 @@ would not have been checked against the removed elements.
Just something to note. Just something to note.
------------------------- ### Velocity Correction
``` ```
var collisionX(BOXSET, velocityX): var collisionX(BOXSET, velocityX):
var ret = velocityX var ret = velocityX
for_each box in BOXSET do for_each box in BOXSET do
if (box.overlap(PLAYER.box + PLAYER.position + {velocityX,0})) then if (box.overlap(PLAYER.box + PLAYER.position + {velocityX, 0})) then
if (velocityX > 0) then if (velocityX > 0) then
ret = min(ret, box.west - PLAYER.position.x) ret = min(ret, box.west - PLAYER.position.x)
else else
ret = max(ret, box.east - PLAYER.position.x) ret = max(ret, box.east - PLAYER.position.x)
end
end end
end end
return ret
end end
return ret
end
``` ```
Two things: 1. collisionX() and collisionY() should be identical except for the Two things: 1. collisionX() and collisionY() should be identical except for the
@@ -122,4 +123,4 @@ right (or up or down).
An unfortunate bug I can already see is that this logic doesn't check corners; An unfortunate bug I can already see is that this logic doesn't check corners;
it might be possible to get stuck on a corner of a wall, but if this becomes an it might be possible to get stuck on a corner of a wall, but if this becomes an
issue in my implementation I will update this article with that information, issue in my implementation I will update this article with that information,
and you can promptly ignore it. and you can promptly ignore it.