本文整理汇总了C++中Collision::getTimeOfCollision方法的典型用法代码示例。如果您正苦于以下问题:C++ Collision::getTimeOfCollision方法的具体用法?C++ Collision::getTimeOfCollision怎么用?C++ Collision::getTimeOfCollision使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Collision
的用法示例。
在下文中一共展示了Collision::getTimeOfCollision方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: update
/*
This is where all game physics starts each frame. It is called each frame
by the game statem manager after player input and AI have been processed. It
updates the physical state of all dynamic objects in the game and
moves all objects to their end of frame positions, updates all necessary
object velocities, and calls all collision event handlers.
*/
void Physics::update(Game *game)
{
// WE'LL USE A CONTINUOUS COLLISION SYSTEM TO ENSURE TEMPORAL
// COHERENCE, WHICH MEANS WE'LL MAKE SURE COLLISIONS ARE RESOLVED
// IN THE ORDER WITH WHICH THEY HAPPEN. WE DON'T WANT GAME EVENTS
// TO APPEAR TO HAPPEN IN THE WRONG ORDER. WE'LL TRY TO MAKE IT
// A LITTLE MORE EFFICIENT BY EMPLOYING A VARIATION ON THE
// SWEEP AND PRUNE ALGORITHM FOR DYNAMIC-DYNAMIC OBJECT COLLISIONS
// IN CASE WE'RE DOING THE ONE UPDATE STEP AT A TIME
// THIS MAKES SURE THE UPDATE DOESN'T GET CALLED AGAIN
// NEXT FRAME WITHOUT THE USER EXPLICITY REQUESTING IT
// BY PRESSING THE 'T' KEY (for Time sTep)
activatedForSingleUpdate = false;
// WE'LL NEED THE WORLD TO ACCESS THE SPRITES AND WORLD LAYERS
GameStateManager *gsm = game->getGSM();
World *world = gsm->getWorld();
// NOTE THAT WE MAKE SURE THE activeCollisions VECTOR IS
// EMPTIED BEFORE THIS METHOD EXITS, SO WE CAN ASSUME
// IT'S EMPTY NOW. activeCollisions CONTAINS ALL THE COLLISIONS
// DETECTED SO FAR THIS FRAME. THESE ARE THE THINGS WE MUST
// RESOLVE.
// START THE CLOCK AT 0, THAT MEANS 0% OF THE WAY THROUGH THE FRAME.
// NOTE THAT TIME 0 IS THE MOST DANGEROUS TIME FOR DETECTING COLLISIONS
// BECAUSE THEY CAN BE EASILY OVERLOOKED. THE SAME FOR SIMULTANEOUS
// COLLISIONS. TO MINIMIZE RIGID BODY PENETRATION, SUCH CIRCUMSTANCES
// ARE TYPICALLY HANDLED AS SPECIAL CASES
currentCollisionTime = 0.0f;
// FIRST WE NEED TO DO COLLISION TESTING PREP WORK FOR SPRITES
// APPLY ACCELERATION AND GRAVITY TO VELOCITY
// INIT TILE COLLISION INFO
// SET ON TILE LAST FRAME USING ON TILE THIS FRAME
// SET ON TILE THIS FRAME TO FALSE
// GET COLLISIONS WITH ALL TILES TO HAPPEN DURING THIS FRAME
// PUT THESE COLLISIONS INTO A SORTABLE DATA STRUCTURE
// INIT SPRITE'S SWEPT SHAPE INFO
// FOR ALL SPRITES, INCLUDING THE BOTS AND PLAYER
vector<CollidableObject*>::iterator spritesIt = sortedSweptShapes[LEFT_EDGE]->begin();
while (spritesIt != sortedSweptShapes[LEFT_EDGE]->end())
{
CollidableObject *sprite = (*spritesIt);
prepSpriteForCollisionTesting(world, sprite);
getAllTileCollisionsForAGivenSprite(world, sprite, 1.0f);
spritesIt++;
}
// PREPARE FOR SPRITE-SPRITE COLLISION TESTING
// SWEEP AND PRUNE DATA STRUCTURES PREP WORK
// SORT S_AND_P VECTOR SORTED BY START X OF SWEPT SHAPE
// SORT S_AND_P VECTOR SORTED BY END X OF SWEPT SHAPE
// WE DON'T NEED THE Y-AXIS SORTED, BUT WOULD IF THIS
// WERE A 3D SYSTEM TO SAVE ON COMPARISONS.
// WE'RE USING C++'s STL sort METHOD AND ARE PROVIDING
// A CUSTOM MEANS FOR COMPARISON
sort(sortedSweptShapes[LEFT_EDGE]->begin(), sortedSweptShapes[LEFT_EDGE]->end(), SweptShapesComparitorByLeft());
sort(sortedSweptShapes[RIGHT_EDGE]->begin(), sortedSweptShapes[RIGHT_EDGE]->end(), SweptShapesComparitorByRight());
// RECORD SORTED POSITIONS WITH EACH SPRITE. THEY NEED TO KNOW WHERE
// THEY ARE IN THOSE DATA STRUCTURES SUCH THAT WE CAN JUMP INTO
// THOSE DATA STRUCTURES TO TEST COLLISIONS WITH NEIGHBORS
updateSweptShapeIndices();
// YOU'LL NEED TO TEST FOR SPRITE-TO-SPRITE COLLISIONS HERE
// *** LOOP STARTS HERE. WE'LL DO THIS UNTIL THERE ARE NO
// MORE COLLISIONS TO RESOLVE FOR THIS FRAME
while (activeCollisions.size() > 0)
{
// SORT COLLISION OBJECTS BY TIME OF COLLISION
// NOTE THAT I'M JUST EMPLOYING THE STL'S List
// CLASS' SORT METHOD BY PROVIDING MY OWN
// MEANS FOR COMPARING Collision OBJECTS
activeCollisions.sort(CollisionComparitor());
// GET FIRST COLLISION - NOTE THAT WE HAVE THE COLLISIONS SORTED
// IN DESCENDING ORDER, SO TO TAKE THE EARLIEST ONE, WE REMOVE IT
// FOM THE BACK OF THE SORTED LIST
Collision *earliestCollision = activeCollisions.back();
activeCollisions.pop_back();
float collisionTime = earliestCollision->getTimeOfCollision();
// MOVE ALL SPRITES UP TO TIME OF FIRST COLLISION USING
// APPROPRIATELY SCALED VELOCITIES
moveAllSpritesUpToBufferedTimeOfCollision(earliestCollision);
// AND ADVANCE COLLISION TIME
//.........这里部分代码省略.........