# HG changeset patch # User ekku # Date 1227198181 0 # Node ID 3274c4804ea52cdf9c258d4011f2b8ea3c6505ae # Parent 04428c5e548cb5a3001740d2d6e44cbc2836e781 Collision and stuff diff -r 04428c5e548c -r 3274c4804ea5 src/proto2/Physics.cc --- a/src/proto2/Physics.cc Thu Nov 20 15:01:17 2008 +0000 +++ b/src/proto2/Physics.cc Thu Nov 20 16:23:01 2008 +0000 @@ -90,6 +90,25 @@ } } +bool PhysicsWorld::collided (Vector oldPos, Vector newPos) { + int deltaX = oldPos.x - newPos.x; + int deltaY = oldPos.y - newPos.y; + double distance = sqrt(deltaX * deltaX + deltaY * deltaY); + double xInc = (double) deltaX / distance; + double yInc = (double) deltaY / distance; + double currentX = oldPos.x; + double currentY = oldPos.y; + + // This implementation is bit slow since it checks some squares twice. + for(unsigned int i = 1; i < distance; i++) { + currentX += xInc; + currentY += yInc; + if(terrain[(int)currentX][(int)currentY] != EMPTY) + return false; + } + return true; +} + void PhysicsObject::integrate(Vector force, uint16_t dt) { // TODO } diff -r 04428c5e548c -r 3274c4804ea5 src/proto2/Physics.hh --- a/src/proto2/Physics.hh Thu Nov 20 15:01:17 2008 +0000 +++ b/src/proto2/Physics.hh Thu Nov 20 16:23:01 2008 +0000 @@ -34,6 +34,8 @@ void addObject (PhysicsObject *object); void tick (void); + void generateTerrain (int seed); + bool collided (Vector oldPos, Vector newPos); }; class PhysicsObject { @@ -42,8 +44,8 @@ float mass; Vector position; Vector velocity; - // Whether the object (worms mainly) is in the air or on the ground. - // Affects to physics. + // Whether the object (worms mainly) is in the air + // or firmly on the ground. Affects to physics. bool inAir; PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);