TileNet
holds integer aligned tiles for broad phase continuous collision detection.
The purpose of TileNet
is to have a solid, tile-based, continuous, simple collision
library for aspiring game programmers.
How it works
The library is built on the DDA Supercover algorithm, which is an extension of
Bresenham's algorithm. For each moving vertex it creates a line. Each line's
overlapping tiles are reported. Your dynamic object decides how it should move.
It may adjust speed, and retry the collision. It may also accept and move.
Limitations
The library will experience problems with huge coordinates. This is because adding
a small increment to a floating point above 224 may not register at all. Precision
becomes worse as you approach 224. The technical reason is that a 32-bit float
has 24 bits in its mantissa.
You do not need to worry about floating point errors, as the library ensures consistency
by checking end-points.
Examples - Setting Up
We start out by including tile net into our program and creating an empty net
extern crate tile_net;
use tile_net::*;
fn main() {
let net: TileNet<usize> = TileNet::new(10, 10);
println!["{:?}", net];
}
This creates a TileNet
that contains usize
as its elements.
All tiles are initialized to Default
of usize
.
You can now edit various tiles:
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set(&1, (9, 0));
println!["{:?}", net];
}
There are several helper functions so you can easily draw something interesting
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set_row(&1, 0);
net.set_row(&1, 9);
net.set_col(&1, 0);
net.set_col(&1, 9);
net.set_box(&1, (3, 3), (5, 7));
println!["{:?}", net];
}
You can use any element in TileNet
as long as it has the following traits:
extern crate tile_net;
use tile_net::*;
#[derive(Clone, Debug, Default)]
struct Example(i32);
fn main() {
let mut net: TileNet<Example> = TileNet::new(10, 10); net.set_row(&Example(1), 0); net.set_row(&Example(2), 9);
net.set_col(&Example(3), 0);
net.set_col(&Example(4), 9);
net.set_box(&Example(5), (3, 3), (5, 7));
println!["{:?}", net]; }
Collision Detection
TileNet
is not used for drawing tiles to a grid, its main focus is continuous, tile-vertex
collision detection.
Continuous collision detection (CCD) prevents objects tunneling through other objects in a
frame. This happens
when we only check the beginning and end points of an object's movement. This library
interpolates on each
tile. So every intermediate tile is checked. Let's see an example.
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set_row(&1, 0);
net.set_row(&2, 9);
net.set_col(&3, 0);
net.set_col(&4, 9);
net.set_box(&5, (3, 3), (5, 7));
println!["{:?}", net];
let mut collider = MyObject::new();
let supercover = collider.tiles(); let tiles = net.collide_set(supercover);
if collider.resolve(tiles, &mut ()) {
println!["Able to move"];
} else {
println!["Unable to move"];
}
}
#[derive(Debug)]
struct MyObject {
pts: Vec<(f32, f32)>,
pos: Vector,
mov: Vector,
}
impl MyObject {
fn new() -> MyObject {
MyObject {
pts: vec![(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)],
pos: Vector(1.1, 1.1),
mov: Vector(100.0, 100.0),
}
}
}
impl Collable<usize, ()> for MyObject {
fn points<'a>(&'a self) -> Points<'a> {
Points::new(self.pos, &self.pts)
}
fn queued(&self) -> Vector {
self.mov
}
fn resolve<'a, I>(&mut self, mut set: TileSet<'a, usize, I>, _state: &mut ()) -> bool
where I: Iterator<Item = (i32, i32)>
{
if set.all(|x| *x == 0) { self.pos = self.pos + self.mov;
self.mov = Vector(0.0, 0.0);
true
} else if self.mov.norm2sq() > 1e-6 { self.mov.scale(0.9);
false
} else { true
}
}
}
What you can do with resolve
is to run it in a loop. After scaling down the movement vector
sufficiently in resolve
, you may end up with a TileSet
that does not cause collision.
This is how we can almost perfectly find the position.
You may employ other methods inside resolve. Whatever suits your needs.
Here is the example again but this time we resolve the collision using a loop
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set_row(&1, 0);
net.set_row(&2, 9);
net.set_col(&3, 0);
net.set_col(&4, 9);
net.set_box(&5, (3, 3), (5, 7));
println!["{:?}", net];
let mut collider = MyObject::new();
loop {
let supercover = collider.tiles();
let tiles = net.collide_set(supercover);
if collider.resolve(tiles, &mut ()) {
println!["Able to move"];
break;
} else {
println!["Unable to move"];
}
}
println!["{:?}", collider];
}
#[derive(Debug)]
struct MyObject {
pts: Vec<(f32, f32)>,
pos: Vector,
mov: Vector,
}
impl MyObject {
fn new() -> MyObject {
MyObject {
pts: vec![(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)],
pos: Vector(1.1, 1.1),
mov: Vector(100.0, 100.0),
}
}
}
impl Collable<usize, ()> for MyObject {
fn points<'a>(&'a self) -> Points<'a> {
Points::new(self.pos, &self.pts)
}
fn queued(&self) -> Vector {
self.mov
}
fn resolve<'a, I>(&mut self, mut set: TileSet<'a, usize, I>, _state: &mut ()) -> bool
where I: Iterator<Item = (i32, i32)>
{
if set.all(|x| *x == 0) { self.pos = self.pos + self.mov;
self.mov = Vector(0.0, 0.0);
true } else if self.mov.norm2sq() > 1e-6 { self.mov.scale(0.9);
false } else {
true
}
}
}
You can try to use more nuanced methods instead of scaling down and checking again. One method
may be to check the first collision point and scale down to the distance thereof.
Everything is iterator based.
TileView
For drawing you may want to avoid sending huge grids to the GPU, so we use a view from the grid.
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set_row(&1, 0);
net.set_row(&2, 9);
net.set_col(&3, 0);
net.set_col(&4, 9);
net.set_box(&5, (3, 3), (5, 7));
println!["{:?}", net];
for element in net.view_box((0, 4, 3, 6)) {
let (value, col, row) = element;
println!["{}-{} = {}", row, col, value];
}
for element in net.view_all() {
let (value, col, row) = element;
println!["{}-{} = {}", row, col, value];
}
for element in net.view_center((3, 3), (4, 2)) {
let (value, col, row) = element;
println!["{}-{} = {}", row, col, value];
}
for element in net.view_center_f32((3.0, 3.0), (4, 2)) {
let (value, col, row) = element;
println!["{}-{} = {}", row, col, value];
}
}
Ergonomics
Instead of using a manual loop, you can use the built-in solve
. Which calls presolve
,
runs a loop around resolve
, and then calls postsolve
with bools denoting whether a
solution was found and at least a single collision was encountered.
extern crate tile_net;
use tile_net::*;
fn main() {
let mut net: TileNet<usize> = TileNet::new(10, 10);
net.set_row(&1, 0);
net.set_row(&2, 9);
net.set_col(&3, 0);
net.set_col(&4, 9);
net.set_box(&5, (3, 3), (5, 7));
println!["{:?}", net];
let mut collider = MyObject::new();
collider.solve(&net, &mut ()); println!["{:?}", collider];
}
#[derive(Debug)]
struct MyObject {
pts: Vec<(f32, f32)>,
pos: Vector,
mov: Vector,
}
impl MyObject {
fn new() -> MyObject {
MyObject {
pts: vec![(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)],
pos: Vector(1.1, 1.1),
mov: Vector(100.0, 100.0),
}
}
}
impl Collable<usize, ()> for MyObject {
fn points<'a>(&'a self) -> Points<'a> {
Points::new(self.pos, &self.pts)
}
fn queued(&self) -> Vector {
self.mov
}
fn postsolve(&mut self, _collided_once: bool, resolved: bool, _state: &mut ()) {
if resolved {
println!["Able to move"];
} else {
println!["Unable to move"];
}
}
fn resolve<'a, I>(&mut self, mut set: TileSet<'a, usize, I>, _state: &mut ()) -> bool
where I: Iterator<Item = (i32, i32)>
{
if set.all(|x| *x == 0) { self.pos = self.pos + self.mov;
self.mov = Vector(0.0, 0.0);
true } else if self.mov.norm2sq() > 1e-6 { self.mov.scale(0.9);
false } else {
true
}
}
}
State
You may have seen the state
variables in presolve
, solve
, and postsolve
.
You can put arbitrary information in this variable. It allows you to communicate between
the three stages and outside of the solve
call.
State is appropriate whenever there exists a property that is not supposed to be part of
the Collable
that you are implementing. In addition to making your Collable
cleaner,
you also avoid redundant information stored in your objects.
See the examples directory for an example where we use presolve and postsolve
to find out if our object can jump or not.