bool BoundingEllipsoid::intersects(BoundingBox &bx, CollisionResultSet::Info& info) { math::Ray r1(center, bx.getCenter()); math::Ray r2(bx.getCenter(), center); math::Vec3f near1, near2, far1, far2; if (!intersectsWhere(r1, near1, far1, info)) return false; if (!bx.intersectsWhere(r2, near2, far2, info)) return false; math::Vec3f d1 = far1 - center; math::Vec3f d2 = far2 - bx.getCenter(); return (center.computeDistSquared(bx.getCenter()) - d1.magnSquared() - d2.magnSquared()) <= 0; }
bool MovingEntity::VerticalCollision(Vector3 pos) { bool colided = false; for (int i = 0;i < 36;i++) { BlockPosition *bp = &_blockPositions[i]; if (_world->IsBlockCollidable(bp->position.x,bp->position.y,bp->position.z)) { BoundingBox blockBox = BoundingBox(Vector3(bp->position.x,bp->position.y,bp->position.z),Vector3(bp->position.x+1,bp->position.y+1,bp->position.z+1)); if (blockBox.contains(pos)) { colided = true; float moveDirection = pos.y - blockBox.getCenter().y; if (moveDirection > 0.0f) { _position.y += (0.5f - moveDirection); }else if (moveDirection < 0.0f) { _position.y -= (0.51f + moveDirection); } } } } return colided; }
TreeNode(BoundingBox box) { _mass = 0; _centerOfMass = Vec2D(); //_state = state; _NW = _SW = _NE = _SE = NULL; _box = box; //cout << "NODE CENTER " << box.getCenter().getX() << " " << box.getCenter().getY() << endl; _position = _box.getCenter(); }
bool collision(BoundingBox& a, BoundingBox& b) { vec2 Axis = a.getAxis(); vec2 mid = (a.getCenter() + b.getCenter()) / 2.0; vec2 A = mid - (1000*Axis); vec2 B = mid + (1000*Axis); Axis = normal(Axis); vec2 C = mid - (1000*Axis); vec2 D = mid + (1000*Axis); Axis = b.getAxis(); vec2 E = mid - (1000*Axis); vec2 F = mid + (1000*Axis); Axis = normal(Axis); vec2 G = mid - (1000*Axis); vec2 H = mid + (1000*Axis); return (collide(a.calc(A,B),b.calc(A,B)) && collide(a.calc(C,D),b.calc(C,D)) && collide(a.calc(E,F),b.calc(E,F)) && collide(a.calc(G,H),b.calc(G,H))); }
Sphere::Sphere( bool fullSphere, byte numSubDiv, float dim ) { setPrimitiveType( PrimitiveType::Triangles ); VertexData position; buildGeometry( fullSphere, numSubDiv, position, dim ); // Build Texture Coordinates. BoundingBox box; for( size_t i = 0; i < position.size(); i++ ) { const Vector3& v = position[i]; box.add(v); } Vector3 center = box.getCenter(); std::vector<Vector3> texCoords; for( size_t i = 0; i < position.size(); i++ ) { const Vector3& vert = position[i]; Vector3 d = vert-center; d.normalize(); // Conveert to spherical coordinates. //float t = d.z / sqrt(d.x*d.x+d.y*d.y+d.z*d.z); //float delta = acos(t); //float phi = atan2(d.y, d.x); //float u = delta / Math::PI; //float v = phi / 2*Math::PI; float u = std::asin(d.x) / PI + 0.5f; float v = std::asin(d.y) / PI + 0.5f; texCoords.push_back( Vector2(u, v) ); } gb->set( VertexAttribute::Position, position ); gb->set( VertexAttribute::TexCoord0, texCoords ); }
TreeNode(BoundingBox box, NodeState state) { _mass = 0; _centerOfMass = Vec2D(); _parent = NULL; _state = state; if (state == INTERNAL) { _NW = new TreeNode(box.getQuadrant(1), EXTERNAL); _NE = new TreeNode(box.getQuadrant(2), EXTERNAL); _SW = new TreeNode(box.getQuadrant(3), EXTERNAL); _SE = new TreeNode(box.getQuadrant(4), EXTERNAL); _SE->setParent(this); _NW->setParent(this); _NE->setParent(this); _SW->setParent(this); } else { _NW = _SW = _NE = _SE = NULL; } _body = NULL; _box = box; //cout << "NODE CENTER " << box.getCenter().getX() << " " << box.getCenter().getY() << endl; _position = box.getCenter(); visited = false; }
int lua_BoundingBox_getCenter(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { do { if ((lua_type(state, 1) == LUA_TUSERDATA)) { BoundingBox* instance = getInstance(state); void* returnPtr = (void*)new Vector3(instance->getCenter()); if (returnPtr) { gameplay::ScriptUtil::LuaObject* object = (gameplay::ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(gameplay::ScriptUtil::LuaObject)); object->instance = returnPtr; object->owns = true; luaL_getmetatable(state, "Vector3"); lua_setmetatable(state, -2); } else { lua_pushnil(state); } return 1; } } while (0); lua_pushstring(state, "lua_BoundingBox_getCenter - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } case 2: { do { if ((lua_type(state, 1) == LUA_TUSERDATA) && (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL)) { // Get parameter 1 off the stack. bool param1Valid; gameplay::ScriptUtil::LuaArray<Vector3> param1 = gameplay::ScriptUtil::getObjectPointer<Vector3>(2, "Vector3", false, ¶m1Valid); if (!param1Valid) break; BoundingBox* instance = getInstance(state); instance->getCenter(param1); return 0; } } while (0); lua_pushstring(state, "lua_BoundingBox_getCenter - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1 or 2)."); lua_error(state); break; } } return 0; }
bool BoundingBox::overlapsWith(const BoundingBox& other) { if(!use || !other.use) return false; float wa = getWidth()/2; float ha = getHeight()/2; float da = getDepth()/2; glm::vec3 Ca = getCenter(); glm::vec3 Az = getFrontNormal(); glm::vec3 Ay = getUpNormal(); glm::vec3 Ax = getRightNormal(); glm::vec3 Cb = other.getCenter(); glm::vec3 Bz = other.getFrontNormal(); glm::vec3 By = other.getUpNormal(); glm::vec3 Bx = other.getRightNormal(); float wb = other.getWidth()/2; float hb = other.getHeight()/2; float db = other.getDepth()/2; glm::vec3 T = Cb - Ca; float lhs = 0, rhs = 0; float Rxx = glm::dot(Ax, Bx); float Rxy = glm::dot(Ax, By); float Rxz = glm::dot(Ax, Bz); // Case 1: L = Ax lhs = glm::dot(T, Ax); rhs = wa + fabs(wb * Rxx) + fabs(hb * Rxy) + fabs(db * Rxz); if(lhs > rhs) return false; float Ryx = glm::dot(Ay, Bx); float Ryy = glm::dot(Ay, By); float Ryz = glm::dot(Ay, Bz); // Case 2: L = Ay lhs = glm::dot(T, Ay); rhs = ha + fabs(wb * Ryx) + fabs(hb * Ryy) + fabs(db * Ryz); if(lhs > rhs) return false; float Rzx = glm::dot(Az, Bx); float Rzy = glm::dot(Az, By); float Rzz = glm::dot(Az, Bz); // Case 3: L = Az lhs = glm::dot(T, Az); rhs = da + fabs(wb * Rzx) + fabs(hb * Rzy) + fabs(db * Rzz); if(lhs > rhs) return false; // Case 4: L = Bx lhs = glm::dot(T, Bx); rhs = fabs(wa * Rxx) + fabs(ha * Ryx) + fabs(da * Rzx) + wb; if(lhs > rhs) return false; // Case 5: L = By lhs = glm::dot(T, By); rhs = fabs(wa * Rxy) + fabs(ha * Ryy) + fabs(da * Rzy) + hb; if(lhs > rhs) return false; // Case 6: L = Bz lhs = glm::dot(T, Bz); rhs = fabs(wa * Rxz) + fabs(ha * Ryz) + fabs(da * Rzz) + db; if(lhs > rhs) return false; // Case 7: L = Ax x Bx lhs = glm::dot(T, glm::cross(Ax, Bx)); rhs = fabs(ha * Rzx) + fabs(da * Ryx) + fabs(hb * Rxz) + fabs(db * Rxy); if(lhs > rhs) return false; // Case 8: L = Ax x By lhs = glm::dot(T, glm::cross(Ax, By)); rhs = fabs(ha * Rzy) + fabs(da * Ryy) + fabs(wb * Rxz) + fabs(db * Rxx); if(lhs > rhs) return false; // Case 9: L = Ax x Bz lhs = glm::dot(T, glm::cross(Ax, Bz)); rhs = fabs(ha * Rzz) + fabs(da * Ryz) + fabs(wb * Rxy) + fabs(hb * Rxx); if(lhs > rhs) return false; // Case 10: L = Ay x Bx lhs = glm::dot(T, glm::cross(Ay, Bx)); rhs = fabs(wa * Rzx) + fabs(da * Rxx) + fabs(hb * Ryz) + fabs(db * Ryy); if(lhs > rhs) return false; // Case 11: L = Ay x By lhs = glm::dot(T, glm::cross(Ay, By)); rhs = fabs(wa * Rzy) + fabs(da * Rxy) + fabs(wb * Ryz) + fabs(db * Ryx); if(lhs > rhs) return false; // Case 12: L = Ay x Bz lhs = glm::dot(T, glm::cross(Ay, Bz)); rhs = fabs(wa * Rzz) + fabs(da * Rxz) + fabs(wb * Ryy) + fabs(hb * Ryx); if(lhs > rhs) return false; // Case 13: L = Az x Bx lhs = glm::dot(T, glm::cross(Az, Bx)); rhs = fabs(wa * Ryx) + fabs(ha * Rxx) + fabs(hb * Rzz) + fabs(db * Rzy); if(lhs > rhs) return false; // Case 14: L = Az x By lhs = glm::dot(T, glm::cross(Az, By)); rhs = fabs(wa * Ryy) + fabs(ha * Rxy) + fabs(wb * Rzz) + fabs(db * Rzx); if(lhs > rhs) return false; // Case 15: L = Az x Bz lhs = glm::dot(T, glm::cross(Az, Bz)); rhs = fabs(wa * Ryz) + fabs(ha * Rxz) + fabs(wb * Rzy) + fabs(hb * Rzx); if(lhs > rhs) return false; return true; }
bool BVHTree::sortAlgZ(BoundingBox b1, BoundingBox b2) { return b1.getCenter()(Z) > b2.getCenter()(Z); }
BoundingSphere::BoundingSphere(const BoundingBox& box) : center(box.getCenter()) , radius((box.getCenter() - box.max.x).length()) { }