--- a/src/proto2/Physics.cc Fri Nov 28 15:27:51 2008 +0000
+++ b/src/proto2/Physics.cc Fri Nov 28 15:45:30 2008 +0000
@@ -22,7 +22,7 @@
}
void PhysicsWorld::tick () {
-// Engine::log(DEBUG, "physics.apply_force") << "*tick*";
+ // Engine::log(DEBUG, "physics.apply_force") << "*tick*";
for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
(*i)->tick();
@@ -36,7 +36,7 @@
}
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity)
- : world(world), mass(mass), position(position), velocity(velocity), inAir(true), facing(3) {
+ : world(world), mass(mass), position(position), velocity(velocity), inAir(true), facing(3) {
world.addObject(this);
}
@@ -60,7 +60,7 @@
if(possibleLocation(cursor)) {
reached = cursor;
continue;
- } else {
+ } else {
// Can't get any further
return reached;
}
@@ -76,7 +76,7 @@
if(depth >= max) {
// We can start a free fall now
- //
+ //
// TODO it should be set inAir if it falls from a cliff
this->inAir = true;
@@ -134,10 +134,8 @@
posAfterTick = position;
velAfterTick = velocity;
while (!forceq.empty()) {
- Engine::log(DEBUG, "PhysicObject.updatePosition") << "Forceq length: " << forceq.front();
total += forceq.front();
forceq.pop();
- // Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Current position: " << posAfterTick;
}
// TODO: This is _ugly_ (but not so ugly as the last one I think)
@@ -155,7 +153,7 @@
Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/;
this->velocity = velAfterTick;
- /*
+
// Collision detection
bool collided = false;
@@ -165,13 +163,13 @@
while ((position-reached).length() < diffVec.length()) {
// Check if any of the shapes points collide
- for (int i = 0; i < shape.size(); i ++) {
+ for (uint64_t i = 0; i < shape.size(); i ++) {
if (world.getType(reached+shape[i]) != EMPTY) { // Collision
reached = reached - unitVector; // Return to last point
collided = true;
this->bounce(world.getNormal(reached+shape[i], reached-unitVector+shape[i]));
this->velocity *= COLLISION_ELASTICITY;
- if (abs(this->velocity.x) < PLAYER_MIN_SPEED && (abs(this->velocity.y) < PLAYER_MIN_SPEED)) {
+ if (this->velocity.length() < PLAYER_MIN_SPEED) {
this->inAir = false;
this->velocity = Vector(0,0);
}
@@ -182,45 +180,46 @@
break;
reached += unitVector;
}
- */
-
-
- bool collided = false;
-
- //goes 1 unit forward every step and check if has hit anything
- Vector unitVector = (newPosition-position) / (newPosition-position).length();
-
- Vector tmpVector = position;
- Vector reached = position;
-
- int steps = (int) (newPosition-position).length() + 2;
-
- //Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition;
- //Vector foo = position+unitVector*steps-newPosition;
- //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo;
+
- for(int i = 0; i < steps; i++) {
- tmpVector += unitVector;
+ /*
+ bool collided = false;
- float minVelocity = 10;
- // Check if any of the four corners of the worm collide
- for(int sh = 0; sh < 4; sh++) {
- if(world.getType(tmpVector+shape[sh]) != EMPTY) {
- reached = position + unitVector*(i-1);
- collided = true;
- this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh]));
- this->velocity *= 0.4;
- if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
- this->inAir = false;
- this->velocity = Vector(0,0);
- }
- break;
- }
- }
- if(collided)
- break;
- }
+ //goes 1 unit forward every step and check if has hit anything
+ Vector unitVector = (newPosition-position) / (newPosition-position).length();
+ Vector tmpVector = position;
+ Vector reached = position;
+
+ int steps = (int) (newPosition-position).length() + 2;
+
+ //Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition;
+ //Vector foo = position+unitVector*steps-newPosition;
+ //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo;
+
+ for(int i = 0; i < steps; i++) {
+ tmpVector += unitVector;
+
+ float minVelocity = 10;
+ // Check if any of the four corners of the worm collide
+ for(int sh = 0; sh < 4; sh++) {
+ if(world.getType(tmpVector+shape[sh]) != EMPTY) {
+ reached = position + unitVector*(i-1);
+ collided = true;
+ this->bounce(world.getNormal(tmpVector+shape[sh], tmpVector-unitVector+shape[sh]));
+ this->velocity *= 0.4;
+ if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
+ this->inAir = false;
+ this->velocity = Vector(0,0);
+ }
+ break;
+ }
+ }
+ if(collided)
+ break;
+ }
+ */
+
// In case of some float error check the final coordinate
if(!collided) {
if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY)
@@ -274,15 +273,15 @@
Vector PhysicsWorld::getNormal (Vector hitPoint, Vector prevPoint) {
// Search free points with bfs and put them to vector
std::vector<Vector> frees;
- Vector hit = Vector((int)hitPoint.x, (int)hitPoint.y);
- Vector prev = Vector((int)prevPoint.x, (int)prevPoint.y);
+ Vector hit(hitPoint);
+ Vector prev(prevPoint);
assert(hit != prev);
int dirIdx = getDirectionIndex(prev-hit);
//float tmp1 = hit.x-prev.x;
//float tmp2 = hit.y-prev.y;
-// Engine::log(DEBUG, "physics.getNormal ") << dirIdx << " " << tmp1 << " " << tmp2;
+ // Engine::log(DEBUG, "physics.getNormal ") << dirIdx << " " << tmp1 << " " << tmp2;
for(int i = 1; i <= 2; i++) {
if(getType(hit+DIRECTIONS[(dirIdx+i) % 8]) == EMPTY)
@@ -319,8 +318,8 @@
}
/*bool PhysicsWorld::collided (Vector oldPos, Vector newPos) {
- return false;
-}*/
+ return false;
+ }*/
/**
* Integrates given force over time and stores new position to
@@ -361,8 +360,8 @@
}
void PhysicsObject::applyForce (Force force) {
- // Add applied force to the queue
- forceq.push(force);
+ // Add applied force to the queue
+ forceq.push(force);
}
void PhysicsObject::changeAim(float da) {
@@ -401,7 +400,7 @@
}
void PhysicsObject::setShape (std::vector<Vector> shape) {
- this->shape = shape;
+ this->shape = shape;
}
void PhysicsObject::tick () {
@@ -448,8 +447,8 @@
for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) {
for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) {
//if((x-midx) * (x-midx) + (y-midy) * (y-midy) < range*range) {
- // and sets it to type
- terrain[x][y] = type;
+ // and sets it to type
+ terrain[x][y] = type;
//}
}
}
--- a/src/proto2/Physics.hh Fri Nov 28 15:27:51 2008 +0000
+++ b/src/proto2/Physics.hh Fri Nov 28 15:45:30 2008 +0000
@@ -44,17 +44,53 @@
public:
+ /**
+ * Adds objects to the physicsworld.
+ *
+ * @param object Pointer to PhysicsObject to add.
+ */
void addObject (PhysicsObject *object);
-
+
+ /**
+ * Advance one time step in physics simulation.
+ */
void tick (void);
+ /**
+ * Get current tick in physics simulation.
+ */
uint32_t getTick (void);
+
+ /**
+ * Generate random terrain.
+ */
void generateTerrain (int seed);
- bool collided (Vector oldPos, Vector newPos);
+ /**
+ * TODO: We'll have to think about this.
+ */
+ bool collided (Vector prevPos, Vector newPos);
+
+ /**
+ * Return a normal for the wall that has been hit.
+ *
+ * @param hitPoint The point of the wall that has been hit.
+ * @param prevPoint The point from where we were coming.
+ */
Vector getNormal(Vector hitPoint, Vector prevPoint);
+ /**
+ * Return terrain type in specific position.
+ *
+ * @param x x-coordinate
+ * @param y y-coordinate
+ */
TerrainType getType(int x, int y) const;
+ /**
+ * Return terrain type in specific position.
+ *
+ * @param pos Position vector
+ */
TerrainType getType(Vector pos) const;
void removeGround(int x, int y, float r);