Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 6, 2011, 12:07:29 PM (13 years ago)
Author:
dafrick
Message:

Merging trunk into dockingsystem branch to be able to use the recent bugfix in MultiTrigger.

Location:
code/branches/dockingsystem
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/dockingsystem

  • code/branches/dockingsystem/src/modules/pong/PongBall.cc

    r7886 r8194  
    2727 */
    2828
     29/**
     30    @file PongBall.cc
     31    @brief Implementation of the PongBall class.
     32*/
     33
    2934#include "PongBall.h"
    3035
    3136#include "core/CoreIncludes.h"
    3237#include "core/GameMode.h"
     38
    3339#include "gametypes/Gametype.h"
     40
    3441#include "PongBat.h"
    3542
     
    4047    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
    4148
     49    /**
     50    @brief
     51        Constructor. Registers and initializes the object.
     52    */
    4253    PongBall::PongBall(BaseObject* creator)
    4354        : MovableEntity(creator)
     
    5768    }
    5869
     70    /**
     71    @brief
     72        Destructor.
     73    */
    5974    PongBall::~PongBall()
    6075    {
     
    6883    }
    6984
     85    /**
     86    @brief
     87        Register variables to synchronize over the network.
     88    */
    7089    void PongBall::registerVariables()
    7190    {
     
    7998    }
    8099
     100    /**
     101    @brief
     102        Is called every tick.
     103        Handles the movement of the ball and its interaction with the boundaries and bats.
     104    @param dt
     105        The time since the last tick.
     106    */
    81107    void PongBall::tick(float dt)
    82108    {
    83109        SUPER(PongBall, tick, dt);
    84110
     111        // Get the current position, velocity and acceleration of the ball.
    85112        Vector3 position = this->getPosition();
    86113        Vector3 velocity = this->getVelocity();
    87114        Vector3 acceleration = this->getAcceleration();
    88115
     116        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
    89117        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
    90118        {
     119            // Its velocity in z-direction is inverted (i.e. it bounces off).
    91120            velocity.z = -velocity.z;
     121            // And its position is set as to not overstep the boundary it has just crossed.
    92122            if (position.z > this->fieldHeight_ / 2)
    93123                position.z = this->fieldHeight_ / 2;
     
    98128        }
    99129
     130        // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry).
    100131        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
    101132        {
    102133            float distance = 0;
    103134
    104             if (this->bat_)
     135            if (this->bat_ != NULL) // If there are bats.
    105136            {
    106                 if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
     137                // If the right boundary has been crossed.
     138                if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL)
    107139                {
     140                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
    108141                    distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
    109                     if (fabs(distance) <= 1)
    110                     {
     142                    if (fabs(distance) <= 1) // If the bat is there to parry.
     143                    {
     144                        // Set the ball to be exactly at the boundary.
    111145                        position.x = this->fieldWidth_ / 2;
     146                        // Invert its velocity in x-direction (i.e. it bounces off).
    112147                        velocity.x = -velocity.x;
     148                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
    113149                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
    114150                        acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1;
     
    116152                        this->fireEvent();
    117153                    }
     154                    // If the left player scores.
    118155                    else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
    119156                    {
     
    125162                    }
    126163                }
    127                 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
     164                // If the left boundary has been crossed.
     165                else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL)
    128166                {
     167                    // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%)
    129168                    distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2);
    130                     if (fabs(distance) <= 1)
    131                     {
     169                    if (fabs(distance) <= 1) // If the bat is there to parry.
     170                    {
     171                        // Set the ball to be exactly at the boundary.
    132172                        position.x = -this->fieldWidth_ / 2;
     173                        // Invert its velocity in x-direction (i.e. it bounces off).
    133174                        velocity.x = -velocity.x;
     175                        // Adjust the velocity in the z-direction, depending on where the ball hit the bat.
    134176                        velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
    135177                        acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1;
     
    137179                        this->fireEvent();
    138180                    }
     181                    // If the right player scores.
    139182                    else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
    140183                    {
     
    149192        }
    150193
     194        // Set the position, velocity and acceleration of the ball, if they have changed.
    151195        if (acceleration != this->getAcceleration())
    152196            this->setAcceleration(acceleration);
     
    157201    }
    158202
     203    /**
     204    @brief
     205        Set the speed of the ball (in x-direction).
     206    @param speed
     207        The speed to be set.
     208    */
    159209    void PongBall::setSpeed(float speed)
    160210    {
    161         if (speed != this->speed_)
     211        if (speed != this->speed_) // If the speed changes
    162212        {
    163213            this->speed_ = speed;
    164214
     215            // Set the speed in the direction of the balls current velocity.
    165216            Vector3 velocity = this->getVelocity();
    166217            if (velocity.x != 0)
    167218                velocity.x = sgn(velocity.x) * this->speed_;
    168             else
     219            else // If the balls current velocity is zero, the speed is set in a random direction.
    169220                velocity.x = this->speed_ * sgn(rnd(-1,1));
    170221
     
    173224    }
    174225
     226    /**
     227    @brief
     228        Set the bats for the ball.
     229    @param bats
     230        An array (of size 2) of weak pointers, to be set as the new bats.
     231    */
    175232    void PongBall::setBats(WeakPtr<PongBat>* bats)
    176233    {
    177         if (this->bDeleteBats_)
     234        if (this->bDeleteBats_) // If there are already some bats, delete them.
    178235        {
    179236            delete[] this->bat_;
     
    182239
    183240        this->bat_ = bats;
     241        // Also store their object IDs, for synchronization.
    184242        this->batID_[0] = this->bat_[0]->getObjectID();
    185243        this->batID_[1] = this->bat_[1]->getObjectID();
    186244    }
    187245
     246    /**
     247    @brief
     248        Get the bats over the network.
     249    */
    188250    void PongBall::applyBats()
    189251    {
    190         if (!this->bat_)
     252        // Make space for the bats, if they don't exist, yet.
     253        if (this->bat_ == NULL)
    191254        {
    192255            this->bat_ = new WeakPtr<PongBat>[2];
Note: See TracChangeset for help on using the changeset viewer.