src/PhysicsObject.hh
changeset 428 712b943195a6
parent 427 01e77fe8c040
equal deleted inserted replaced
427:01e77fe8c040 428:712b943195a6
    28 
    28 
    29 /**
    29 /**
    30  * PhysicsObject class. Represents an object in the physics simulation.
    30  * PhysicsObject class. Represents an object in the physics simulation.
    31  */
    31  */
    32 class PhysicsObject {
    32 class PhysicsObject {
    33 public:
    33 protected:
    34     /** Reference to PhysicsWorld. */
    34     /** Reference to PhysicsWorld. */
    35     PhysicsWorld &world;
    35     PhysicsWorld &world;
    36 
    36 
       
    37 private:
       
    38     /** Position */
       
    39     Vector position;
       
    40 
       
    41     /** Position at previous tick */
       
    42     Vector previousPosition;
       
    43 
       
    44     /** Velocity */
       
    45     Vector velocity;
       
    46 
       
    47     /** Mass */
       
    48     float mass; 
       
    49  
       
    50     /** Object's shape, polygon vertices */
       
    51     std::vector<Vector> shape;
       
    52 
       
    53     /** Force queue, emptied on every physics tick */
       
    54     std::queue<std::pair<Force, TimeMS> > forceq;
       
    55 
       
    56 
    37 protected:
    57 protected:
    38     /** Object position. */
       
    39     Vector position;
       
    40 
       
    41     /** Object position on previous physics tick. */
       
    42     Vector previousPosition;
       
    43 
       
    44     /** Object velocity */
       
    45     Vector velocity;
       
    46 
       
    47     /** Object mass. */
       
    48     float mass;
       
    49 
       
    50     /** Tells if the object is "on the ground" */
    58     /** Tells if the object is "on the ground" */
    51     bool inAir;
    59     bool inAir;
    52 
    60 
    53     /** Object elasticity. */
    61     /** Object elasticity */
    54     float collision_elasticity;
    62     float collision_elasticity;
    55 
    63 
    56     /** Aim angle in radians. */
    64     /** Aim angle in radians */
    57     float aim; 
    65     float aim; 
    58 
    66 
    59     /** Player facing. */
    67     /** Player facing */
    60     FacingDirection facing; 
    68     FacingDirection facing; 
    61 
    69 
    62     /** Specifies if the object should be simulated */
    70     /** Specifies if the object should be simulated */
    63     bool alive;
    71     bool alive;
    64 
    72 
   109     /**
   117     /**
   110      * Set player facing.
   118      * Set player facing.
   111      *
   119      *
   112      * @param facingRight True if player is facing right.
   120      * @param facingRight True if player is facing right.
   113      */
   121      */
   114     void setFacing (FacingDirection facing);
   122     void setFacing (FacingDirection facing) {
       
   123         this->facing = facing;
       
   124     }
   115 
   125 
   116     /**
   126     /**
   117      * Makes the player jump in the air.
   127      * Makes the player jump in the air.
   118      *
   128      *
   119      * @param direction -1: jump left, 0: jump up, 1: jump right
   129      * @param direction -1: jump left, 0: jump up, 1: jump right
   150      * its movements will not be calculated.
   160      * its movements will not be calculated.
   151      */
   161      */
   152     void disable (void);
   162     void disable (void);
   153 
   163 
   154 private:
   164 private:
   155     /** Objects shape. Edgepoints of the polygon */
       
   156     std::vector<Vector> shape;
       
   157 
       
   158     /** Object force queue. The queue is emptied on every physics
       
   159         tick.*/
       
   160     std::queue<std::pair<Force, TimeMS> > forceq;
       
   161 
       
   162     /**
   165     /**
   163      * Handle player movement and apply forces.
   166      * Handle player movement and apply forces.
   164      *
   167      *
   165      * @param dt Time intervall
   168      * @param dt Time intervall
   166      */
   169      */
   182      * @param force Force
   185      * @param force Force
   183      * @param dt Time
   186      * @param dt Time
   184      * @param d Previous derivative
   187      * @param d Previous derivative
   185      * @return Derivative
   188      * @return Derivative
   186      */
   189      */
   187     Derivative evaluate (Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick);
   190     Derivative evaluate (Force force, TimeMS dt, const Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick);
   188 
   191 
   189     /**
   192     /**
   190      * Return object acceleration with given force.
   193      * Return object acceleration with given force.
   191      *
   194      *
   192      * @param force Force
   195      * @param force Force
   219     /**
   222     /**
   220      * Get object type.
   223      * Get object type.
   221      *
   224      *
   222      * @return Object type
   225      * @return Object type
   223      */
   226      */
   224     ObjectType getType (void) const;
   227     ObjectType getType (void) const {
       
   228         return type;
       
   229     }
   225 
   230 
   226     /**
   231     /**
   227      * Checks if it is possible for the object to be in the given
   232      * Checks if it is possible for the object to be in the given
   228      * location.
   233      * location.
   229      *
   234      *
   234     /**
   239     /**
   235      * Get current object position.
   240      * Get current object position.
   236      *
   241      *
   237      * @return position vector
   242      * @return position vector
   238      */
   243      */
   239     Vector getPosition (void) const;
   244     Vector getPosition (void) const {
       
   245         return position;
       
   246     }
   240 
   247 
   241     /**
   248     /**
   242      * Get previous object position.
   249      * Get previous object position.
   243      *
   250      *
   244      * @return position vector
   251      * @return position vector
   245      */
   252      */
   246     Vector getPreviousPosition (void) const;
   253     Vector getPreviousPosition (void) const {
       
   254         return previousPosition;
       
   255     }
   247 
   256 
   248     /**
   257     /**
   249      * Get current object screen coordinates
   258      * Get current object screen coordinates
   250      *
   259      *
   251      * @return PixelCoordinate position
   260      * @return PixelCoordinate position
   255     /**
   264     /**
   256      * Get current object velocity.
   265      * Get current object velocity.
   257      *
   266      *
   258      * @return Velocity vector
   267      * @return Velocity vector
   259      */
   268      */
   260     Vector getVelocity (void) const;
   269     Vector getVelocity (void) const {
   261 
   270         return velocity;
   262 
   271     }
   263     /**
       
   264      * Return object shape.
       
   265      *
       
   266      * @return Polygon points
       
   267      */
       
   268     const std::vector<Vector>& getShape (void) const;
       
   269 
       
   270     /**
       
   271      * Set object shape.
       
   272      *
       
   273      * @param shape Vector containing polygon points
       
   274      */
       
   275     void setShape(std::vector<Vector> shape);
       
   276 
   272 
   277     /**
   273     /**
   278      * Return object facing.
   274      * Return object facing.
   279      *
   275      *
   280      * @return Object facing (true if facing right)
   276      * @return Object facing (true if facing right)
   281      */
   277      */
   282     FacingDirection getFacing (void) const;
   278     FacingDirection getFacing (void) const {
       
   279         return facing;
       
   280     }
   283 
   281 
   284     /**
   282     /**
   285      * Return object aim angle.
   283      * Return object aim angle.
   286      *
   284      *
   287      * @return Object aim angle
   285      * @return Object aim angle
   288      */
   286      */
   289     float getAim (void) const;
   287     float getAim (void) const {
       
   288         return aim;
       
   289     }
   290 
   290 
   291     /**
   291     /**
   292      * Get object direction.
   292      * Get object direction.
   293      *
   293      *
   294      * @return Unit vector to facing+aim
   294      * @return Unit vector to facing+aim
   303     /**
   303     /**
   304      * Check if the object is alive.
   304      * Check if the object is alive.
   305      *
   305      *
   306      * @return Is the object alive?
   306      * @return Is the object alive?
   307      */
   307      */
   308     bool isAlive (void);
   308     bool isAlive (void) const {
       
   309         return alive;
       
   310     }
   309     
   311     
   310     /**
   312     /**
   311      * Tells the state of the object.
   313      * Tells the state of the object.
   312      *
   314      *
   313      * @return True if object has been destroyed.
   315      * @return True if object has been destroyed.
   317     /**
   319     /**
   318      * Set object pivot.
   320      * Set object pivot.
   319      *
   321      *
   320      * @param pivot Pivot object
   322      * @param pivot Pivot object
   321      */
   323      */
   322     void setPivot (PhysicsObject *pivot);
   324     void setPivot (PhysicsObject *pivot) {
       
   325         this->pivot = pivot;
       
   326     }
   323 
   327 
   324     /**
   328     /**
   325      * Return the pivot object pointer.
   329      * Return the pivot object pointer.
   326      */
   330      */
   327     PhysicsObject *getPivot (void);
   331     PhysicsObject *getPivot (void) {
       
   332         return pivot;
       
   333     }
       
   334 
       
   335     /**
       
   336      * Compute the force that this object (as a pivot) exerts on the given object
       
   337      *
       
   338      * @param bob Othe object
       
   339      * @return Force
       
   340      */
       
   341     virtual Vector getPivotForce (void);
   328 
   342 
   329     /**
   343     /**
   330      * Checks if object collides with other objects
   344      * Checks if object collides with other objects
   331      *
   345      *
   332      * @param obj Other PhysicsObject
   346      * @param obj Other PhysicsObject
   333      * @return Did we collide?
   347      * @return Did we collide?
   334      */
   348      */
   335     bool collides (const PhysicsObject &obj);
   349     bool collides (const PhysicsObject &obj);
   336 
   350 
   337     /**
   351     /**
   338      * Compute the force that this object (as a pivot) exerts on the given object
       
   339      *
       
   340      * @param bob Othe object
       
   341      * @return Force
       
   342      */
       
   343     virtual Vector getPivotForce (void);
       
   344 
       
   345     /**
       
   346      * Update object in physics simulation.
   352      * Update object in physics simulation.
   347      *
   353      *
   348      * @param tick_length Length of the physics tick
   354      * @param tick_length Length of the physics tick
   349      */
   355      */
   350     virtual void tick (TimeMS tick_length);
   356     virtual void tick (TimeMS tick_length);
   351 
   357 
   352 protected:
   358 protected:
   353     /**
   359     /**
       
   360      * Set object shape.
       
   361      *
       
   362      * XXX: constructor
       
   363      *
       
   364      * @param shape Vector containing polygon points
       
   365      */
       
   366     void setShape(std::vector<Vector> shape) {
       
   367         this->shape = shape;
       
   368     }
       
   369 
       
   370     /**
   354      * Update object position, also updating our previous position
   371      * Update object position, also updating our previous position
   355      *
   372      *
   356      * @param pos new position
   373      * @param pos new position
   357      */
   374      */
   358     void setPosition (Vector pos);
   375     void setPosition (Vector pos);
   360     /**
   377     /**
   361      * Update current object velocity.
   378      * Update current object velocity.
   362      *
   379      *
   363      * @param velocity new velocity
   380      * @param velocity new velocity
   364      */
   381      */
   365     void setVelocity (Vector velocity);
   382     void setVelocity (Vector velocity) {
       
   383         this->velocity = velocity;
       
   384     }
       
   385 
       
   386     /**
       
   387      * Reset state and disable, ready to be resume()'d again in a different place
       
   388      */
       
   389     void reset (void);
       
   390 
       
   391     /**
       
   392      * Resume after a reset() at the given position with a zero velocity
       
   393      */
       
   394     void resume (Vector position);
   366 };
   395 };
   367 
   396 
   368 /** Helper struct for the integration */
   397 /** Helper struct for the integration */
   369 struct Derivative {
   398 struct Derivative {
   370     Vector dx; // Velocity
   399     /**
   371     Vector dv; // Acceleration
   400      * Velocity
       
   401      */
       
   402     Vector dx;
       
   403 
       
   404     /**
       
   405      * Acceleration
       
   406      */ 
       
   407     Vector dv; 
       
   408     
       
   409     Derivative () : dx(), dv() { }
       
   410     Derivative (Vector dx, Vector dv) : dx(dx), dv(dv) { }
   372 };
   411 };
   373 
   412 
   374 /**
   413 /**
   375  * Returns the "sign" of the cross product between given points. In
   414  * Returns the "sign" of the cross product between given points. In
   376  * practice the sign of the return value tels on which side of the
   415  * practice the sign of the return value tels on which side of the