src/proto2/Physics.cc
changeset 54 b8b043ba0abd
parent 52 8db6726c27fc
child 58 a53f5ad69500
equal deleted inserted replaced
53:a76ddb2e39fb 54:b8b043ba0abd
     3 
     3 
     4 #include "Physics.hh"
     4 #include "Physics.hh"
     5 
     5 
     6 
     6 
     7 PhysicsWorld::PhysicsWorld (Vector dimensions)
     7 PhysicsWorld::PhysicsWorld (Vector dimensions)
     8     : dimensions(dimensions), tick_timer(PHYSICS_TICK_MS) {
     8     : tick_timer(PHYSICS_TICK_MS), dimensions(dimensions) {
     9 
     9 
    10     slots.connect(tick_timer.sig_timer(), this, PhysicsWorld::tick);
    10     slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
    11     tick_timer.enable();
    11     tick_timer.enable();
    12 }
    12 }
    13 
    13 
    14 void PhysicsWorld::addObject (PhysicsObject *object) {
    14 void PhysicsWorld::addObject (PhysicsObject *object) {
    15     objects.push_back(object);
    15     objects.push_back(object);
    32         
    32         
    33     Vector newPosition = position + velocity * PHYSICS_TICK_MS * 1000;
    33     Vector newPosition = position + velocity * PHYSICS_TICK_MS * 1000;
    34 
    34 
    35     //TODO Handle the object as a square or a polygon
    35     //TODO Handle the object as a square or a polygon
    36 
    36 
    37     if(newPosition.x() < 0 || (newPosition.x() > PHYSICS_WORLD_WIDTH)
    37     if(newPosition.x < 0 || (newPosition.x > PHYSICS_WORLD_WIDTH)
    38        || (newPosition.y() < 0) || (newPosition.y() >= PHYSICS_WORLD_HEIGHT)) {
    38        || (newPosition.y < 0) || (newPosition.y >= PHYSICS_WORLD_HEIGHT)) {
    39         
    39         
    40         // CRASH!
    40         // CRASH!
    41         this->velocity *= -1;
    41         this->velocity *= -1;
    42 
    42 
    43     } else {
    43     } else {
    48 void PhysicsObject::applyForce (Vector force) {
    48 void PhysicsObject::applyForce (Vector force) {
    49     this->velocity += force / mass * PHYSICS_TICK_MS * 1000;  // The last factor denotes the time.
    49     this->velocity += force / mass * PHYSICS_TICK_MS * 1000;  // The last factor denotes the time.
    50     // It should be scaled somehow.
    50     // It should be scaled somehow.
    51 }
    51 }
    52 
    52 
    53 void PhysicsObjectu::updatePhysics (Vector position, Vector velocity, Vector force) {
    53 void PhysicsObject::updatePhysics (Vector position, Vector velocity, Vector force) {
    54     this->position = position;
    54     this->position = position;
    55     this->velocity = velocity;
    55     this->velocity = velocity;
    56     this->force = force;
    56     this->force = force;
    57 }
    57 }
    58     
    58