Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9802 for code/trunk


Ignore:
Timestamp:
Nov 21, 2013, 10:11:27 PM (11 years ago)
Author:
jo
Message:

Making the Tetris collision detection more robust, by decoupling stone-stone collisions and stone-brick collisions.

Location:
code/trunk/src/modules/tetris
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/tetris/Tetris.cc

    r9801 r9802  
    183183
    184184
    185 
    186     bool Tetris::isValidStonePosition(TetrisStone* stone, const Vector3& position)
     185    /**
     186     * @brief Returns true, if NO collision was detected.
     187     * Else returns false and the active brick is repositioned as side-effect.
     188     */
     189    bool Tetris::checkStoneStoneCollision(TetrisStone* stone, const Vector3& position)
    187190    {
    188191        assert(stone);
     
    193196            //Vector3 currentStonePosition = rotateVector((*it)->getPosition(), this->activeBrick_->getRotationCount());
    194197            const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
    195             //!< Saves the position of the currentStone
    196198
    197199            //filter out cases where the falling stone is already below a steady stone
     
    208210        }
    209211
    210         // after we checked for collision with all stones, we also check for collision with the bottom
     212        return true;
     213    }
     214   
     215    /**
     216     * @brief Returns true, if NO collision was detected.
     217     * Else returns false and the active brick is repositioned as side-effect.
     218     */
     219    bool Tetris::checkStoneBottomCollision(TetrisStone* stone, const Vector3& position)
     220    {
     221        assert(stone);
    211222        if(position.y < this->center_->getStoneSize()/2.0f) //!< If the stone has reached the bottom of the level
    212223        {
     
    220231            return false;
    221232        }
    222 
    223233        return true;
    224234    }
     235   
    225236    /**
    226237     * @brief This function determines wether a brick touches another brick or the ground.
     
    238249            TetrisStone* stone = brick->getStone(i);
    239250            const Vector3& stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount());
    240             if(! this->isValidStonePosition(stone, brickPosition + stonePosition) )
    241             {
    242                 // recurse because all stones have to checked again after the brick was re-positioned
     251            if(! this->checkStoneStoneCollision(stone, brickPosition + stonePosition) )
     252            {
    243253                return false;
    244254            }
    245255        }
     256        // check all stones in the brick
     257        for (unsigned int i = 0; i < brick->getNumberOfStones(); i++ )
     258        {
     259            TetrisStone* stone = brick->getStone(i);
     260            const Vector3& stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount());
     261            if(! this->checkStoneBottomCollision(stone, brickPosition + stonePosition) )
     262            {
     263                return false;
     264            }
     265        }
     266       
    246267        return true;
    247268    }
  • code/trunk/src/modules/tetris/Tetris.h

    r9667 r9802  
    8383            void createBrick(void);
    8484            void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
    85             bool isValidStonePosition(TetrisStone* stone, const Vector3& position);
     85            bool checkStoneStoneCollision(TetrisStone* stone, const Vector3& position);
     86            bool checkStoneBottomCollision(TetrisStone* stone, const Vector3& position);
    8687            bool isValidBrickPosition(TetrisBrick* brick);
    8788            void findFullRows(void);
Note: See TracChangeset for help on using the changeset viewer.