Rope has forces
authorekku
Sat, 06 Dec 2008 21:02:35 +0000
changeset 228 dbc1bb7a98b5
parent 227 39cd6861e43e
child 229 355e46effa41
Rope has forces
src/PhysicsObject.cc
src/PhysicsObject.hh
src/Player.hh
src/Rope.cc
src/Rope.hh
--- a/src/PhysicsObject.cc	Sat Dec 06 20:56:24 2008 +0000
+++ b/src/PhysicsObject.cc	Sat Dec 06 21:02:35 2008 +0000
@@ -114,6 +114,18 @@
     // Add gravity to the force queue
     forceq.push(world.gravity);
     
+    // If the object (practically player) has a pivot point add
+    // a force towards that
+    if (pivot != NULL) {
+        Vector direction(pivot->getPosition()-position);
+        float magnitude = pivot->getPivotForce(this);
+        float length = direction.length();
+        if (length > 0) {
+            direction = direction / length * magnitude;
+            forceq.push(direction);
+        }
+    }
+
     // Go trough every force in the queue
     Force total;
     while (!forceq.empty()) {
@@ -322,6 +334,10 @@
     this->shape = shape;
 }
 
+void PhysicsObject::setPivot (PhysicsObject *pivot) {
+    this->pivot = pivot;
+}
+
 void PhysicsObject::tick (TimeMS tick_length) {
     this->updatePosition(tick_length);
 }
@@ -356,6 +372,8 @@
     }
 }
 
+float PhysicsObject::getPivotForce (PhysicsObject *bob) { return 0.0; }
+
 void PhysicsObject::draw(CL_GraphicContext *gc) {
     CL_Quad player(
                    (position+shape[0]).x, (position+shape[0]).y,
--- a/src/PhysicsObject.hh	Sat Dec 06 20:56:24 2008 +0000
+++ b/src/PhysicsObject.hh	Sat Dec 06 21:02:35 2008 +0000
@@ -34,6 +34,8 @@
     bool alive;
     bool shouldDelete;
 
+    PhysicsObject *pivot;
+
     PhysicsObject(PhysicsWorld &world, float mass, Vector position, 
                   Vector velocity, bool enabled = true);
     virtual ~PhysicsObject (void);
@@ -219,6 +221,16 @@
     bool removeIfDestroyed (void);
 
     /**
+     * Sets this object's pivot to the given value, which will then exert a force on this object
+     */
+    void setPivot (PhysicsObject *pivot);
+
+    /**
+     * Compute the force that this object (as a pivot) exerts on the given object
+     */
+    virtual float getPivotForce (PhysicsObject *bob);
+
+    /**
      * Update object in physics simulation.
      */
     virtual void tick (TimeMS tick_length);
--- a/src/Player.hh	Sat Dec 06 20:56:24 2008 +0000
+++ b/src/Player.hh	Sat Dec 06 21:02:35 2008 +0000
@@ -13,6 +13,8 @@
 #include <vector>
 
 class Player : public PhysicsObject {
+    friend class Rope;
+
     public:
         GameState &state;
 
--- a/src/Rope.cc	Sat Dec 06 20:56:24 2008 +0000
+++ b/src/Rope.cc	Sat Dec 06 21:02:35 2008 +0000
@@ -29,19 +29,34 @@
 
 void Rope::onCollision() {
     this->rs = FIXED;
+
+    // stop movement
     disable();
+
+    // set player's pivot
+    pl.setPivot(this);
 }
 
 void Rope::release (void) {
     // TODO make it fly first and fold only 
     // after it's close to the player
     this->rs = FOLDED;
+    
+    // player doesn't have a pivot anymore
+    pl.setPivot(NULL);
 }
 
 RopeState Rope::getState (void) {
     return this->rs;
 }
 
+float Rope::getPivotForce (PhysicsObject *bob) {
+    if ( (this->position - pl.getPosition()).length() >= this->length)
+        return 2000;
+    else
+        return 0;
+}
+
 void Rope::draw (CL_GraphicContext *gc) const {
     gc->draw_line((int)(this->pl.getPosition().x), (int)(this->pl.getPosition().y)
         , (int)(this->position.x), (int)(this->position.y), CL_Color::black);
--- a/src/Rope.hh	Sat Dec 06 20:56:24 2008 +0000
+++ b/src/Rope.hh	Sat Dec 06 21:02:35 2008 +0000
@@ -17,6 +17,10 @@
     float length;
     virtual void onCollision (void);
     RopeState rs;
+    
+protected:
+    virtual float getPivotForce (PhysicsObject *bob);
+
 public:
     Rope(Player &climber);