bool Collidable::Collides(Collidable* inputPtr, CollisionResponseFunc func, CollisionFeedback *feedbackPtr) { CollisionFeedback defaultFeedback; if( feedbackPtr == 0 ) feedbackPtr = &defaultFeedback; Collidable *invokingPtr = this; if( this->GetCOType() > inputPtr->GetCOType() ) { invokingPtr = inputPtr; inputPtr = this; } feedbackPtr->collisionPair.collidablePtrs[0] = invokingPtr; feedbackPtr->collisionPair.collidablePtrs[1] = inputPtr; if( CollisionLibrary::GetInstance().GetPairCollisionFunc( invokingPtr->GetCOType(), inputPtr->GetCOType())(feedbackPtr) ) { if( func != 0 ) func( feedbackPtr ); return true; } return false; }
void Activate(unsigned int aId) { const CollidableTemplate &collidabletemplate = Database::collidabletemplate.Get(aId); Collidable *collidable = new Collidable(collidabletemplate, aId); Database::collidable.Put(aId, collidable); collidable->AddToWorld(); }
void Asteroid::ResolveCollision(Collidable& collidable) { if (collidable.GetCollidableType() == CollidableType_Asteroid) { dynamic_cast<Asteroid&>(collidable).ResolveCollision(*this); } else if (collidable.GetCollidableType() == CollidableType_Shot) { dynamic_cast<Shot&>(collidable).ResolveCollision(*this); } else if (collidable.GetCollidableType() == CollidableType_Player) { dynamic_cast<Player&>(collidable).ResolveCollision(*this); } }
void OcTree::WalkScene(const Collidable &collider, const FilterFunc &filterFunc) const { using TreeNodePair = std::pair<bool, OcTreeNode::Ptr>; std::deque<TreeNodePair> possiblePairs; possiblePairs.push_back(std::make_pair(false, m_Root)); while (!possiblePairs.empty()) { // pop next possible node. TreeNodePair currentPair = possiblePairs.back(); possiblePairs.pop_back(); // procced bool tested = currentPair.first; OcTreeNode::Ptr treeNode = currentPair.second; if (treeNode->GetTotalSceneNodeCount() > 0) { Side result = tested ? Side::IN : collider.IsInside(treeNode->GetAABB()); if (result != Side::OUT) { if (result == Side::IN) tested = true; // test currentTreeNode's belonging sceneNodes int sceneNodeCount = treeNode->GetSceneNodeCount(); for (int i = 0; i < sceneNodeCount; i++) { SceneNode::Ptr sceneNode = treeNode->GetSceneNodeAt(i); if (tested || collider.IsInsideFast(sceneNode->GetWorldAABB())) filterFunc(sceneNode); } // add currentTreeNode's childs to possiblePairs vector. for (int i = 0; i < 8; i++) { OcTreeNode::Ptr childNode = treeNode->GetChildAt(i); if (childNode != nullptr && childNode->GetTotalSceneNodeCount() > 0) possiblePairs.push_back(std::make_pair(tested, childNode)); } } } } }
void buildTestScene1() { float w,h,f; w = 9.0f; h = 6.0f; f = 6.0f; cam = PinholeCamera(Vec3(0.0f,0.0f,0.0f), Vec3(0.0f,0.0f,-5.0f), Vec3(0.0f,1.0f,0.0f),f,w,h); buffer = RayBuffer(); PointLight *l; l = new PointLight(Vec3(1.0f,0.0f,1.0f),Col(1.0f,1.0f,1.0f), 15, 1, 1, 1); lights.addLight(*l); l = new PointLight(Vec3(-3.0f,3.0f,-1.0f),Col(1.0f,1.0f,1.0f), 10, 1, 1, 1); lights.addLight(*l); l = new PointLight(Vec3(2.0f,0.0f,0.0f),Col(0.30f,0.30f,0.30f), 4, 1, 1, 1); lights.addLight(*l); std::vector<Collidable *> *vec = new std::vector<Collidable *>(); Collidable *c; c = new Sphere(Vec3(0.0f, .5f, -4.20f), 1.0f, Material(1)); c->precomputeBounds(); vec->push_back(c); c = new Sphere(Vec3(-0.0f, -1.0f, -3.0f), 0.5f, Material(2)); c->precomputeBounds(); vec->push_back(c); c = new Sphere(Vec3(-0.0f, 0.00f, -0.50f), 0.1f, Material(3)); c->precomputeBounds(); vec->push_back(c); c = new Sphere(Vec3(0.7f, 0.30f, -2.5f), 0.5f, Material(4)); c->precomputeBounds(); //vec->push_back(c); c = new Sphere(Vec3(02.0f, 0.00f, -5.0f), 0.70f, Material(4)); c->precomputeBounds(); vec->push_back(c); c = new Sphere(Vec3(0.9f, 0.00f, -02.50f), 0.50f, Material(7)); c->precomputeBounds(); vec->push_back(c); sceneTree.buildTreeStart(*vec); sceneTree.isLeaf = true; sceneTree.items = vec; sceneTree.xMin = -100; sceneTree.xMax = 100; sceneTree.yMin = -100; sceneTree.yMax = 100; sceneTree.zMin = -100; sceneTree.zMax = 100; sceneTree.axis = -1; sceneTree.depth = 0; sceneTree.id = 55; }
bool Collidable::collides_with(Collidable &c) { if (c.collidable_get_type() == COLLIDABLE_POINT) { General::Point<float> pos; c.collidable_get_position(pos); return collides_with_point(pos); } else if (c.collidable_get_type() == COLLIDABLE_BOX) { General::Point<float> pos; General::Point<int> offset; General::Size<int> size; c.collidable_get_position(pos); c.collidable_get_box(offset, size); return collides_with_box(pos, offset, size); } else { // COLLIDABLE_BONES General::Point<float> pos; std::vector<Bones::Bone> bones; c.collidable_get_position(pos); c.collidable_get_bones(bones); return collides_with_bones(pos, bones); } // Should never get here General::log_message("IMPOSSIBLE CONDITION: collides_with unknown"); return false; }
bool8_t Collidable::IsColliding(Collidable other) { bool right = other.GetPosition().x > GetPosition().x + GetWidth(); bool left = other.GetPosition().x + other.GetWidth() < GetPosition().x; bool up = other.GetPosition().y + other.GetHeight() < GetPosition().y; bool down = other.GetPosition().y > GetPosition().y + GetHeight(); return right || left || up || down; }
bool Collision::Check(const Collidable& a, const Collidable& b, Vector *v) { if((a.collisionBits & b.collisionBits) == 0) return false; switch(a.getShape()) { case COLL_AABB: switch(b.getShape()) { case COLL_AABB: return AABB_vs_AABB((const AABB&)a, (const AABB&)b, v); case COLL_CIRCLE: return AABB_vs_Circle((const AABB&)a, (const Circle&)b, v); case COLL_LINE: return AABB_vs_Line((const AABB&)a, (const Line&)b, v); } break; case COLL_CIRCLE: switch(b.getShape()) { case COLL_AABB: return AABB_vs_Circle((const AABB&)b, (const Circle&)a, v); case COLL_CIRCLE: return Circle_vs_Circle((const Circle&)a, (const Circle&)b, v); case COLL_LINE: return Circle_vs_Line((const Circle&)a, (const Line&)b, v); } break; case COLL_LINE: switch(b.getShape()) { case COLL_AABB: return AABB_vs_Line((const AABB&)b, (const Line&)a, v); case COLL_CIRCLE: return Circle_vs_Line((const Circle&)b, (const Line&)a, v); case COLL_LINE: return Line_vs_Line((const Line&)a, (const Line&)b, v); } break; } assert(false); return false; }
bool CollisionHandler::checkCollisions(Collidable & c) { for (int i = 0; i < NUM_TYPES; i++) { List<Collidable>::Iterator iter = collidables[i].getIterator(); while(iter.hasNext()) { Collidable * c2 = iter.next(); if (&c != c2 && c.intersects(c2)) return true; } } return false; }
Actor* ActorFactory::CreateActor(tinyxml2::XMLElement* actorNode, std::string sourceName) { Actor* pActor = new Actor; pActor->SetId(GetNextActorId()); pActor->SetSource(sourceName); tinyxml2::XMLElement* propertiesNode = actorNode->FirstChildElement("Properties"); if (!propertiesNode) { LOG("Actor Factory", "Failed to load actor properties"); return NULL; } else { if (!pActor->Init(propertiesNode)) { LOG("Actor Factory", "Actor properties node loading failed"); return false; } } //-------------------------------------------------------- tinyxml2::XMLElement* componentsNode = actorNode->FirstChildElement("Components"); if (!componentsNode) { LOG("Actor Factory", "Failed to load actor components"); return NULL; } else { tinyxml2::XMLElement* rendCompNode = componentsNode->FirstChildElement("RenderComponent"); if (rendCompNode) { Renderable*pRend = new Renderable(pActor); if (!pRend->Init(rendCompNode)) { LOG("Actor Factory", "Renderable component loading failed"); return false; } pActor->AddComponent(pRend); } tinyxml2::XMLElement* moveCompNode = componentsNode->FirstChildElement("MovableComponent"); if (moveCompNode) { Movable* pMoveComp = new Movable(pActor); if (!pMoveComp->Init(moveCompNode)) { LOG("Actor Factory", "Movable component loading failed"); return false; } pActor->AddComponent(pMoveComp); } tinyxml2::XMLElement* colCompNode = componentsNode->FirstChildElement("CollisionComponent"); if (colCompNode) { //std::sting type = colCompNode->FirstChildElement("Type")->Attribute("value"); //if (type == "BOX") //{ //colComp = new Rectangle(pActor); //} //else if (type == "BOX") //{ // colComp = new Rectangle(pActor); //} Collidable* pColComp; pColComp = new Rectangle(pActor); if (!pColComp->Init(moveCompNode)) { LOG("Actor Factory", "Rectangel collidable component loading failed"); return false; } pActor->AddComponent(pColComp); } } g_pBGL->GetLevelManager()->InsertActor(pActor); return pActor; }
void buildTestScene2() { float w,h,f; w = 9.0f; h = 6.0f; f = 3.0f; float con = 2; cam = PinholeCamera(Vec3(0.0f,0.0f,0.0f), Vec3(0.0f,0.0f,-5.0f), Vec3(0.0f,1.0f,0.0f),f,w,h); buffer = RayBuffer(); PointLight *l; l = new PointLight(Vec3(0.0f, con-0.10f, -con),Col(1.0f,1.0f,1.0f), 15, 1, 1, 1); lights.addLight(*l); std::vector<Collidable *> *vec = new std::vector<Collidable *>(); Collidable *c; c = new Sphere(Vec3(0.0f, 0.0f, -2.0f), 0.50f, Material(3)); c->precomputeBounds(); vec->push_back(c); c = new Sphere(Vec3(-1.0f, -1.0f, -3.0f), 1.0f, Material(1)); c->precomputeBounds(); vec->push_back(c); //left c = new Triangle( Vec3(-con,-con,0), Vec3(-con,-con,-2*con), Vec3(-con,con,0), Material(8)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(-con,-con,-2*con), Vec3(-con,con,-2*con), Vec3(-con,con,0), Material(8)); c->precomputeBounds(); vec->push_back(c); //right c = new Triangle( Vec3(con,-con,0), Vec3(con,con,0), Vec3(con,-con,-2*con), Material(9)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(con,-con,-2*con), Vec3(con,con,0), Vec3(con,con,-2*con), Material(9)); c->precomputeBounds(); vec->push_back(c); //back c = new Triangle( Vec3(-con,con,-2*con), Vec3(-con,-con,-2*con), Vec3(con,-con,-2*con), Material(10)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(-con,con,-2*con), Vec3(con,-con,-2*con), Vec3(con,con,-2*con), Material(10)); c->precomputeBounds(); vec->push_back(c); //bottom c = new Triangle( Vec3(-con,-con,0), Vec3(con,-con,0), Vec3(con,-con,-2*con), Material(10)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(-con,-con,-2*con), Vec3(-con,-con,0), Vec3(con,-con,-2*con), Material(10)); c->precomputeBounds(); vec->push_back(c); //top c = new Triangle( Vec3(-con,con,0), Vec3(con,con,-2*con), Vec3(con,con,0), Material(10)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(-con,con,-2*con), Vec3(con,con,-2*con), Vec3(-con,con,0), Material(10)); c->precomputeBounds(); vec->push_back(c); //front c = new Triangle( Vec3(-con,con,con), Vec3(con,-con,con), Vec3(-con,-con,con), Material(2)); c->precomputeBounds(); vec->push_back(c); c = new Triangle( Vec3(-con,con,con), Vec3(con,con,con), Vec3(con,-con,con), Material(2)); c->precomputeBounds(); vec->push_back(c); sceneTree.buildTreeStart(*vec); sceneTree.isLeaf = true; sceneTree.items = vec; sceneTree.xMin = -100; sceneTree.xMax = 100; sceneTree.yMin = -100; sceneTree.yMax = 100; sceneTree.zMin = -100; sceneTree.zMax = 100; sceneTree.axis = -1; sceneTree.depth = 0; sceneTree.id = 55; }
bool Sprite::doesCollideWith(const Collidable& other) const { return intersecting(this->getBoundingRect(), other.getBoundingRect()); }
bool Collidable::collide(Collidable& b) { return Collision::check(getBoundingBox(), b.getBoundingBox()); }
static bool beamCheckCollision( QVector pos, float len, const Collidable &un ) { return (un.GetPosition()-pos).MagnitudeSquared() <= len*len+2*len*un.radius+un.radius*un.radius; }
bool Pong::areColliding(const Collidable &a, const Collidable &b) const { return a.getRight() >= b.getLeft() && a.getLeft() <= b.getRight() && a.getBottom() >= b.getTop() && a.getTop() <= b.getBottom(); }
bool AxisAlignedBox::Intersect(const Collidable& collidable)const { return collidable.Intersect(*this); }