bool BoxCollider::Fits( const CylinderCollider &c ) const { BoxCollider b; b.position = glm::vec3( c.end.x, c.end.y / c.height, c.end.z); b.SetRadius( glm::vec3( c.radius, c.height/2, c.radius ) ); return Fits( b ); }
bool BoxCollider::Fits( const BoxCollider &AABB ) const { bool xAxis = ( Min().x <= AABB.Min().x && Max().x >= AABB.Max().x); bool yAxis = ( Min().y <= AABB.Min().y && Max().y >= AABB.Max().y); bool zAxis = ( Min().z <= AABB.Min().z && Max().z >= AABB.Max().z); return xAxis && yAxis && zAxis; }
JNIEXPORT void JNICALL Java_org_gearvrf_NativeBoxCollider_setHalfExtents(JNIEnv *env, jobject obj, jlong jcollider, jfloat x, jfloat y, jfloat z) { BoxCollider *collider = reinterpret_cast<BoxCollider *>(jcollider); collider->set_half_extents(x, y, z); }
// // removes from the passed vector all the colliders which are outside of the frustum // void Frustum::CollidersInFrustum(std::vector<ColliderComponent*>& shapesToCheck) const { std::vector<ColliderComponent*> inFrustum; BEGIN_STD_VECTOR_ITERATOR( ColliderComponent*, shapesToCheck ) if( currentItem->GetRTTI() == CylinderCollider::rtti ) { CylinderCollider* collider = static_cast<CylinderCollider*>(currentItem); CylinderShape* cylinder = static_cast<CylinderShape*>( collider->GetShape() ); if ( CylinderInFrustum( *cylinder ) ) { inFrustum.push_back( collider ); } } else if ( currentItem->GetRTTI() == BoxCollider::rtti ) { BoxCollider* collider = static_cast<BoxCollider*>(currentItem); BoxShape* box = static_cast<BoxShape*>( collider->GetShape() ); if ( BoxInFrustum( *box ) ) { inFrustum.push_back( collider ); } } END_STD_VECTOR_ITERATOR shapesToCheck.swap( inFrustum ); }
void Grid::ResolveCollisions(Entity* ent) { RigidBody* rigid = ent->GetComponent<RigidBody>(); if (rigid) { rigid->ResolveCollisions(); BoxCollider* box = ent->GetComponent<BoxCollider>(); if (box) { Vector3& max = box->GetMaxCoord(); Vector3& min = box->GetMinCoord(); if (min.x < 0 || min.y < 0 || max.x > GridWidth || max.y > GridHeight) ent->OnCollisionEnter(Collision()); } } for (auto &child : ent->GetChildren()) { ResolveCollisions(child); } }
// Creates a player GameObject* GameManager::CreatePlayer( uint32_t index, const XMFLOAT3& position, const XMFLOAT3& scale ) { Scene* scene = Scene::GetInstance(); GameObject* player = scene->AddGameObject( "Player_" + std::to_string( index ) ); ID3D11Device* device = player->GetDevice(); ID3D11DeviceContext* deviceContext = player->GetDeviceContext(); // Set the player's transform info Transform* transform = player->GetTransform(); transform->SetPosition( position ); transform->SetScale( scale ); // Add the collider to the player BoxCollider* collider = player->AddComponent<BoxCollider>(); collider->SetSize( XMFLOAT3( 1, 1, 1 ) ); // Add the rigid body to the player Rigidbody* rigidbody = player->AddComponent<Rigidbody>(); rigidbody->SetMass( 0 ); // Add the default material to the player DefaultMaterial* material = player->AddComponent<DefaultMaterial>(); material->LoadDiffuseMap( "Textures\\Rocks2.jpg" ); material->LoadNormalMap( "Textures\\Rocks2Normals.jpg" ); material->SetDirectionalLight( StageLight ); // Add the mesh renderer to the player MeshRenderer* meshRenderer = player->AddComponent<MeshRenderer>(); meshRenderer->SetMaterial( material ); meshRenderer->SetMesh( MeshLoader::Load( "Models\\cube.obj", device, deviceContext ) ); return player; }
void ClosestPtToAABB( glm::vec3& p, const BoxCollider& AABB ) { glm::vec3 min = AABB.Min(); glm::vec3 max = AABB.Max(); glm::clamp( p.x, min.x, max.x ); glm::clamp( p.y, min.y, max.y ); glm::clamp( p.z, min.z, max.z ); }
void GameScreen::BuildQuadTree(){ if (m_quadTree){ delete m_quadTree; m_quadTree = NULL; } m_quadTree = new QuadTree(); m_quadTree->buildFullTree(NBR_LEVELS, (m_width + 0.1f) / 2, (m_height + 0.1f) / 2, 0.5f, m_width + 0.1f, m_height + 0.1f, 1); for (int i = 0; i < m_land->size(); i++){ BoxCollider* temp = new BoxCollider; temp->setDimension(m_landCube->getScale(), m_landCube->getScale(), m_landCube->getScale()); temp->setPos(m_land->at(i)); m_quadTree->addObject(temp); } }
float SqrDistPointAABB( glm::vec3 &point, const BoxCollider &AABB ) { float sqrDist( 0.0f); glm::vec3 min = AABB.Min(); glm::vec3 max = AABB.Max(); if (point.x < min.x) sqrDist += ( min.x - point.x) * ( min.x - point.x); if (point.x > max.x) sqrDist += ( point.x - max.x) * ( point.x - max.x); if (point.y < min.y) sqrDist += ( min.y - point.y) * ( min.y - point.y); if (point.y > max.y) sqrDist += ( point.y - max.y) * ( point.y - max.y); if (point.z < min.z) sqrDist += ( min.z - point.z) * ( min.z - point.z); if (point.z > max.z) sqrDist += ( point.z - max.z) * ( point.z - max.z); return sqrDist; }
void Grid::PopulateCells(Entity* ent) { BoxCollider* box = ent->GetComponent<BoxCollider>(); if (box) { box->cell = cell; box->part.clear(); Vector3 min = box->GetMinCoord(); Vector3 max = box->GetMaxCoord(); min.x /= GridWidth; min.x *= NumberOfPartitionsX; max.x /= GridWidth; max.x *= NumberOfPartitionsX; min.y /= GridHeight; min.y *= NumberOfPartitionsY; max.y /= GridHeight; max.y *= NumberOfPartitionsY; for (int i = min.x; i <= max.x; ++i) for (int j = min.y; j <= max.y; ++j) { Partition* c = GetPartition(i, j); if (c) { c->Add(ent); box->part.push_back(c); } } } for (auto &child : ent->GetChildren()) { if (child->IsActive()) PopulateCells(child); } }
bool World::collidesWithWalls(const BoxCollider &collider) const { for (const Entity &e : scene->entities) { if (e.hasComponent<AACollisionBox>()) { if (collider.collidesWith(e.getComponent<AACollisionBox>().box)) { return true; } } } return false; }
bool World::collidesWithWalls(BoxCollider collider) { for (unsigned int i = 0; i < scene->walls.size(); i++) { Entity wall = scene->walls[i]; BoxCollider wallCollider(wall.position, wall.scale); if (collider.collidesWith(wallCollider)) { return true; } } return false; }
// Creates an arrow game object GameObject* GameManager::CreateArrow( const XMFLOAT3& position, const XMFLOAT3& force ) { static const XMFLOAT3 ArrowSize = { 1, 0.1f, 1.0f }; Scene* scene = Scene::GetInstance(); ID3D11Device* device = _gameObject->GetDevice(); ID3D11DeviceContext* deviceContext = _gameObject->GetDeviceContext(); // Create the arrow object GameObject* arrow = scene->AddGameObject( _gameObject->GetName() + "_Arrow_" + std::to_string( _arrowCount++ ) ); // Set some transform info Transform* transform = arrow->GetTransform(); transform->SetPosition( position ); // Add the arrow's collider BoxCollider* collider = arrow->AddComponent<BoxCollider>(); collider->SetSize( ArrowSize ); // Add the arrow's rigidbody Rigidbody* rigidbody = arrow->AddComponent<Rigidbody>(); rigidbody->SetMass( 1.0f ); // Set the arrow's collision callback GameObject::CollisionCallback callback = std::bind( &GameManager::OnArrowCollide, this, _1 ); arrow->AddEventListener( "OnArrowCollide", callback ); // Add a default material DefaultMaterial* material = arrow->AddComponent<DefaultMaterial>(); material->LoadDiffuseMap( "Textures\\SolidWhite.png" ); material->SetDirectionalLight( StageLight ); // Add a mesh renderer MeshRenderer* meshRenderer = arrow->AddComponent<MeshRenderer>(); meshRenderer->SetMaterial( material ); meshRenderer->SetMesh( MeshLoader::Load( "Models\\arrow.obj", device, deviceContext ) ); return arrow; }
void CompositeCollider::addCollider(const BoxCollider& collider) { mBoxColliders.push_back(collider); std::vector<Wall2f> walls = collider.getWalls(); mWalls.insert(mWalls.end(), walls.begin(), walls.end()); }
//----------------------------------------------------------------------- bool BoxColliderTranslator::translateChildProperty(ScriptCompiler* compiler, const AbstractNodePtr &node) { PropertyAbstractNode* prop = reinterpret_cast<PropertyAbstractNode*>(node.get()); ParticleAffector* af = any_cast<ParticleAffector*>(prop->parent->context); BoxCollider* affector = static_cast<BoxCollider*>(af); if (prop->name == token[TOKEN_BOX_WIDTH]) { if (passValidateProperty(compiler, prop, token[TOKEN_BOX_WIDTH], VAL_REAL)) { // Property: box_width Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setWidth(val); return true; } } } else if (prop->name == token[TOKEN_BOXCOLL_WIDTH]) { // Property: box_collider_width (deprecated and replaced by 'box_width') if (passValidateProperty(compiler, prop, token[TOKEN_BOXCOLL_WIDTH], VAL_REAL)) { Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setWidth(val); return true; } } } else if (prop->name == token[TOKEN_BOX_HEIGHT]) { // Property: box_height if (passValidateProperty(compiler, prop, token[TOKEN_BOX_HEIGHT], VAL_REAL)) { Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setHeight(val); return true; } } } else if (prop->name == token[TOKEN_BOXCOLL_HEIGHT]) { // Property: box_collider_height (deprecated and replaced by 'box_height') if (passValidateProperty(compiler, prop, token[TOKEN_BOXCOLL_HEIGHT], VAL_REAL)) { Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setHeight(val); return true; } } } else if (prop->name == token[TOKEN_BOX_DEPTH]) { // Property: box_depth if (passValidateProperty(compiler, prop, token[TOKEN_BOX_DEPTH], VAL_REAL)) { Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setDepth(val); return true; } } } else if (prop->name == token[TOKEN_BOXCOLL_DEPTH]) { // Property: box_collider_depth (deprecated and replaced by 'box_depth') if (passValidateProperty(compiler, prop, token[TOKEN_BOXCOLL_DEPTH], VAL_REAL)) { Real val = 0.0f; if(getReal(prop->values.front(), &val)) { affector->setDepth(val); return true; } } } else if (prop->name == token[TOKEN_INNER_COLLISION]) { // Property: inner_collision if (passValidateProperty(compiler, prop, token[TOKEN_INNER_COLLISION], VAL_BOOL)) { bool val; if(getBoolean(prop->values.front(), &val)) { affector->setInnerCollision(val); return true; } } } else { // Parse the BaseCollider BaseColliderTranslator baseColliderTranslator; return baseColliderTranslator.translateChildProperty(compiler, node); } return false; }
void GameScreen::placeUnit(Unit* unit){ bool done = false, regen = false; int attempts = 0; while (!done){ unit->setPosition(random::rnd.number(m_width), random::rnd.number(m_height), 1); for (int i = 0; i < m_land->size(); i++){ Vector point1, point2; point1 = unit->getPosition().x - (unit->getModel()->getCollider().getWidth() / 2); point2 = unit->getPosition().x + (unit->getModel()->getCollider().getWidth() / 2); BoxCollider tempBCol; tempBCol = m_landCube->getCollider(); tempBCol.setPos(m_land->at(i)); if ((point1.x <= tempBCol.getPos().x + (tempBCol.getWidth() / 2) && point1.x >= tempBCol.getPos().x - (tempBCol.getWidth() / 2)) || (point2.x <= tempBCol.getPos().x + (tempBCol.getWidth() / 2) && point2.x >= tempBCol.getPos().x - (tempBCol.getWidth() / 2))){ unit->setPosition(m_land->at(i).x, m_land->at(i).y + m_landCube->getCollider().getHeight(), 1); for (int j = 0; j < m_land->size(); j++){ if (unit->getPosition().x == m_land->at(j).x){ if (unit->getPosition().y < m_land->at(j).y){ float temp; temp = m_land->at(j).y - unit->getPosition().y; if (temp <= 0.1f){ unit->setPosition(m_land->at(j).x, m_land->at(j).y + m_landCube->getCollider().getHeight(), 1); } } } } if (unit->getPosition().y < m_height){ done = true; } for (int k = 0; k < NBR_TEAMS; k++){ for (int l = 0; l < NBR_UNITS; l++){ if (m_teams[k]->getUnit(l) == unit){ continue; } if (m_teams[k]->getUnit(l)->getPosition() == unit->getPosition()){ done = false; } } } break; } } if (attempts >= 100){ done = true; regen = true; } attempts++; } if (regen){ createGame(m_tGen.getCurrentType()); } unit->setPosition(unit->getPosition().x, unit->getPosition().y + m_landCube->getCollider().getHeight(), 1); }
void OctTree::buildTree() { if (objects.size() <= 1) return; vec3 dim = region.max - region.min; if (dim == vec3(0)) { //findEnclosingCube(); dim = vec3(650, 650, 650.0f);//region.max - region.min; } if (dim.x < MIN_SIZE && dim.y < MIN_SIZE && dim.z < MIN_SIZE) { return; } vec3 half = dim / 2.0f; vec3 center = region.min + half; BoundingBox octant[8]; octant[0] = BoundingBox(region.min, center); octant[1] = BoundingBox(vec3(center.x, region.min.y, region.min.z), vec3(region.max.x, center.y, center.z)); octant[2] = BoundingBox(vec3(center.x, region.min.y, center.z), vec3(region.max.x, center.y, region.max.z)); octant[3] = BoundingBox(vec3(region.min.x, region.min.y, center.z), vec3(center.x, center.y, region.max.z)); octant[4] = BoundingBox(vec3(region.min.x, center.y, region.min.z), vec3(center.x, region.max.y, center.z)); octant[5] = BoundingBox(vec3(center.x, center.y, region.min.z), vec3(region.max.x, region.max.y, center.z)); octant[6] = BoundingBox(center, region.max); octant[7] = BoundingBox(vec3(region.min.x, center.y, center.z), vec3(center.x, region.max.y, region.max.z)); std::vector<GameObject*> octList[8]; for (int i = 0; i < 8; i++) octList[i] = std::vector<GameObject*>(); std::vector<int> delist = std::vector<int>(); for (unsigned int index = 0; index < objects.size(); index++) { BoxCollider* bc = (BoxCollider*)objects[index]->getComponent(EGameComponentType::BOX_COLLIDER); if (bc->min != bc->max) { for (int i = 0; i < 8; i++) { if (octant[i].contains(bc->getBoundingBox())) { octList[i].push_back(objects[index]); delist.push_back(index); break; } } } } for (int i = delist.size() - 1; i >= 0; i--) objects.erase(objects.begin() + delist[i]); for (int i = 0; i < 8; i++) { if (octList[i].size() != 0) { children[i] = createNode(octant[i], octList[i]); active |= (unsigned int)(1 << i); children[i]->buildTree(); } } treeBuilt = true; treeReady = true; }
bool SphereCollider::Fits( const BoxCollider &other ) const { return other.Fits(*this); }
bool CylinderCollider::Fits( const BoxCollider &other ) const { return other.Fits(*this); }
void GameScreen::update(float mouseX, float mouseY){ updateMouse(mouseX, mouseY); for (int i = 0; i < NBR_TEAMS; i++){ m_teams[i]->update(); } //Causes an explosion at where the curShor hits if (m_curUnit->getWeapon()->getCurShot()){ if (m_quadTree->processCollisions(m_curUnit->getWeapon()->getCurShot()->getModel())){ if (m_explo){ float exploForce = 0.0f; exploForce = m_curUnit->getWeapon()->getPower() * 0.5f; if (exploForce < 0.2f){ exploForce = 0.2f; } m_explo->circularExplosion(Vector(m_curUnit->getWeapon()->getCurShot()->getModel()->getPos().x, m_curUnit->getWeapon()->getCurShot()->getModel()->getPos().y, 1), exploForce, m_curUnit->getWeapon()->getDamage() * m_curUnit->getWeapon()->getPower(), *m_quadTree); m_curUnit->getWeapon()->hitObject(NULL, 0, 0); m_hitTerrain = true; m_endTurnTimer->resetTimer(); } } } for (int i = 0; i < NBR_TEAMS; i++){ for (int j = 0; j < NBR_UNITS; j++){ Unit* tempUnit = m_teams[i]->getUnit(j); //Smooths out physics tempUnit->getModel()->setPos(tempUnit->getPhysics()->nextPos()); //Checks to see if collision occurs between current position and next BoxCollider* tempCol = (BoxCollider*)m_quadTree->processCollisions(tempUnit->getModel(), tempUnit->getPhysics()->nextPos()); if (tempCol){ //When going left, collide with right of terrain if (tempUnit->getPhysics()->getVelX() < 0){ if (tempCol->hitright(&tempUnit->getModel()->getCollider())){ tempUnit->getPhysics()->setAccelX(0.0f); tempUnit->getPhysics()->setVelocityX(0.0f); } } //When going right collide with left of terrain if (tempUnit->getPhysics()->getVelX() > 0){ if (tempCol->hitLeft(&tempUnit->getModel()->getCollider())){ tempUnit->getPhysics()->setAccelX(0.0f); tempUnit->getPhysics()->setVelocityX(0.0f); } } //When falling collide with top of terrain if (tempUnit->getPhysics()->getVelY() < 0){ if (tempCol->hitTop(&tempUnit->getModel()->getCollider())){ tempUnit->getPhysics()->setAccelY(0.0f); tempUnit->getPhysics()->setVelocityY(0.0f); tempUnit->getPhysics()->isGrounded(true); } } //When jumping collide with bottom of terrain if (tempUnit->getPhysics()->getVelY() > 0){ if (tempCol->hitBottom(&tempUnit->getModel()->getCollider())){ tempUnit->getPhysics()->setAccelY(0.0f); tempUnit->getPhysics()->setVelocityY(0.0f); } } } else{ //If not colliding the unit is falling tempUnit->getPhysics()->isGrounded(false); } //If the unit goes below the water level they die if (tempUnit->getPosition().y < WATER_LEVEL && !tempUnit->isDead()){ tempUnit->setCurHealth(0); if (m_curUnit == tempUnit){ changeUnit(); } } //When a unit is below 0 health set them to dead and change unit if (!tempUnit->isDead() && tempUnit->getCurHealth() <= 0){ tempUnit->setCurHealth(0); if (m_curUnit == tempUnit){ changeUnit(); } } //If the unit is alive, run its update if (!tempUnit->isDead()){ if (m_delayTest){ tempUnit->update(m_mousePos); } tempUnit = NULL; } } } m_delayTest = true; //Keeps camera on the unit m_cam->move()->setPos(m_curUnit->getPosition().x, m_curUnit->getPosition().y, m_cam->move()->getPos().z); if (m_cam->getPos().y <= m_landCube->getScale() * 2){ m_cam->move()->setPos(m_cam->getPos().x, m_landCube->getScale() * 2, m_cam->getPos().z); } m_skySphere->setPos(m_cam->getPos().x, m_cam->getPos().y, 1); //If the game lasts longer than its allowed its a draw if (m_gameTimer->getElapsedTime() >= GAME_TIME){ SceneSelect::getInstance(m_hdc).setWinner("draw"); SceneSelect::getInstance(m_hdc).setScene(END, m_hdc); return; } int teamsAlive = 0; string teamName = "draw"; //if it does'nt change the game is a draw //Checks to see how many teams are alive for (int i = 0; i < NBR_TEAMS; i++){ if (!m_teams[i]->isDead()){ teamsAlive++; teamName = m_teams[i]->getName(); } } //If there arent 2 teams alive, gameover if (teamsAlive < 2){ SceneSelect::getInstance(m_hdc).setWinner(teamName); SceneSelect::getInstance(m_hdc).setScene(END, m_hdc); return; } //Changes unit at the end of the turn if (m_turnTimer->getElapsedTime() >= TURN_TIME){ changeUnit(); } //Changes the unit after it hits the terrain or after firing a projectile if (m_hitTerrain && m_endTurnTimer->getElapsedTime() >= HIT_TURN_TIME || m_fired && m_endTurnTimer->getElapsedTime() >= FIRED_TURN_TIME){ changeUnit(); } }
void ItemElement::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QGraphicsPixmapItem::paint(painter,option,widget); if(qobject_cast<MainScene*>(scene())){ showColliders = qobject_cast<MainScene*>(scene())->showColliders(); if(this->isSelected()){ mRotateEnabled = qobject_cast<MainScene*>(scene())->rotate(); } } else { mRotateEnabled = false; } if(qobject_cast<SplitScene*>(scene())){ showColliders = true; } if(showColliders){ QRectF tmpRect; QList<QGraphicsItem*> colliders = mTemplate->getColliderRoot()->childItems(); foreach(QGraphicsItem* col, colliders){ if(col->type() == QGraphicsItem::UserType+1){ BoxCollider* boxCol = static_cast<BoxCollider*>(col); tmpRect = tmpRect.united(boxCol->getRectToDraw().boundingRect()); QPen pen(QColor(Qt::cyan)); pen.setWidth (1); painter->setPen (pen); painter->drawPolygon(boxCol->getRectToDraw()); painter->setBrush (QColor(Qt::green)); painter->setOpacity (0.3); painter->drawPolygon(boxCol->getRectToDraw()); } if(col->type() == QGraphicsItem::UserType+2){ MeshCollider* meshCol = static_cast<MeshCollider*>(col); tmpRect = tmpRect.united(meshCol->getPolyToDraw().boundingRect()); QPen pen(QColor(Qt::cyan)); pen.setWidth (1); painter->setPen (pen); painter->drawPolygon(meshCol->getPolyToDraw()); painter->setBrush (QColor(Qt::green)); painter->setOpacity (0.3); painter->drawPolygon(meshCol->getPolyToDraw()); } if(col->type() == QGraphicsItem::UserType+3){ CircleCollider* cirCol = static_cast<CircleCollider*>(col); int rad = (int)cirCol->getRadius(); QPointF center = cirCol->getCenter(); QPointF topLeft = QPointF(center.x() - rad,center.y() - rad); QPointF bottomRight = QPointF((center.x() + rad),(center.y() + rad)); tmpRect = tmpRect.united(QRectF(topLeft,bottomRight)); QPen pen(QColor(Qt::cyan)); pen.setWidth (1); painter->setPen (pen); painter->drawEllipse(center,rad,rad); painter->setBrush (QColor(Qt::green)); painter->setOpacity (0.3); painter->drawEllipse(center,rad,rad); } updateColliderRect(tmpRect); } if(mRotateEnabled){ painter->drawEllipse(boundingRect().center(),50,50); if(mItemDragged){ painter->setPen(QColor(Qt::darkGreen)); painter->setBrush(QColor(Qt::darkGreen)); // painter->drawPie(QRectF(boundingRect().center()-QPointF(50,-50),boundingRect().center()+QPointF(50,-50)),mRotationStartAngle*16,mRotationSpanAngle*16); } } } }