예제 #1
0
void Physics::getAllTileCollisionsForAGivenSprite(	World *world,  
													CollidableObject *sprite,
													float percentageOfFrameRemaining)
{
	vector<WorldLayer*> *layers = world->getLayers();
	AABB *boundingVolume = sprite->getBoundingVolume();
	PhysicalProperties *pp = sprite->getPhysicalProperties();

	for (int i = 0; i < world->getNumLayers(); i++)
	{
		WorldLayer *layer = layers->at(i);
		if (layer->hasCollidableTiles())
			layer->findTileCollisionsForSprite(this, sprite);
	}
}
예제 #2
0
/*
	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)
{

	GameStateManager *gsm = game->getGSM();
	SpriteManager *spm = gsm->getSpriteManager();
	AnimatedSprite *player = spm->getPlayer();

	if (once == 0) {

	
	/*	b2PolygonShape groundBox;
		groundBox.SetAsBox(50.0f, 10.0f);

		groundBody->CreateFixture(&groundBox, 0.0f);
*/
		b2BodyDef bodyDef;
		bodyDef.type = b2_dynamicBody;
		bodyDef.position.Set(600, 9280);
		body = world->CreateBody(&bodyDef);

		b2PolygonShape dynamicBox;
		dynamicBox.SetAsBox(32.0f, 32.0f);

		b2FixtureDef fixtureDef;
		fixtureDef.shape = &dynamicBox;


		// Set the box density to be non-zero, so it will be dynamic.
		fixtureDef.density = 1.0f;

		// Override the default friction.
		fixtureDef.friction = 0.3f;

		// Add the shape to the body.
		body->CreateFixture(&fixtureDef);
		//body->SetLinearVelocity(b2Vec2(0,-1000));

		//body->SetUserData(player);
		player->setBody(body);
		// This is our little game loop.

		


		once += 1;
	}

	body->ApplyForce( world->GetGravity(), body->GetWorldCenter(), true );

			float32 timeStep = 1.0f / 60.0f;
			int32 velocityIterations = 6;
			int32 positionIterations = 2;

			world->Step(timeStep, velocityIterations, positionIterations);

			// Now print the position and angle of the body.
			b2Vec2 position = body->GetPosition();
			//b2Vec2 position2 = body2->GetPosition();

			float32 angle = body->GetAngle();

			printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
		
			if (f == 0) {
				World *woorld = gsm->getWorld();
				vector<WorldLayer*> *layers = woorld->getLayers();
				CollidableObject *sprite = NULL;
				for (int i = 0; i < woorld->getNumLayers(); i++)
				{
					WorldLayer *layer = layers->at(i);
					if (layer->hasCollidableTiles())
					{
						layer->findTileCollisionsForSprite(this,sprite);
					}
				}
				f++;
			}
			
			if (player->getinvincible() == true) {
				if (invincibletimer == 0) {
					invincibletimer = 100;
				}
				invincibletimer -= 1;
				if (invincibletimer == 0) {
					player->setinvincible(false);
				}
			}
			
				 
			if (z!=0)z--;
			if (z==0)
			if (player->getinvincible() == false)
			for (b2ContactEdge* edge = body->GetContactList(); edge; edge = edge->next)
				if (edge->contact->IsTouching()) {
					z=50;

					if(game->getPlayerLife() > 0)
						{game->decreasePlayerLife();
					PlaySound(L"data/sounds/explosion.wav", NULL, SND_FILENAME|SND_SYNC);}
					else 
					{
					if(game->getLives() > 1) {
						game->decreaseLives();
					}						
					else if(game->getLives() == 1 && game->getPlayerLife() == 0)
					{
						GameStateManager *gsm = game->getGSM();
						once = 0;
						//body->SetTransform(b2Vec2((600-position.x),(9280-position.y)),body->GetAngle());
						Viewport *viewport = game->getGUI()->getViewport();
						viewport->setViewportX(0);
						viewport->setViewportY(8880);

						gsm->gameOver();
						PlaySound(L"data/sounds/end.wav", NULL, SND_FILENAME|SND_SYNC);
					}
	
					game->setPlayerLife();
				}

			}

			/*if (f > 0)
			{
				if(game->getPlayerLife() > 0)
				game->decreasePlayerLife();
		
				else 
				{
					if(game->getLives() > 1)
						game->decreaseLives();
					else if(game->getLives() == 1 && game->getPlayerLife() == 0)
					{
						GameStateManager *gsm = game->getGSM();
						gsm->setCurrentGameState(GS_GAME_OVER);
					}
	
					game->setPlayerLife();
				}	
			}*/
			//OutputDebug(L"My output string.");


	//		activatedForSingleUpdate = false;

	//// WE'LL NEED THE WORLD TO ACCESS THE SPRITES AND WORLD LAYERS
	//World *woorld = gsm->getWorld();

	//currentCollisionTime = 0.0f;


	//// 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(woorld, sprite);
	//	getAllTileCollisionsForAGivenSprite(woorld, 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
	//	currentCollisionTime = collisionTime;

	//	// AND UPDATE THE VELOCITIES OF THE SPRITE(S) INVOLVED IN THE COLLISION
	//	performCollisionResponse(earliestCollision);

	//	// EXECUTE COLLISION EVENT CODE
	//	// TEST TO SEE TYPES OF OBJECTS AND APPROPRIATE RESPONSE
	//	// ACCORDING TO CUSTOMIZED COLLISION EVENT HANDLER
	//	collisionListener->respondToCollision(game, earliestCollision);

	//	CollidableObject *co1 = earliestCollision->getCO1();
	//	CollidableObject *co2 = earliestCollision->getCO2();
	//	removeActiveCOCollisions(co1);
 //		co1->updateSweptShape(1.0f - currentCollisionTime);
	//	getAllTileCollisionsForAGivenSprite(woorld, co1, 1.0f - currentCollisionTime);

	//	// ONLY DO IT FOR THE SECOND ONE IF IT'S NOT A TILE
	//	if (!earliestCollision->isCollisionWithTile())
	//	{
	//		removeActiveCOCollisions(co2);
	//		co2->updateSweptShape(1.0f - currentCollisionTime);
	//		getAllTileCollisionsForAGivenSprite(woorld, co2, 1.0f - currentCollisionTime);
	//	}
	//	else
	//	{
	//		spriteToTileCollisionsThisFrame[co1].insert(earliestCollision->getTile());
	//		recycledCollidableObjectsList.push_back(co2);
	//	}
	//	
	//	// IF IT WAS ONLY ONE SPRITE WITH A TILE THIS IS EASY TO DO
	//	if (earliestCollision->isCollisionWithTile())
	//	{
	//		reorderCollidableObject(co1);
	//	}
	//	// YOU'LL HAVE TO WORRY ABOUT REORDERING STUFF FOR COLLISIONS
	//	// BETWEEN TWO SPRITES
	//	
	//	// NOW TEST NEIGHBORS OF SPRITES INVOLVED IN RESOLVED COLLISION
	//	// AGAINST NEIGHBORS IN SWEPT SHAPE DATA STRUCTURES. YOU'LL HAVE
	//	// TO FIGURE OUT HOW TO DO THIS AND HOW TO RESOLVE SUCH COLLISIONS
	//		
	//	// RECYCLE THE COLLISION SINCE WE'RE NOW DONE WITH IT
	//	recycledCollisions.push_back(earliestCollision);
	//}

	//// APPLY THE REMAINING TIME TO MOVE THE SPRITES. NOTE THAT
	//// THIS IS ACTUALLY A VERY RISKY, TRICKY STEP BECAUSE IT COULD
	//// MOVE OBJECTS ALMOST TO THE POINT OF COLLISION, WHICH MAY THEN
	//// BE DETECTED ALMOST AT TIME 0 NEXT FRAME. THERE ARE OTHER TRICKY
	//// ISSUES RELATED TO OUR BUFFER AS WELL, SO WE CHEAT A LITTLE HERE
	//// AND SCALE THE TIME REMAINING DOWN A LITTE
	//if (currentCollisionTime < 1.0f)
	//	moveAllSpritesToEndOfFrame();

	//// INIT TILE COLLISION INFO
	//// SET ON TILE LAST FRAME USING ON TILE THIS FRAME
	//// SET ON TILE THIS FRAME TO FALSE
	//spritesIt = sortedSweptShapes[LEFT_EDGE]->begin();
	//while (spritesIt != sortedSweptShapes[LEFT_EDGE]->end())
	//{
	//	CollidableObject *sprite = (*spritesIt);
	//	sprite->advanceOnTileStatus();
	//	spritesIt++;
	//}

	//// WE'RE NOT GOING TO ALLOW MULTIPLE COLLISIONS TO HAPPEN IN A FRAME
	//// BETWEEN THE SAME TWO OBJECTS
	//spriteToTileCollisionsThisFrame.clear();
	//


}