/** * This method updates the player. It moves the player and also stops the player * from moving solid objects. */ void Player::update(const std::vector<SDL_Rect>& solids) { // Collisions for the X axis. rect.x += std::round(movement.x * Application::getDeltaTime()); for (const auto& solid : solids) { if (SDL_HasIntersection(&solid, &rect)) { // If the player is moving to the right. if (movement.x > 0) { rect.x = solid.x - rect.w; } // If the player is moving to the left. else if (movement.x < 0) { rect.x = solid.x + solid.w; } } } // Collisions for the Y axis. rect.y += std::round(movement.y * Application::getDeltaTime()); for (const auto& solid : solids) { if (SDL_HasIntersection(&solid, &rect)) { // If the player is moving down. if (movement.y > 0) { rect.y = solid.y - rect.h; } // If the player is moving up. else if (movement.y < 0) { rect.y = solid.y + solid.h; } } } // Center the camera on the player. Application::getCamera().x = (Application::getWindowSize().x / 2) - rect.x; Application::getCamera().y = (Application::getWindowSize().y / 2) - rect.y; // Make sure the player faces the mouse. angle = Tools::angleBetweenPoints(rect.x + (rect.w / 2), rect.y + (rect.h / 2), Application::getMousePosition().x - Application::getCamera().x, Application::getMousePosition().y - Application::getCamera().y) - 180; shoot_delay += Application::getDeltaTime(); }
void checkCollision(soldiers *bullets, soldiers *meatbags, Mix_Chunk **deaths, int *deadNo) { int looperBullet, looperMeatbags, randomDeath ,noDead; noDead = 0; for(looperBullet = 0; looperBullet < bullets->no_men; looperBullet++) { for(looperMeatbags = 0; looperMeatbags < meatbags->no_men; looperMeatbags++) { if(SDL_HasIntersection(&(bullets->men[looperBullet]->posAndHitbox),&(meatbags->men[looperMeatbags]->posAndHitbox)) == SDL_TRUE && bullets->men[looperBullet]->isAnimated == SUCCESS && meatbags->men[looperMeatbags]->isAnimated == SUCCESS && bullets->men[looperBullet]->frame.x != 128) { randomDeath = rand() % 6; if(bullets->men[looperBullet]->type != 5) { bullets->men[looperBullet]->isAnimated = FAIL; } meatbags->men[looperMeatbags]->isAnimated = FAIL; Mix_PlayChannel(-1,deaths[randomDeath], 0); meatbags->men[looperMeatbags]->frame.x = 0; meatbags->men[looperMeatbags]->frame.y = 0; noDead++; } } } *deadNo = *deadNo + noDead; }
//this function checks for a collison between the playerbullet and the spaceship //it then resets the spaceship's position on the opposite side that it appeared from void spaceShipHit( Bullet *_playerBullet, Ship *_spaceShip ) { //only if the player's bullet has been fired will it check for a collision //if there is one then the score is changed if(_playerBullet->fire==1) { if(SDL_HasIntersection(&_spaceShip->pos, &_playerBullet->pos)) { _playerBullet->invaderHit=1; _playerBullet->score+=100; _spaceShip->active=0; if(_spaceShip->randomDir==0) { _spaceShip->pos.x=WIDTH+shipDelay; } else if(_spaceShip->randomDir==1) { _spaceShip->pos.x=0-shipDelay; } } } }
void updateVisibility() { isVisible = false; if (!bitmap) return; if (bitmap->isDisposed()) return; if (!opacity) return; /* Compare sprite bounding box against the scene */ /* If sprite is zoomed/rotated, just opt out for now * for simplicity's sake */ const Vec2 &scale = trans.getScale(); if (scale.x != 1 || scale.y != 1 || trans.getRotation() != 0) { isVisible = true; return; } SDL_Rect self; self.x = trans.getPosition().x - trans.getOrigin().x; self.y = trans.getPosition().y - trans.getOrigin().y; self.w = bitmap->width(); self.h = bitmap->height(); isVisible = SDL_HasIntersection(&self, &sceneRect); }
std::vector<QuadElement> QuadTree::QueryRange(const SDL_Rect& boundary) { std::vector<QuadElement> entitiesinrange; if (!SDL_HasIntersection(&boundary_, &boundary)) return entitiesinrange; if (nodes_[NORTHWEST].get() != nullptr) { std::vector<QuadElement> nodeentities; for (int i = 0; i < nodecount_; i++) { nodeentities = nodes_[i]->QueryRange(boundary); if (nodeentities.size() > 0) { entitiesinrange.insert(entitiesinrange.end(), nodeentities.begin(), nodeentities.end()); nodeentities.clear(); } } } else entitiesinrange = entities_; return entitiesinrange; }
bool QuadTree::Insert(QuadElement entity) { if ( ( !SDL_HasIntersection(&boundary_, &entity.boundingrectangle->Rectangle())) || (entity.boundingrectangle->width_ * entity.boundingrectangle->height_) > (boundary_.w * boundary_.h)) return false; if (nodes_[NORTHWEST].get() != nullptr) { for (int i = 0; i < nodecount_; i++) nodes_[i]->Insert(entity); } else { entities_.push_back(entity); if ( (entities_.size() <= capacity_) || ( currentlevel_+1 > maxlevels_) ) return true; Subdivide(); for (int i = 0; i < nodecount_; i++) { for (int j = 0; j < entities_.size(); j++) nodes_[i]->Insert(entities_[j]); } entities_.clear(); return true; } return false; }
bool Game_Object::intersects_rect(Game_Object& object) { SDL_Rect a = { static_cast<int>( std::round( position.get_x() - static_cast<double>(width) / 2 ) ), static_cast<int>( std::round( position.get_y() - static_cast<double>(height) / 2 ) ), width, height }; SDL_Rect b = { static_cast<int>( std::round( object.position.get_x() - static_cast<double>(object.get_width()) / 2 ) ), static_cast<int>( std::round( object.position.get_y() - static_cast<double>(object.get_height()) / 2 ) ), object.get_width(), object.get_height() }; return SDL_HasIntersection(&a, &b); }
int Tank::lightCollision(SDL_Rect light) { if (SDL_HasIntersection(&light, &posRect)) { return 1; } return 0; }
int Tank::turkeyCollision(SDL_Rect tLegRect) { if (SDL_HasIntersection(&tLegRect, &posRect)) { tLegNum++; return 1; } return 0; }
int Tank::rockCollision(SDL_Rect rockRect) { if (SDL_HasIntersection(&rockRect, &posRect)) { rocks++; return 1; } return 0; }
int Tank::keyCollision(SDL_Rect keyRect) { if (SDL_HasIntersection(&keyRect, &posRect)) { key++; return 1; } return 0; }
bool Enemy::collision(SDL_Rect playerLoc) { SDL_bool collision = SDL_HasIntersection(&playerLoc, &worldLoc); if (collision == SDL_TRUE) { return true; } else { return false; } }
bool collide(ItemLogic& item, BoxLogic& daBox) { bool collide = false; if (SDL_HasIntersection(&item.hitBox, &daBox.hitBox)) { collide = true; item.alive = false; } return collide; }
/* * SDL.hasIntersection(r1, r2) * * Arguments: * r1 the first rectangle * r2 the second rectangle * * Returns: * True on intersections */ static int l_hasIntersection(lua_State *L) { SDL_Rect a, b; videoGetRect(L, 1, &a); videoGetRect(L, 2, &b); return commonPush(L, "b", SDL_HasIntersection(&a, &b)); }
bool CollisionManager::checkCollision(const SDL_Rect bo, const SDL_Rect check, SDL_Rect* intersect) { //SDL_IntersectRect unreliable :( if(SDL_HasIntersection(&bo, &check) == SDL_TRUE) { SDL_IntersectRect(&bo, &check, intersect); return true; } return false; }
void BoundingBoxCollisionDetector::detect(Sprite* s1, Sprite* s2) { if (!s1->is_visible() || !s2->is_visible()) return; Rect* r1 = s1->collision_rect(); Rect* r2 = s2->collision_rect(); if (SDL_HasIntersection(r1, r2) == SDL_TRUE) { s1->on_collision(s2); s2->on_collision(s1); } }
bool Piece::isIntersectRange(SDL_Rect rect) { for (int i = 0; i < this->mRange.size(); ++i) { if (SDL_HasIntersection(&rect, &mRange[i])) { return true; } } return false; }
void TurrentBullet::update() { posX_ += vX_; posY_ += vY_; posRect_.x = posX_; posRect_.y = posY_; if (SDL_HasIntersection(&posRect_, Window::rect()) == SDL_FALSE) suicide(); }
/** * This method updates all of the enemies in the game. */ void Level::update(Player& player) { for (auto& enemy : enemies) { enemy->update(this, player); } for (auto& projectile : enemy_projectiles) { projectile.update(); } for (auto& wall : wall_rects) { for (auto projectile = enemy_projectiles.begin(); projectile != enemy_projectiles.end();) { if (SDL_HasIntersection(&wall, &projectile->getRect())) { enemy_projectiles.erase(projectile); } else { projectile++; } } } for (auto projectile = enemy_projectiles.begin(); projectile != enemy_projectiles.end();) { if (SDL_HasIntersection(&projectile->getRect(), &player.getRect())) { player.damage(projectile->getDamage()); enemy_projectiles.erase(projectile); } else { projectile++; } } }
bool Piece::isClickedRange(SDL_Rect mousePosition) { for (int i = 0; i < this->mRange.size(); ++i) { if (SDL_HasIntersection(&mousePosition, &mRange[i])) { GAMEMANAGER.setRectRangeSelected(mRange[i]); return true; } } return false ; }
bool Button::isClicked(Vec2 cursor) { SDL_Rect point; point.x = (int)cursor.x; point.y = (int)cursor.y; point.h = 1; point.w = 1; /* Apparently this is implemented in SDL 2.0.4 but 2.0.3 is the latest stable release. if (SDL_PointInRect()) */ return SDL_HasIntersection(&point, &AABB) != 0; // Had to put != 0 because it uses SDL_Bool instead of bool (Generates Warning) }
int sprite_handleMouse(sprite* s, SDL_Event* e) { if (s->mouseEnabled == 0 || s->visible == 0) return 0; // mouse SDL_Rect m; SDL_GetMouseState(&m.x, &m.y); m.w = 1; m.h = 1; SDL_Rect aabb; aabb.x = s->x - s->width / 2; aabb.y = s->y - s->height / 2; aabb.w = s->width; aabb.h = s->height; int inside = SDL_HasIntersection(&m, &aabb); switch (e->type) { case SDL_MOUSEBUTTONDOWN: if (inside) { // handle onMouseDown if (s->onMouseDown != NULL) s->onMouseDown((entity*)s); s->mouseDown = 1; return 1; } break; case SDL_MOUSEBUTTONUP: if (inside && s->mouseDown) { // click if (s->onClick != NULL) s->onClick((entity*)s); } if (s->mouseDown) { // reset if (s->onMouseUp != NULL) s->onMouseUp((entity*)s); s->mouseDown = 0; } break; } return 0; }
RangeStatue Piece::addRangeRect(SDL_Rect rangeRect, bool force) { map<std::string, Piece> whiteList = GAMEMANAGER.getPieceWhiteList(); map<std::string, Piece> blackList = GAMEMANAGER.getPieceBlackList(); SDL_Rect damierRect = GAMEMANAGER.getDamierRect(); if (!SDL_HasIntersection(&damierRect, &rangeRect)) return RangeStatue::Out; for (std::map<string, Piece>::iterator it = whiteList.begin(); it != whiteList.end(); ++it) { if (SDL_HasIntersection(&(it->second.mPosition), &rangeRect)) { if (this->mColor == ColorPiece::White) return RangeStatue::InPiece; this->mRange.push_back(rangeRect); return (force) ? RangeStatue::InEnnmyPiece : RangeStatue::InPiece; } } for (std::map<string, Piece>::iterator it2 = blackList.begin(); it2 != blackList.end(); ++it2) { if (SDL_HasIntersection(&(it2->second.mPosition), &rangeRect)) { if (this->mColor == ColorPiece::Black) return RangeStatue::InPiece; this->mRange.push_back(rangeRect); return (force) ? RangeStatue::InEnnmyPiece : RangeStatue::InPiece; } } this->mRange.push_back(rangeRect); return RangeStatue::Correct; }
vector<Event> Eventgen::checkForEvents() { vector<Event> to_trigger; for(const auto& group : m_grouped_event_boxes) { ///////////////////////////////// //check if the group has the "single" property bool single = false; map<string, string>::const_iterator pip = group.properties.find(PROPERTY_GROUP_SINGLE_IDENTIFIER); if(pip != group.properties.end() && pip->second == "true") { single = true; } for (const auto& object : group.objects) { //TODO: optimize map translation SDL_Rect objectBoundingBox(object.bbox); objectBoundingBox.y += mViewport.h - (mMap->getTileMap().height * mMap->getTileMap().tileheight); if (SDL_HasIntersection(&objectBoundingBox, &mPlayerBoundingBox)) { Event event; event.groupname = group.name; event.objectname = object.name; if(single) { auto singleIterator = std::find(m_triggered_single_events.begin(), m_triggered_single_events.end(), event); if(singleIterator != m_triggered_single_events.end()) { LogDebug("Not triggering single event again: " << object.name); continue; } else { m_triggered_single_events.push_back(event); } } LogDebug("Triggering event for object: " << object.name); to_trigger.push_back(event); } } } return to_trigger; }
int checkButtonClicked(baseEntity *mouse, entity *startButton, SDL_Event *events) { if(SDL_HasIntersection(&(mouse->dimensions), &(startButton->posAndHitbox)) == SDL_TRUE) { return SUCCESS; } else { return FAIL; } }
void PlayerTargetingSystem::ProcessEntities() { int mouseX, mouseY; const uint8_t* keyboardstate; std::vector<bool> mousestate; BoundingRectangleComponent* targetboundingrectangle; // Set up Player if not already done if (_player == NOTARGET) { _player = GetECSManager()->GetAssociatedEntities("PLAYER")[0]; _playertargetingcomponent = GetEntityComponent<TargetingComponent*>(_player, TargetingComponent::ID); if (_playertargetingcomponent == nullptr) std::cout << "PLAYERTARGETINGSYSTEM:: No Player Targeting Component, peace out!" << std::endl; else _playertargetingcomponent->SetTarget(NOTARGET); } if (_input->leftMousePressed()) { SDL_GetMouseState(&mouseX, &mouseY); _playertargetingcomponent->SetTarget(NOTARGET); _playertargetingcomponent->SetTargetState(false); SDL_Rect queryrect{ mouseX, mouseY, 1, 1 }; std::vector<QuadElement> elements = _gameworld->SparseGridQueryRange(queryrect); for (int i = 0; i < elements.size(); i++) { if (SDL_HasIntersection(&elements[i].boundingrectangle->Rectangle(), &queryrect)) { _playertargetingcomponent->SetTarget(elements[i].entityid); _playertargetingcomponent->SetTargetState(true); break; } } } if (_input->isKeyPressed(SDL_SCANCODE_TAB)) std::cout << "Handle Tab Targeting" << std::endl; if (_playertargetingcomponent->HasTarget()) UpdateTargetReticule(); }
bool Rect::intersectWith(const Rect& other, Rect* intersection) const { SDL_Rect a; SDL_Rect b; if (!intersection) return SDL_HasIntersection(this->copyInto(&a), other.copyInto(&b)) == SDL_TRUE; SDL_Rect result; const SDL_bool intersects = SDL_IntersectRect(this->copyInto(&a), other.copyInto(&b), intersection->copyInto(&result)); intersection->x = result.x; intersection->y = result.y; intersection->width = result.w; intersection->height = result.h; return intersects == SDL_TRUE; }
int checkCollision(Bullet bullet, Invader invaders[ROWS][COLS]) { /* this is the function that checks for collision between the bullet and the Invader * if there is a collision the invader is set inactive * the function will return 1 if there is a collision and 0 if there isn't one * the return values will be used in the main to deactivate the bullet */ for(int r=0; r < ROWS; ++r) { for(int c=0; c < COLS; ++c) { if (SDL_HasIntersection(&invaders[r][c].pos, &bullet.dstPos) == SDL_TRUE && invaders[r][c].active == 1) { invaders[r][c].active = 0; return 1; } } } return 0; }
void ModuleCollider::CheckAllCol() { for (list<Collider>::iterator itA = Colliders.begin(); itA != Colliders.end(); ++itA) { for (list<Collider>::iterator itB = Colliders.begin(); itB != Colliders.end(); ++itB) { if (itA != itB) { if (itA->parent->type == road && itB->parent->type == road) {} else { if (SDL_HasIntersection(&itA->rect, &itB->rect)) { itA->parent->OnCollisionEnter(itB->parent); } } } } } }
bool GameObject::collideWith(GameObject* other) const { if (other && this->textureResource) { SDL_Rect myRect; myRect.x = (int)(this->location.x + this->textureResource->width * 0.1f); myRect.y = (int)(this->location.y + this->textureResource->height * 0.1f); myRect.w = (int)(this->textureResource->width * 0.8f); myRect.h = (int)(this->textureResource->height * 0.8f); SDL_Rect otherRect; otherRect.x = (int)(other->location.x + other->textureResource->width * 0.1f); otherRect.y = (int)(other->location.y + other->textureResource->height * 0.1f); otherRect.w = (int)(other->textureResource->width * 0.8f); otherRect.h = (int)(other->textureResource->height * 0.8f); return SDL_TRUE == SDL_HasIntersection(&myRect, &otherRect); } return false; }