/* addSpriteToRenderList - This method checks to see if the sprite parameter is inside the viewport. If it is, a RenderItem is generated for that sprite and it is added to the render list. */ void SpriteManager::addSpriteToRenderList(AnimatedSprite *sprite, RenderList *renderList, Viewport *viewport) { // GET THE SPRITE TYPE INFO FOR THIS SPRITE AnimatedSpriteType *spriteType = sprite->getSpriteType(); PhysicalProperties *pp = sprite->getPhysicalProperties(); float rotation = sprite->getRotationInRadians(); // IS THE SPRITE VIEWABLE? if (viewport->areWorldCoordinatesInViewport( pp->getX(), pp->getY(), spriteType->getTextureWidth(), spriteType->getTextureHeight())) { // SINCE IT'S VIEWABLE, ADD IT TO THE RENDER LIST RenderItem itemToAdd; itemToAdd.id = sprite->getFrameIndex(); renderList->addRenderItem( sprite->getCurrentImageID(), pp->round(pp->getX()-viewport->getViewportX()), pp->round(pp->getY()-viewport->getViewportY()), pp->round(pp->getZ()), sprite->getAlpha(), spriteType->getTextureWidth(), spriteType->getTextureHeight(), rotation); } }
void WalkaboutMouseEventHandler::handleMousePressEvent(Game *game, int mouseX, int mouseY) { if (game->getGSM()->isGameInProgress()) { Viewport *viewport = game->getGUI()->getViewport(); // DETERMINE WHERE ON THE MAP WE HAVE CLICKED int worldCoordinateX = mouseX + viewport->getViewportX(); int worldCoordinateY = mouseY + viewport->getViewportY(); SpriteManager *spriteManager = game->getGSM()->getSpriteManager(); Player* player = static_cast<Player*>(spriteManager->getPlayer()); PhysicalProperties* playerPP = spriteManager->getPlayer()->getPhysicalProperties(); if(!player->getIsDead() && !player->getIsDying() && player->getAmmo() != 0) { float dx = worldCoordinateX - playerPP->getX(); float dy = worldCoordinateY - playerPP->getY(); float distanceToMouse = sqrtf(dx*dx + dy*dy); dx /= distanceToMouse; dy /= distanceToMouse; float bulletOffset = 60; float bulletSpeed = 50; //Fire projectile spriteManager->createProjectile(playerPP->getX() + bulletOffset*dx, playerPP->getY() + bulletOffset*dy, bulletSpeed*dx,bulletSpeed*dy); player->decrementAmmo(); } } }
void SpriteManager::addSpriteToRenderList(AnimatedSprite *sprite, RenderList *renderList, Viewport *viewport) { // GET THE SPRITE TYPE INFO FOR THIS SPRITE AnimatedSpriteType *spriteType = sprite->getSpriteType(); PhysicalProperties *pp = sprite->getPhysicalProperties(); if (i >= 3) { b2Body* bods= sprite->getBody(); b2Vec2 position = bods->GetPosition(); pp->setX(position.x); pp->setY(position.y); } i+=1; // IS THE SPRITE VIEWABLE? if (viewport->areWorldCoordinatesInViewport( pp->getX(), pp->getY(), spriteType->getTextureWidth(), spriteType->getTextureHeight()) ) { // SINCE IT'S VIEWABLE, ADD IT TO THE RENDER LIST RenderItem itemToAdd; itemToAdd.id = sprite->getFrameIndex(); if (spriteType->getTextureWidth()==61) { renderList->addRenderItem( sprite->getCurrentImageID(), pp->round(X-viewport->getViewportX()), pp->round(Y-25-viewport->getViewportY()), pp->round(pp->getZ()), sprite->getAlpha(), spriteType->getTextureWidth(), spriteType->getTextureHeight()); } else { renderList->addRenderItem( sprite->getCurrentImageID(), pp->round(pp->getX()-viewport->getViewportX()), pp->round(pp->getY()-viewport->getViewportY()), pp->round(pp->getZ()), sprite->getAlpha(), spriteType->getTextureWidth(), spriteType->getTextureHeight()); } } }
void GameStateManager::updateViewport(Game *game, float time) { AnimatedSprite *p = spriteManager->getPlayer(); PhysicalProperties *pp = p->getPhysicalProperties(); float pVelX = pp->getVelocityX()*time; float pVelY = pp->getVelocityY()*time; //VIEWPORT SCOPING Viewport *vp = game->getGUI()->getViewport(); if(pVelX < 0 && (pp->round(pp->getX() - vp->getViewportX())) < vp->getViewportWidth()/3) { vp->setScrollSpeedX(pVelX); /*if(vp->getViewportX() == 0) vp->setScrollSpeedX(0);*/ } else if(pVelX > 0 && (pp->round(pp->getX() - vp->getViewportX())) > (vp->getViewportWidth()/3*2)) { vp->setScrollSpeedX(pVelX); /*if(vp->getViewportX()+vp->getViewportWidth() == world.getWorldWidth()) vp->setScrollSpeedX(0);*/ } else { vp->setScrollSpeedX(0); } if(pVelY < 0 && (pp->round(pp->getY() - vp->getViewportY())) < vp->getViewportHeight()/3) { vp->setScrollSpeedY(pVelY); /*if(vp->getViewportY() == 0) vp->setScrollSpeedY(0);*/ } else if(pVelY > 0 && (pp->round(pp->getY() - vp->getViewportY())) > (vp->getViewportHeight()/3*2)) { vp->setScrollSpeedY(pVelY); /*if(vp->getViewportY()+vp->getViewportHeight() == world.getWorldHeight()) vp->setScrollSpeedY(0);*/ } else { vp->setScrollSpeedY(0); } vp->moveViewport( vp->getScrollSpeedX(), vp->getScrollSpeedY(), world.getWorldWidth(), world.getWorldHeight()); }
bool Physics::willObjectsCollide(CollidableObject* coA, CollidableObject* coB) { PhysicalProperties* pp = coA->getPhysicalProperties(); BoundingVolume* bv = coA->getBoundingVolume(); float widthA = bv->getWidth(); float heightA = bv->getHeight(); float xA = pp->getX() + bv->getX(); float yA = pp->getY() + bv->getY(); float velXA = pp->getVelocityX(); float velYA = pp->getVelocityY(); float minXA = xA-(widthA/2); float maxXA = xA+(widthA/2); float minYA = yA-(heightA/2); float maxYA = yA+(heightA/2); if(velXA >= 0) maxXA += velXA; if(velXA < 0) minXA += velXA; if(velYA >= 0) maxYA += velYA; if(velYA < 0) minYA += velYA; pp = coB->getPhysicalProperties(); bv = coB->getBoundingVolume(); float widthB = bv->getWidth(); float heightB = bv->getHeight(); float xB = pp->getX() + bv->getX(); float yB = pp->getY() + bv->getY(); float velXB = pp->getVelocityX(); float velYB = pp->getVelocityY(); float minXB = xB-(widthB/2); float maxXB = xB+(widthB/2); float minYB = yB-(heightB/2); float maxYB = yB+(heightB/2); if(velXB >= 0) maxXB += velXB; if(velXB < 0) minXB += velXB; if(velYB >= 0) maxYB += velYB; if(velYB < 0) minYB += velYB; if( !(maxXB < minXA || minXB > maxXA || maxYB < minYA || minYB > maxYA)) return true; return false; }
//////////////////////////KEEP///////////////////////////////////////////// void Physics::addCollidableObject(CollidableObject *collidableObjectToAdd) { PhysicalProperties *pp = collidableObjectToAdd->getPhysicalProperties(); float height = pixelsToMeters(collidableObjectToAdd->getBoundingVolume()->getHeight()) / 2; float width = pixelsToMeters(collidableObjectToAdd->getBoundingVolume()->getWidth()) / 2; float x = pixelsToMeters(pp->getX()); float y = pixelsToMeters(-pp->getY()); // Define the dynamic body. We set its position and call the body factory. b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(x, y); b2Body* body = box2DWorld->CreateBody(&bodyDef); testSubjectBody = body; // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(width, height); // Define the dynamic body fixture. 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); }
void ScienceMouseEventHandler::handleMousePressEvent(Game *game, int mouseX, int mouseY) { if (game->getGSM()->isGameInProgress()) { Viewport *viewport = game->getGUI()->getViewport(); // DETERMINE WHERE ON THE MAP WE HAVE CLICKED int worldCoordinateX = mouseX + viewport->getViewportX(); int worldCoordinateY = mouseY + viewport->getViewportY(); // NOW LET'S SEE IF THERE IS A SPRITE THERE GameStateManager *gsm = game->getGSM(); SpriteManager *spriteManager = gsm->getSpriteManager(); // IF THERE IS NO SELECTED SPRITE LOOK FOR ONE if (!(spriteManager->getIsSpriteSelected())) { // IF THIS DOES NOT RETURN NULL THEN YOU FOUND A SPRITE AT THAT LOCATION if((spriteManager->getSpriteAt(worldCoordinateX, worldCoordinateY) != NULL)) { AnimatedSprite *selected = spriteManager->getSpriteAt(worldCoordinateX, worldCoordinateY); spriteManager->setIsSpriteSelected(true); } } else if (spriteManager->getSelectedSprite() != NULL) { // MOVE A SPRITE IN A DESIRED DIRECTION AnimatedSprite *selected = spriteManager->getSelectedSprite(); PhysicalProperties *pp = selected->getPhysicalProperties(); float spriteX = pp->getX(); float spriteY = pp->getY(); //IF A SPRITE IS WHERE YOU WANT IT THEN STOP IT if (((spriteX > worldCoordinateX - 64) && (spriteX < worldCoordinateX + 64)) && (spriteY > worldCoordinateY - 64) && (spriteY < worldCoordinateY + 64)) { pp->setVelocity(0, 0); } else { float deltaX = worldCoordinateX - spriteX; float deltaY = worldCoordinateY - spriteY; float hyp = sqrtf((deltaX * deltaX) + (deltaY * deltaY)); pp->setVelocity((deltaX / hyp) * 3, (deltaY / hyp) * 3); } spriteManager->setIsSpriteSelected(false); // GridPathfinder *pathfinder = spriteManager->getPathfinder(); // pathfinder->mapPath(selected, (float)worldCoordinateX, (float)worldCoordinateY); // gsm->setSpriteSelected(false, selected); } else { spriteManager->setIsSpriteSelected(false); } } }
/* handleKeyEvent - this method handles all keyboard interactions. Note that every frame this method gets called and it can respond to key interactions in any custom way. Ask the GameInput class for key states since the last frame, which can allow us to respond to key presses, including when keys are held down for multiple frames. */ void BugsKeyEventHandler::handleKeyEvents(Game *game) { // WE CAN QUERY INPUT TO SEE WHAT WAS PRESSED GameInput *input = game->getInput(); // LET'S GET THE PLAYER'S PHYSICAL PROPERTIES, IN CASE WE WANT TO CHANGE THEM GameStateManager *gsm = game->getGSM(); AnimatedSprite *player = gsm->getSpriteManager()->getPlayer(); PhysicalProperties *pp = player->getPhysicalProperties(); Viewport *viewport = game->getGUI()->getViewport(); // IF THE GAME IS IN PROGRESS if (gsm->isGameInProgress()) { if (input->isKeyDownForFirstTime(D_KEY)) { viewport->toggleDebugView(); game->getGraphics()->toggleDebugTextShouldBeRendered(); } if (input->isKeyDownForFirstTime(L_KEY)) { game->getGraphics()->togglePathfindingGridShouldBeRendered(); } if (input->isKeyDownForFirstTime(F_KEY)) { game->getGraphics()->togglePathfindingPathShouldBeRendered(); } if (input->isKeyDownForFirstTime(R_KEY)) { if (wcscmp(player->getCurrentState().c_str(), L"RUNNING") == 0) player->setCurrentState(L"WALKING"); else if ((wcscmp(player->getCurrentState().c_str(), L"IDLE") != 0) && (wcscmp(player->getCurrentState().c_str(), L"DANCING") != 0)) player->setCurrentState(L"RUNNING"); } if (input->isKeyDownForFirstTime(Y_KEY)){ player->setCurrentState(DYING); } if (input->isKeyDownForFirstTime(I_KEY)){ player->setCurrentState(IDLE); } bool viewportMoved = false; float viewportVx = 0.0f; float viewportVy = 0.0f; float vX = pp->getVelocityX(); float vY = pp->getVelocityY(); if (input->isKeyDown(UP_KEY)) { if (pp->getY() < (viewport->getViewportY() + 0.5f * viewport->getViewportHeight())) viewportVy -= MAX_PLAYER_VELOCITY; vY = -1 * MAX_PLAYER_VELOCITY; vX = 0; viewportMoved = true; } else if (input->isKeyDown(DOWN_KEY)) { if (pp->getY() > (viewport->getViewportY() + 0.5f * viewport->getViewportHeight())) viewportVy += MAX_PLAYER_VELOCITY; vY = MAX_PLAYER_VELOCITY; vX = 0; viewportMoved = true; } else if (input->isKeyDown(LEFT_KEY)) { if (pp->getX() < (viewport->getViewportX() + 0.5f * viewport->getViewportWidth())) viewportVx -= MAX_PLAYER_VELOCITY; vX = -1 * MAX_PLAYER_VELOCITY; vY = 0; viewportMoved = true; } else if (input->isKeyDown(RIGHT_KEY)) { if (pp->getX() > (viewport->getViewportX() + 0.5f * viewport->getViewportWidth())) viewportVx += MAX_PLAYER_VELOCITY; vX = MAX_PLAYER_VELOCITY; vY = 0; viewportMoved = true; } else { vX = 0.0f; vY = 0.0f; } if (viewportMoved) viewport->moveViewport((int)floor(viewportVx+0.5f), (int)floor(viewportVy+0.5f), game->getGSM()->getWorld()->getWorldWidth(), game->getGSM()->getWorld()->getWorldHeight()); pp->setVelocity(vX, vY); } // 0X43 is HEX FOR THE 'C' VIRTUAL KEY // THIS CHANGES THE CURSOR IMAGE if ((input->isKeyDownForFirstTime(C_KEY)) && input->isKeyDown(VK_SHIFT)) { Cursor *cursor = game->getGUI()->getCursor(); unsigned int id = cursor->getActiveCursorID(); id++; if (id == cursor->getNumCursorIDs()) id = 0; cursor->setActiveCursorID(id); } // LET'S MESS WITH THE TARGET FRAME RATE IF THE USER PRESSES THE HOME OR END KEYS WindowsTimer *timer = (WindowsTimer*)game->getTimer(); int fps = timer->getTargetFPS(); // THIS SPEEDS UP OUR GAME LOOP AND THUS THE GAME, NOTE THAT WE COULD ALTERNATIVELY SCALE // DOWN THE GAME LOGIC (LIKE ALL VELOCITIES) AS WE SPEED UP THE GAME. THAT COULD PROVIDE // A BETTER PLAYER EXPERIENCE if (input->isKeyDown(VK_HOME) && (fps < MAX_FPS)) timer->setTargetFPS(fps + FPS_INC); // THIS SLOWS DOWN OUR GAME LOOP, BUT WILL NOT GO BELOW 5 FRAMES PER SECOND else if (input->isKeyDown(VK_END) && (fps > MIN_FPS)) timer->setTargetFPS(fps - FPS_INC); }
void Physics::update(Game *game) { // REMEMBER, AT THIS POINT, ALL PLAYER INPUT AND AI // HAVE ALREADY BEEN PROCESSED AND BOT AND PLAYER // STATES, VELOCITIES, AND ACCELERATIONS HAVE ALREADY // BEEN UPDATED. NOW WE HAVE TO PROCESS THE PHYSICS // OF ALL THESE OBJECTS INTERACTING WITH EACH OTHER // AND THE STATIC GAME WORLD. THIS MEANS WE NEED TO // DETECT AND RESOLVE COLLISIONS IN THE ORDER THAT // THEY WILL HAPPEN, AND WITH EACH COLLISION, EXECUTE // ANY GAMEPLAY RESPONSE CODE, UPDATE VELOCITIES, AND // IN THE END, UPDATE POSITIONS // FIRST, YOU SHOULD START BY ADDING ACCELERATION TO ALL // VELOCITIES, WHICH INCLUDES GRAVITY, NOTE THE EXAMPLE // BELOW DOES NOT DO THAT // FOR NOW, WE'LL JUST ADD THE VELOCITIES TO THE // POSITIONS, WHICH MEANS WE'RE NOT APPLYING GRAVITY OR // ACCELERATION AND WE ARE NOT DOING ANY COLLISION // DETECTION OR RESPONSE float timer = 0; GameStateManager *gsm = game->getGSM(); SpriteManager *sm = gsm->getSpriteManager(); World *w = gsm->getWorld(); GameRules* gR = game->getGameRules(); vector<WorldLayer*> *layers = w->getLayers(); AnimatedSprite *player; PhysicalProperties *pp; TiledLayer *tL; list<Collision*> collisions; //finding TileLayer for(unsigned int i = 0; i < layers->size(); i++) { WorldLayer *currentLayer = (*layers)[i]; if(currentLayer->hasCollidableTiles() == true) { tL = dynamic_cast<TiledLayer*>(currentLayer); if(tL != 0) { i = layers->size(); }//end if }//end if } player = sm->getPlayer(); pp = player->getPhysicalProperties(); //UPDATING ALL VELOCITIES AND DOING TILE COLLISION pp->incVelocity(this,pp->getAccelerationX(), pp->getAccelerationY() + gravity); collideTestWithTiles(player, tL, &collisions); list<Bot*>::iterator botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->incVelocity(this, pp->getAccelerationX(), pp->getAccelerationY()); if(pp->isGravAffected() == true) pp->incVelocity(this, 0, gravity); collideTestWithTiles(bot, tL, &collisions); botIterator++; } //HERE, COLLIDE SPRITES WITH OTHER SPRITES collideTestWithSprites(game, player, &collisions); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); if(bot->isCurrentlyCollidable() == true); collideTestWithSprites(game, bot, &collisions); botIterator++; } //SORT COLLISIONS collisions.sort(compare_collisionTime); //RESOLVING ALL THE COLLISIONS while(collisions.empty() == false) { Collision* currentCollision = collisions.front(); collisions.pop_front(); float colTime = currentCollision->getTOC(); CollidableObject* co1 = currentCollision->getCO1(); CollidableObject* co2 = currentCollision->getCO2(); if(colTime >= 0 && colTime <= 1) { pp = co1->getPhysicalProperties(); //pp->setVelocity(pp->getVelocityX()*9.99f,pp->getVelocityY()*9.99f); pp = co2->getPhysicalProperties(); //pp->setVelocity(pp->getVelocityX()*9.99f,pp->getVelocityY()*9.99f); pp = player->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(colTime-timer)),pp->getY() + (pp->getVelocityY()*(colTime-timer))); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(colTime-timer)), pp->getY() + (pp->getVelocityY()*(colTime-timer))); botIterator++; } gsm->updateViewport(game, colTime-timer); resolveCollision(game, currentCollision); gR->gameSpecificResolve(game, currentCollision); boolean deleteLast = false; list<Collision*>::iterator cIterator = collisions.begin(); list<Collision*>::iterator lastIterator; while(cIterator != collisions.end()) { if(deleteLast == true) { collisions.erase(lastIterator); } deleteLast = false; Collision* check = (*cIterator); if(check->contains(co1) || check->contains(co2)) { CollidableObject* checkStatic = check->getCO2(); if(checkStatic->isStaticObject()) { coStackCounter ++; coStack[coStackCounter] = checkStatic; } collisionStackCounter ++; collisionStack[collisionStackCounter] = check; lastIterator = cIterator; deleteLast = true; } else { //check->calculateTimes(); } cIterator++; } if(deleteLast == true) { collisions.erase(lastIterator); } collideTestWithTiles(co1, tL, &collisions); collideTestWithSprites(game, co1, &collisions); if(co2->isStaticObject() == false) { collideTestWithTiles(co2, tL, &collisions); collideTestWithSprites(game, co2, &collisions); } collisions.sort(compare_collisionTime); timer += (colTime-timer); }//end if if(co2->isStaticObject() == true) { coStackCounter ++; coStack[coStackCounter] = co2; } collisionStackCounter ++; collisionStack[collisionStackCounter] = currentCollision; } if(timer < 1) { gsm->updateViewport(game, 1-timer); pp = player->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(1-timer)),pp->getY() + (pp->getVelocityY()*(1-timer))); //pp->setVelocity(0.0f, pp->getVelocityY()); botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + (pp->getVelocityX()*(1-timer)), pp->getY() + (pp->getVelocityY()*(1-timer))); botIterator++; } gsm->updateViewport(game, 1-timer); } pp = player->getPhysicalProperties(); if(pp->getX() < 0) { pp->setX(0); } if(pp->getY() < 0) { pp->setY(0); } //pp->setVelocity(0.0f, pp->getVelocityY()); /*pp->setPosition(pp->getX() + pp->getVelocityX(), pp->getY() + pp->getVelocityY()); // FOR NOW THE PLAYER IS DIRECTLY CONTROLLED BY THE KEYBOARD, // SO WE'LL NEED TO TURN OFF ANY VELOCITY APPLIED BY INPUT // SO THE NEXT FRAME IT DOESN'T GET ADDED pp->setVelocity(0.0f, pp->getVelocityY()); // AND NOW MOVE ALL THE BOTS list<Bot*>::iterator botIterator = sm->getBotsIterator(); while (botIterator != sm->getEndOfBotsIterator()) { Bot *bot = (*botIterator); pp = bot->getPhysicalProperties(); pp->setPosition(pp->getX() + pp->getVelocityX(), pp->getY() + pp->getVelocityY()); botIterator++; }*/ }
void Physics::resolveCollision(Game* game, Collision* currentCollision) { CollidableObject* co1 = currentCollision->getCO1(); CollidableObject* co2 = currentCollision->getCO2(); PhysicalProperties* pp; BoundingVolume* bv; AnimatedSprite* player = game->getGSM()->getSpriteManager()->getPlayer(); if(co2->isStaticObject() == true) { pp = co2->getPhysicalProperties(); bv = co2->getBoundingVolume(); float tX = pp->getX(); float tY = pp->getY(); float tXR = tX + bv->getWidth(); float tYB = tY + bv->getHeight(); pp = co1->getPhysicalProperties(); bv = co1->getBoundingVolume(); float x = pp->getX() + bv->getX() - (bv->getWidth()/2); float y = pp->getY() + bv->getY() - (bv->getHeight()/2); float xR = x+bv->getWidth(); float yB = y+bv->getHeight(); //pp->setVelocity(0, 0); /*if(x < tX) pp->setX(pp->getX() - 0.1); if(x > tX) pp->setX(pp->getX() + 0.1);*/ if(x >= tXR) { pp->setX(pp->getX() + 0.1); pp->setVelocity(0, pp->getVelocityY()); } if(xR <= tX) { pp->setX(pp->getX() - 0.1); pp->setVelocity(0, pp->getVelocityY()); } if(y >= tYB) { pp->setY(pp->getY() + 0.1); pp->setVelocity(pp->getVelocityX(), 0); } if(yB <= tY) { pp->setY(pp->getY() - 0.1); if(co1 == player) { pp->setJumped(false); pp->setDoubleJumped(false); pp->setStunned(false); } pp->setVelocity(pp->getVelocityX(), 0); } //if(currentCollision->getTOC() == currentCollision->getSYC()) //{ // /*if(pp->getVelocityY() < 0) // { // pp->setY(pp->getY() + 0.1); // } // else // { // pp->setY(pp->getY() - 0.1); // if(co1 == player) // { // pp->setJumped(false);s // pp->setDoubleJumped(false); // if(player->getCurrentState().compare(L"JUMP_STATE") == 0 // || player->getCurrentState().compare(L"JUMPL_STATE") == 0) // { // player->setCurrentState(L"IDLE_STATE"); // } // } // }*/ // if(y < tY) // { // pp->setY(pp->getY() - 0.1); // if(co1 == player) // { // pp->setJumped(false); // pp->setDoubleJumped(false); // /*if(player->getCurrentState().compare(L"JUMP_STATE") == 0 // || player->getCurrentState().compare(L"JUMPL_STATE") == 0) // { // player->setCurrentState(L"IDLE_STATE"); // }*/ // } // } // if(y > tY) // { // pp->setY(pp->getY() + 0.1); // // } // pp->setVelocity(pp->getVelocityX(), 0); // // //} //else if(currentCollision->getTOC() == currentCollision->getSXC()) //{ // /*if(pp->getVelocityX() < 0) // { // pp->setX(pp->getX() + 0.1); // } // else // { // pp->setX(pp->getX() - 0.1); // }*/ // /*if(x < tX) // pp->setX(pp->getX() - 0.1); // if(x > tX) // pp->setX(pp->getX() + 0.1);*/ // pp->setVelocity(0, pp->getVelocityY()); //} //else //{ // /*if(pp->getVelocityY() < 0) // pp->setY(pp->getY() + 0.1); // else // pp->setY(pp->getY() - 0.1); // if(pp->getVelocityX() < 0) // pp->setX(pp->getX() + 0.1); // else // pp->setX(pp->getX() - 0.1);*/ // pp->setVelocity(0,0); //} } else { pp = co1->getPhysicalProperties(); bv = co1->getBoundingVolume(); float x = pp->getX()+ bv->getX() - (bv->getWidth()/2); float y = pp->getY()+ bv->getY() - (bv->getHeight()/2); float xR = x + bv->getWidth(); float yB = y + bv->getHeight(); PhysicalProperties* p2 = co2->getPhysicalProperties(); bv = co2->getBoundingVolume(); float tX = p2->getX() + bv->getX() - (bv->getWidth()/2); float tY = p2->getY() + bv->getY() - (bv->getHeight()/2); float tXR = tX+bv->getWidth(); float tYB = tY+bv->getHeight(); //pp->setVelocity(0, 0); if(x >= tXR) { if(co1->isMobile()) pp->setX(pp->getX() + 0.1); if(co2->isMobile()) p2->setX(p2->getX() - 0.1); } if(xR <= tX) { if(co1->isMobile()) pp->setX(pp->getX() - 0.1); if(co2->isMobile()) p2->setX(p2->getX() + 0.1); } if(y >= tYB) { if(co1->isMobile()) pp->setY(pp->getY() + 0.1); if(co2->isMobile()) p2->setY(p2->getY() - 0.1); } if(yB <= tY) { if(co1->isMobile()) pp->setY(pp->getY() - 0.1); if(co2->isMobile()) p2->setY(p2->getY() + 0.1); } } }
void Physics::collideTestWithTiles(CollidableObject *c,TiledLayer *tL, list<Collision*> *collisions) { BoundingVolume *bv = c->getBoundingVolume(); float toRight= bv->getWidth()/2; float toBottom = bv->getHeight()/2; PhysicalProperties *pp = c->getPhysicalProperties(); float x = pp->getX()+bv->getX(); float y = pp->getY()+bv->getY(); float xVel = pp->getVelocityX(); float yVel = pp->getVelocityY(); float minX = x - toRight; float maxX = x + toRight; float minY = y - toBottom; float maxY = y + toBottom; if(xVel > 0) maxX += xVel; else minX += xVel; if(yVel > 0) maxY += yVel; else minY += yVel; int tW = tL->getTileWidth(); int tH = tL->getTileHeight(); int firstCol = minX/tW; int lastCol = maxX/tW; int firstRow = minY/tH; int lastRow = maxY/tH; if(firstCol < 0) firstCol = 0; if(firstRow < 0) firstRow = 0; if(lastCol >= tL->getColumns()) lastCol = tL->getColumns() - 1; if(lastRow >= tL->getRows()) lastRow = tL->getRows() - 1; for(int i = firstRow; i <= lastRow; i++) { for(int j = firstCol; j <= lastCol; j++) { Tile* current = tL->getTile(i,j); if(current->collidable == true) { if( !( (i+1)*tH < minY || i*tH > maxY || (j+1)*tW < minX || j*tW > maxX) ) { CollidableObject* tileCO = coStack[coStackCounter]; coStackCounter --; BoundingVolume *bv = tileCO->getBoundingVolume(); bv->setWidth(tW); bv->setHeight(tH); bv->setX(tW/2); bv->setY(tW/2); pp = tileCO->getPhysicalProperties(); pp->setPosition(j*tW,i*tH); /* Collision* currentCollision = collisionStack.top(); collisionStack.pop();*/ Collision* currentCollision = collisionStack[collisionStackCounter]; collisionStackCounter --; currentCollision->setCO1(c); currentCollision->setCO2(tileCO); currentCollision->calculateTimes(); collisions->push_back(currentCollision); } } } } }