[Orxonox-commit 5137] r9802 - code/trunk/src/modules/tetris

jo at orxonox.net jo at orxonox.net
Thu Nov 21 22:11:27 CET 2013


Author: jo
Date: 2013-11-21 22:11:27 +0100 (Thu, 21 Nov 2013)
New Revision: 9802

Modified:
   code/trunk/src/modules/tetris/Tetris.cc
   code/trunk/src/modules/tetris/Tetris.h
Log:
Making the Tetris collision detection more robust, by decoupling stone-stone collisions and stone-brick collisions.

Modified: code/trunk/src/modules/tetris/Tetris.cc
===================================================================
--- code/trunk/src/modules/tetris/Tetris.cc	2013-11-21 20:26:33 UTC (rev 9801)
+++ code/trunk/src/modules/tetris/Tetris.cc	2013-11-21 21:11:27 UTC (rev 9802)
@@ -182,8 +182,11 @@
     }
 
 
-
-    bool Tetris::isValidStonePosition(TetrisStone* stone, const Vector3& position)
+    /**
+     * @brief Returns true, if NO collision was detected.
+     * Else returns false and the active brick is repositioned as side-effect.
+     */
+    bool Tetris::checkStoneStoneCollision(TetrisStone* stone, const Vector3& position)
     {
         assert(stone);
 
@@ -192,7 +195,6 @@
         {
             //Vector3 currentStonePosition = rotateVector((*it)->getPosition(), this->activeBrick_->getRotationCount());
             const Vector3& currentStonePosition = (*it)->getPosition(); //!< Saves the position of the currentStone
-            //!< Saves the position of the currentStone
 
             //filter out cases where the falling stone is already below a steady stone
             if(position.y < currentStonePosition.y - this->center_->getStoneSize()/2.0f)
@@ -207,7 +209,16 @@
             }// This case applies if the stones overlap partially vertically
         }
 
-        // after we checked for collision with all stones, we also check for collision with the bottom
+        return true;
+    }
+    
+    /**
+     * @brief Returns true, if NO collision was detected.
+     * Else returns false and the active brick is repositioned as side-effect.
+     */
+    bool Tetris::checkStoneBottomCollision(TetrisStone* stone, const Vector3& position)
+    {
+        assert(stone);
         if(position.y < this->center_->getStoneSize()/2.0f) //!< If the stone has reached the bottom of the level
         {
             float baseOffset = abs(stone->getPosition().y);
@@ -219,9 +230,9 @@
             this->activeBrick_->setPosition(Vector3(this->activeBrick_->getPosition().x, yOffset, this->activeBrick_->getPosition().z));
             return false;
         }
-
         return true;
     }
+    
     /**
      * @brief This function determines wether a brick touches another brick or the ground.
      *
@@ -237,12 +248,22 @@
         {
             TetrisStone* stone = brick->getStone(i);
             const Vector3& stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount());
-            if(! this->isValidStonePosition(stone, brickPosition + stonePosition) )
+            if(! this->checkStoneStoneCollision(stone, brickPosition + stonePosition) )
             {
-                // recurse because all stones have to checked again after the brick was re-positioned
                 return false;
             }
         }
+        // check all stones in the brick
+        for (unsigned int i = 0; i < brick->getNumberOfStones(); i++ )
+        {
+            TetrisStone* stone = brick->getStone(i);
+            const Vector3& stonePosition = rotateVector(stone->getPosition(), brick->getRotationCount());
+            if(! this->checkStoneBottomCollision(stone, brickPosition + stonePosition) )
+            {
+                return false;
+            }
+        }
+        
         return true;
     }
 

Modified: code/trunk/src/modules/tetris/Tetris.h
===================================================================
--- code/trunk/src/modules/tetris/Tetris.h	2013-11-21 20:26:33 UTC (rev 9801)
+++ code/trunk/src/modules/tetris/Tetris.h	2013-11-21 21:11:27 UTC (rev 9802)
@@ -82,7 +82,8 @@
             void startBrick(void);
             void createBrick(void);
             void cleanup(void); //!< Cleans up the Gametype by destroying the ball and the bats.
-            bool isValidStonePosition(TetrisStone* stone, const Vector3& position);
+            bool checkStoneStoneCollision(TetrisStone* stone, const Vector3& position);
+            bool checkStoneBottomCollision(TetrisStone* stone, const Vector3& position);
             bool isValidBrickPosition(TetrisBrick* brick);
             void findFullRows(void);
             void clearRow(unsigned int row);




More information about the Orxonox-commit mailing list