bool CCollider::CollideWith(CCollider& other, const double dt) { if (!this->m_active || !other.GetActive()) // If one of the collider does not collide, no collision will occur, hence false { return false; } if (this->m_type == CT_AABB) { if (other.GetType() == CT_AABB) { // AABB - AABB collision return AABBCollision(other, dt); } else if (other.GetType() == CT_DIST) { // AABB - Dist collision return AABBCollision(other, dt); // Use AABB - AABB collision } } else if (this->m_type == CT_DIST) { if (other.GetType() == CT_AABB) { // Dist - AABB collision return AABBCollision(other, dt); // Use AABB - AABB collision } else if (other.GetType() == CT_DIST) { // Dist - Dist collision return distCollision(other, dt); } } }
void MyGame::processPlayer() { SDL_Point mousePoint = inputSystem.ProcessMouseMove(); Vector2 mousePos = { (double)mousePoint.x, (double)mousePoint.y }; player->changeDirection(map->getDrawLocation(), mousePos); int playerAction; const uint8_t *keysPressed = this->inputSystem.ProcessButtonDown(); SDL_Point mouseClicked = inputSystem.ProcessMouseClick(); //std::cout << "Shift: " << keysPressed[SDL_SCANCODE_LSHIFT] << " W: " << keysPressed[SDL_SCANCODE_W] << std::endl; if (keysPressed[SDL_SCANCODE_LSHIFT] && keysPressed[SDL_SCANCODE_W]) { playerAction = RUN; } else if((player->isAttacking()) || ((mouseClicked.x != -1 && mouseClicked.y != -1) && (keysPressed[SDL_SCANCODE_W]))) { playerAction = ATTACK; } else if (keysPressed[SDL_SCANCODE_W]) { playerAction = WALK; } else if ((player->isAttacking()) || (mouseClicked.x != -1 && mouseClicked.y != -1)) { playerAction = ATTACK; } else { playerAction = STAND_STILL; } for (int i = 0; i < obstacles.size(); i++) { if (AABBCollision(obstacles[i].getCollisionBox(), player->getObjectCollision())) { if ((obstacles[i].getname() == "cage" || obstacles[i].getname() == "wife") && (keyCollected == true)) { gameState = WIN; } else { playerAction = KICK_OUT; } } } if (AABBCollision(keyCollision, player->getObjectCollision()) && this->gameTimer.getCurrentRuntime() >= keyDropTimeout) { keyCollected = true; keyCollision = { -1, -1, -1, -1 }; } player->update(playerAction, this->gameTimer.getCurrentRuntime(), this->gameTimer.getDeltaTime()); }
void PhysicsSystem::SoftBodyCollision_AABB() { for(vector<PhysicsNode*>::iterator i = allNodes.begin(); i != allNodes.end(); ++i) { PhysicsNode *n1 = (*i); CollisionAABB *b1 = new CollisionAABB(n1->GetPosition(),Vector3(n1->GetColliRadius(),n1->GetColliRadius(),n1->GetColliRadius())); int maxX = 0.0f; int minX = 0.0f; int maxY = 0.0f; int minY = 0.0f; int maxZ = 0.0f; int minZ = 0.0f; for(vector<PhysicsNode*>::iterator j = clothNodes.begin(); j != clothNodes.end(); j++) { PhysicsNode *n2 = (*j); if(j == clothNodes.begin()){ maxX = n2->m_position.x; minX = n2->m_position.x; maxY = n2->m_position.y; minY = n2->m_position.y; maxZ = n2->m_position.z; minZ = n2->m_position.z; } if(n2->GetPosition().x > maxX){ maxX = n2->m_position.x; } if(n2->GetPosition().x < minX){ minX = n2->m_position.x; } if(n2->GetPosition().y > maxY){ maxY = n2->m_position.y; } if(n2->GetPosition().y < minY){ minY = n2->m_position.y; } if(n2->GetPosition().z > maxZ){ maxZ = n2->m_position.z; } if(n2->GetPosition().z < minZ){ minZ = n2->m_position.z; } } float originX = (maxX-(maxX-minX)/4); float originY = (maxY-(maxY-minY)/4); float originZ = (maxZ-(maxZ-minZ)/4); float lengthX = (maxX-minX)/4; float lengthY = (maxY-minY)/2; float lengthZ = (maxZ-minZ)/4; CollisionAABB *b2 = new CollisionAABB(Vector3(maxX-lengthX,maxY-lengthY,maxZ-lengthZ),Vector3(lengthX,lengthY,lengthZ)); CollisionAABB *b3 = new CollisionAABB(Vector3(maxX-3*lengthX,maxY-lengthY,maxZ-3*lengthZ),Vector3(lengthX,lengthY,lengthZ)); CollisionAABB *b4 = new CollisionAABB(Vector3(maxX-3*lengthX,maxY-lengthY,maxZ-lengthZ),Vector3(lengthX,lengthY,lengthZ)); CollisionAABB *b5 = new CollisionAABB(Vector3(maxX-lengthX,maxY-lengthY,maxZ-3*lengthZ),Vector3(lengthX,lengthY,lengthZ)); CollisionData *d0 = new CollisionData(); if(AABBCollision(*b1,*b2,d0)){ // cout << "Collided:"<<endl; } else { if(AABBCollision(*b1,*b3,d0)){ // cout << "Collided:"<<endl; } else { if(AABBCollision(*b1,*b4,d0)){ // cout << "Collided:"<<endl; } else { if(AABBCollision(*b1,*b5,d0)){ // cout << "Collided:"<<endl; } else { // cout << "--------"<<endl; } } } } delete b1; delete b2; delete d0; } }
void PhysicsSystem::BroadPhaseCollisions() { for(vector<PhysicsNode*>::iterator i = allNodes.begin() ; i != allNodes.end(); ++i) { if((*i)->isPlane) { for(vector<PhysicsNode*>::iterator j = i; j != allNodes.end(); ++j) { if(*i != *j) { c_Sphere c2 = c_Sphere((*j)->GetPosition(), 50); CollisionData cd = CollisionData(); if(SphereInPlane((*j)->GetPosition(), c2.m_radius, c2, &cd)) { AddCollImpulse2(*j, cd); //(*j)->SetLinearVelocity((*j)->GetLinearVelocity()); //Vector3 temp = (*j)->GetLinearVelocity(); //temp.y = -temp.y; //(*j)->SetLinearVelocity(temp); } } } } //Getting the position from and making the spaceship backfire. if(((*i))->isShip && (*i)->isHit == false) { RandPos = (*i)->GetPosition();// + Vector3(100, 100, 100); //hitCount++; } //Check if hit the player/camera. if((*i)->isBackFire) { cubeAABB a = cubeAABB((*i)->GetPosition(), Vector3(100,100,100)); cubeAABB b = cubeAABB(CamPos, Vector3(300, 300, 300)); if(AABBCollision(a, b)){ lifes -= 1; jitter = true; } } for(vector<PhysicsNode*>::iterator j = i; j != allNodes.end(); ++j) { if(*i != *j) { c_Sphere c1 = c_Sphere((*i)->GetPosition(), 50); c_Sphere c2 = c_Sphere((*j)->GetPosition(), 50); CollisionData cd = CollisionData(); if(SphereSphereCollision(c1, c2, &cd) == true){ AddCollisionImpulse(*i, *j, cd); (*i)->m_HasHit = true; if((*i)->isShip == true) { ((*i))->isHit = true; score += 100; pos = ((*i))->GetPosition(); } } // AI if (AI){ if((*i)->isShip && (*j)->isMissile) { if(*i != *j) { Vector3 a = (*i)->GetPosition(); Vector3 b = (*j)->GetPosition(); float diffX = abs(a.x-b.x); float diffY = abs(a.y-b.y); float diffZ = abs(a.z-b.z); Vector3 diff = Vector3(diffX, diffY, diffZ); float sep; sep = (AIlevel == 0) ? 100.0 : 150.0f; if(diff.x <= 100) { if(diff.y <= 100) { if(diff.z <= 100) { (*i)->SetPosition((*i)->GetPosition() + Vector3(100, 100, 100)); RandPos = (*i)->GetPosition() + Vector3(100, 100, 100); } // add force or velo or acce } } } } } } } } }
void MyGame::processGameplay() { processPlayer(); map->update(this->player->getPosition(), SCREEN_WIDTH, SCREEN_HEIGHT); Vector2 playerPos = { player->getPosition().x, player->getPosition().y }; for (int i = 0; i < enemies.size(); i++) { Vector2 hunterPos = { enemies[i]->getPosition().x, enemies[i]->getPosition().y }; if (player->isAtDamageFrame() && distanceBetweenVectors(playerPos, hunterPos) <= 100) { if (circleCollision(player->getAttackCollision(), enemies[i]->getDamageCollision())) { enemies[i]->updateHealth(5); } } if (enemies[i]->getStatus() != DEAD) { enemies[i]->update(player->getPosition(), this->gameTimer.getCurrentRuntime(), this->mapTexture->getLocation().w, this->mapTexture->getLocation().h, this->obstacles); if (enemies[i]->getStatus() == SHOOTING) { std::vector<Projectile> newBullets = enemies[i]->shootBullets(this->gameTimer.getCurrentRuntime()); for (int i = 0; i < newBullets.size(); i++) { activeProjectiles.push_back(newBullets[i]); } } } else if (enemies[i]->getStatus() == DEAD) { std::uniform_int_distribution<int> keyDropChance(1, 1); if (keyDropChance(this->randomNumberGen) == 1 && keyDropped == false) { keyDropped = true; keyLocation = { enemies[i]->getPosition().x - map->getDrawLocation().x - 16, enemies[i]->getPosition().y - map->getDrawLocation().y - 16, 32, 32 }; keyCollision = { enemies[i]->getPosition().x - 16, enemies[i]->getPosition().y - 16, 32, 32 }; keyDropTimeout = gameTimer.getCurrentRuntime() + 1000; } } for (int i = activeProjectiles.size() - 1; i > 0; i--) { if (activeProjectiles[i].isActive()) { bool hitSomething = false; Vector2 playerPos = { this->player->getPosition().x, this->player->getPosition().y }; Vector2 bulletPos = { this->activeProjectiles[i].getLocation().x, this->activeProjectiles[i].getLocation().y }; if (distanceBetweenVectors(playerPos, bulletPos) < 100) { if (circleCollision(this->player->getDamageCollision(), this->activeProjectiles[i].getCollision())) { hitSomething = true; } if (hitSomething) { player->updateHealth(-5); } } for (int j = 0; j < obstacles.size(); j++) { if (AABBCollision(this->activeProjectiles[i].getObjCollision(), obstacles[j].getCollisionBox())) { hitSomething = true; break; } } activeProjectiles[i].update(hitSomething); } if (!activeProjectiles[i].isActive()) { activeProjectiles.erase(activeProjectiles.begin() + i); } } } }
void Hunter::update(SDL_Rect playerLocation, long currentRunTime, int mapWidth, int mapHeight, std::vector<EnvironmentObject> obstacles) { previousLocation.x = this->body->getPosition().x + cos((this->body->getDirection() - 180) * PI / 180) * this->body->getSpeed(); previousLocation.y = this->body->getPosition().y + sin((this->body->getDirection() - 180) * PI / 180) * this->body->getSpeed(); std::mt19937 random; random.seed(SDL_GetTicks()); std::uniform_int_distribution<int> randomDegree(0, 359); Vector2 player = { (double)playerLocation.x , (double)playerLocation.y }; if (this->status != DEAD) { switch (action) { case HUNT: if (currentRunTime >= directionChangeTime) { int degree = randomDegree(random); changeDirection(degree); directionChangeTime = currentRunTime + turnInterval; } this->body->accelerate(this->body->getMaxSpeed()); break; case TRACK: this->body->decelerate(this->body->getMaxSpeed()); changeDirection((int)angleBetweenVectors(this->body->getPosition(), player)); if (distanceBetweenVectors(this->body->getPosition(), player) > spotRange) { action = CHECK; lastPlayerLocation = player; } else if (distanceBetweenVectors(this->body->getPosition(), player) <= shootRange) { this->body->decelerate(this->body->getMaxSpeed()); action = SHOOT; } else { this->body->accelerate(this->body->getMaxSpeed()); } break; case CHECK: changeDirection((int)angleBetweenVectors(this->body->getPosition(), lastPlayerLocation)); if (distanceBetweenVectors(this->body->getPosition(), lastPlayerLocation) > 5) { this->body->accelerate(this->body->getMaxSpeed()); } else { this->body->decelerate(this->body->getMaxSpeed()); action = HUNT; } break; case SHOOT: status = SHOOTING; } if (this->body->getPosition().x < 0 || this->body->getPosition().x >= mapWidth) { this->body->changeDirection(this->body->getDirection() + 180); } if (this->body->getPosition().y < 0 || this->body->getPosition().y >= mapHeight) { this->body->changeDirection(this->body->getDirection() + 180); } for (int i = 0; i < obstacles.size(); i++) { if (AABBCollision(obstacles[i].getCollisionBox(),this->objectCollison)) { this->body->manuallyUpdatePosition(previousLocation); this->body->decelerate(this->body->getMaxSpeed()); this->body->changeDirection(this->body->getDirection() + 180); action = HUNT; } } this->body->updateDisplacement(0, 60); this->initCollision(); if (monsterSpotted(player) && action != FLEE && action != SHOOT) { action = TRACK; } } else { this->destroy(); } }