src/PhysicsObject.hh
changeset 427 01e77fe8c040
parent 423 947ab54de4b7
child 428 712b943195a6
equal deleted inserted replaced
426:510c83aab425 427:01e77fe8c040
    35     PhysicsWorld &world;
    35     PhysicsWorld &world;
    36 
    36 
    37 protected:
    37 protected:
    38     /** Object position. */
    38     /** Object position. */
    39     Vector position;
    39     Vector position;
       
    40 
    40     /** Object position on previous physics tick. */
    41     /** Object position on previous physics tick. */
    41     Vector previousPosition;
    42     Vector previousPosition;
       
    43 
    42     /** Object velocity */
    44     /** Object velocity */
    43     Vector velocity;
    45     Vector velocity;
       
    46 
    44     /** Object mass. */
    47     /** Object mass. */
    45     float mass;
    48     float mass;
       
    49 
    46     /** Tells if the object is "on the ground" */
    50     /** Tells if the object is "on the ground" */
    47     bool inAir;
    51     bool inAir;
       
    52 
    48     /** Object elasticity. */
    53     /** Object elasticity. */
    49     float collision_elasticity;
    54     float collision_elasticity;
    50 
    55 
    51     // Attributes for players
       
    52     /** Aim angle in radians. */
    56     /** Aim angle in radians. */
    53     float aim; 
    57     float aim; 
       
    58 
    54     /** Player facing. */
    59     /** Player facing. */
    55     FacingDirection facing; 
    60     FacingDirection facing; 
    56 
    61 
    57     /** Specifies if the player is alive (or dead). */
    62     /** Specifies if the object should be simulated */
    58     bool alive;
    63     bool alive;
    59     /** True if player object should be removed from the game. */
    64 
       
    65     /** True if !alive, and the object can be delete'd */
    60     bool shouldDelete;
    66     bool shouldDelete;
    61     
    67     
    62     /** Type of the object */
    68     /** Type of the object */
    63     ObjectType type;
    69     ObjectType type;
    64 
    70 
    65     /** Pivot object for this object. */
    71     /** Pivot object for this object */
    66     PhysicsObject *pivot;
    72     PhysicsObject *pivot;
    67 
    73 
    68     /** 
    74     /** 
    69      * Class constructor
    75      * Class constructor
    70      *
    76      *
    76      * @param collision_elasticity Object elasticity
    82      * @param collision_elasticity Object elasticity
    77      * @param enabled Is the object currently part of the simulation
    83      * @param enabled Is the object currently part of the simulation
    78      */
    84      */
    79     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, 
    85     PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity, ObjectType type, 
    80             float collision_elasticity, bool enabled = true);
    86             float collision_elasticity, bool enabled = true);
       
    87     
       
    88     /**
       
    89      * Virtual destructor
       
    90      */
    81     virtual ~PhysicsObject (void);
    91     virtual ~PhysicsObject (void);
    82 
       
    83 
    92 
    84     /**
    93     /**
    85      * Apply a force to the object. The force is applied to the object
    94      * Apply a force to the object. The force is applied to the object
    86      * on the next physics tick.
    95      * on the next physics tick.
    87      *
    96      *
    88      * @param force Force vector
    97      * @param force Force vector
    89      */
    98      */
    90     void applyForce(Force force, TimeMS dt = PHYSICS_TICK_MS);
    99     void applyForce (Force force, TimeMS dt = PHYSICS_TICK_MS);
    91 
   100 
    92     /**
   101     /**
    93      * Change player aim. This function takes care that aim angle
   102      * Change player aim. This function takes care that aim angle
    94      * stays inside limits.
   103      * stays inside limits.
    95      *
   104      *
    96      * @param da Aim angle change in radians
   105      * @param da Aim angle change in radians
    97      */
   106      */
    98     void changeAim(float da);
   107     void changeAim (float da);
    99    
   108    
   100     /**
   109     /**
   101      * Set player facing.
   110      * Set player facing.
   102      *
   111      *
   103      * @param facingRight True if player is facing right.
   112      * @param facingRight True if player is facing right.
   190      *
   199      *
   191      * @param right Boolean describing the movement direction.
   200      * @param right Boolean describing the movement direction.
   192      * @return New position
   201      * @return New position
   193      */
   202      */
   194     void walk (TimeMS, bool right);
   203     void walk (TimeMS, bool right);
       
   204 
       
   205     /*
       
   206      * Undocumented private method
       
   207      */
   195     Vector walk_one_step (float, bool);
   208     Vector walk_one_step (float, bool);
   196 
   209 
   197 public:
   210 public:
   198     /**
   211     /**
   199      * Define object behaviour on collisions.
   212      * Define object behaviour on collisions.
   219     bool possibleLocation (Vector loc);
   232     bool possibleLocation (Vector loc);
   220 
   233 
   221     /**
   234     /**
   222      * Get current object position.
   235      * Get current object position.
   223      *
   236      *
   224      * @return Position vector
   237      * @return position vector
   225      */
   238      */
   226     Vector getPosition (void) const;
   239     Vector getPosition (void) const;
   227 
   240 
   228     /**
   241     /**
   229      * Set previous object position.
       
   230      *
       
   231      * @param Position vector
       
   232      */
       
   233     void setPosition (Vector pos);
       
   234 
       
   235     /**
       
   236      * Get previous object position.
   242      * Get previous object position.
   237      *
   243      *
   238      * @return Position vector
   244      * @return position vector
   239      */
   245      */
   240     Vector getPreviousPosition (void) const;
   246     Vector getPreviousPosition (void) const;
   241 
   247 
   242     /**
   248     /**
   243      * Get current object screen coordinates
   249      * Get current object screen coordinates
   251      *
   257      *
   252      * @return Velocity vector
   258      * @return Velocity vector
   253      */
   259      */
   254     Vector getVelocity (void) const;
   260     Vector getVelocity (void) const;
   255 
   261 
       
   262 
   256     /**
   263     /**
   257      * Return object shape.
   264      * Return object shape.
   258      *
   265      *
   259      * @return Polygon points
   266      * @return Polygon points
   260      */
   267      */
   270     /**
   277     /**
   271      * Return object facing.
   278      * Return object facing.
   272      *
   279      *
   273      * @return Object facing (true if facing right)
   280      * @return Object facing (true if facing right)
   274      */
   281      */
   275     FacingDirection getFacing() const;
   282     FacingDirection getFacing (void) const;
   276 
   283 
   277     /**
   284     /**
   278      * Return object aim angle.
   285      * Return object aim angle.
   279      *
   286      *
   280      * @return Object aim angle
   287      * @return Object aim angle
   281      */
   288      */
   282     float getAim() const;
   289     float getAim (void) const;
   283 
   290 
   284     /**
   291     /**
   285      * Get object direction.
   292      * Get object direction.
   286      *
   293      *
   287      * @return Unit vector to facing+aim
   294      * @return Unit vector to facing+aim
   339      * Update object in physics simulation.
   346      * Update object in physics simulation.
   340      *
   347      *
   341      * @param tick_length Length of the physics tick
   348      * @param tick_length Length of the physics tick
   342      */
   349      */
   343     virtual void tick (TimeMS tick_length);
   350     virtual void tick (TimeMS tick_length);
       
   351 
       
   352 protected:
       
   353     /**
       
   354      * Update object position, also updating our previous position
       
   355      *
       
   356      * @param pos new position
       
   357      */
       
   358     void setPosition (Vector pos);
       
   359 
       
   360     /**
       
   361      * Update current object velocity.
       
   362      *
       
   363      * @param velocity new velocity
       
   364      */
       
   365     void setVelocity (Vector velocity);
   344 };
   366 };
   345 
   367 
   346 /** Helper struct for the integration */
   368 /** Helper struct for the integration */
   347 struct Derivative {
   369 struct Derivative {
   348     Vector dx; // Velocity
   370     Vector dx; // Velocity