Making things simpler. More to come soon.
authorsaiam
Thu, 20 Nov 2008 21:50:55 +0000
changeset 85 351cb6b69c04
parent 84 3cb862028a24
child 86 ed31ece6f340
Making things simpler. More to come soon.
src/proto2/Physics.cc
src/proto2/Physics.hh
--- a/src/proto2/Physics.cc	Thu Nov 20 21:35:17 2008 +0000
+++ b/src/proto2/Physics.cc	Thu Nov 20 21:50:55 2008 +0000
@@ -37,28 +37,19 @@
  */   
 void PhysicsObject::updatePosition () {
     // Add gravity to the force queue
-    forceq.push(Force(world.gravity, PHYSICS_TICK_MS));
+    forceq.push(world.gravity);
     
     // Go trough every force in the queue
     // TODO: It might be possible to optimize by adding forces together
-    std::queue<Force> newfq;
-    Force tmpf;
     Force total;
     posAfterTick = position;
     velAfterTick = velocity;
     while (!forceq.empty()) {
-        tmpf = forceq.front();
-        if (tmpf.dt <= PHYSICS_TICK_MS) { // Force affects only one tick
-            total.force += tmpf.force;
-        } else { // Add remaining time to next tick
-            newfq.push(Force(tmpf.force, tmpf.dt - PHYSICS_TICK_MS));
-            total.force += tmpf.force;
-        }
+        total += forceq.front();
         forceq.pop();
         //        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Current position: " << posAfterTick;
     }
-    integrate(total.force, PHYSICS_TICK_MS);
-    forceq = newfq;
+    integrate(total, PHYSICS_TICK_MS);
 
     Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/;
     this->velocity = velAfterTick;
@@ -134,7 +125,7 @@
  * @param force Force vector.
  * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
  */
-void PhysicsObject::integrate(Vector force, TimeMS dt) {
+void PhysicsObject::integrate(Force force, TimeMS dt) {
     Derivative tmpd;
     Derivative k1 = evaluate(force, 0, tmpd);
     Derivative k2 = evaluate(force, 0.5f*dt, k1);
@@ -151,7 +142,7 @@
     //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
 }
 
-Derivative PhysicsObject::evaluate(Vector force, TimeMS dt, Derivative &d) {
+Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) {
     Vector curPos = posAfterTick + (d.dx*dt)/1000;
     Vector curVel = velAfterTick + (d.dv*dt)/1000;
 
@@ -162,7 +153,7 @@
     return out;
 }
 
-Vector PhysicsObject::acceleration(const Vector &force) {
+Vector PhysicsObject::acceleration(const Force &force) {
     return (force/mass);
 }
 
@@ -172,9 +163,9 @@
  * @param force Force vector.
  * @param dt The time the force is applied.
  */
-void PhysicsObject::applyForce (Vector force, TimeMS dt) {
+void PhysicsObject::applyForce (Force force, TimeMS dt) {
     // Add applied force to the queue
-    forceq.push(Force(force, dt));
+    forceq.push(force);
 }
 
 void PhysicsObject::updatePhysics (Vector position, Vector velocity) {
--- a/src/proto2/Physics.hh	Thu Nov 20 21:35:17 2008 +0000
+++ b/src/proto2/Physics.hh	Thu Nov 20 21:50:55 2008 +0000
@@ -15,7 +15,7 @@
 
 // forward-declare
 class PhysicsObject;
-class Force;
+typedef Vector Force;
 struct Derivative;
 
 class PhysicsWorld {
@@ -63,7 +63,7 @@
 
     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity);
     
-    virtual void applyForce (Vector force, TimeMS dt);
+    virtual void applyForce (Force force, TimeMS dt);
     void updatePhysics (Vector position, Vector velocity);
     
 private:
@@ -72,9 +72,9 @@
     /**
      * Use RK4 to integrate the effects of force over a time intervall.
      */
-    void integrate(Vector force, TimeMS dt);
-    Derivative evaluate(Vector force, TimeMS dt, Derivative &d);
-    Vector acceleration(const Vector &force);
+    void integrate(Force force, TimeMS dt);
+    Derivative evaluate(Force force, TimeMS dt, Derivative &d);
+    Vector acceleration(const Force &force);
 
 public:
     Vector getPosition (void);
@@ -82,15 +82,6 @@
     void tick (void);
 };
 
-class Force {
-public:
-    Vector force;
-    TimeMS dt;
-    
-    Force() {}
-    Force(Vector force, TimeMS dt) : force(force), dt(dt) {}
-};
-
 struct Derivative {
     Vector dx; // Velocity
     Vector dv; // Acceleration