float Plane::DistanceFromSphere( const Sphere & sphere ) const { float distance = Normal() * sphere.GetCenter() + d; if ( distance > sphere.GetRadius() ) { return distance - sphere.GetRadius(); } if ( distance < -sphere.GetRadius() ) { return distance + sphere.GetRadius(); } else { return 0.0f; } }
Box Box::FromSphere(const Sphere &sphere) { Box b; b.SetMin(sphere.GetCenter() - Vector3(sphere.GetRadius())); b.SetMax(sphere.GetCenter() + Vector3(sphere.GetRadius())); return b; }
void TextView::Update(Observable* o_val) { Sphere *sphere = (Sphere*)o_val; cout << "The radius of the sphere is " << sphere->GetRadius() << endl; cout << "The area of the sphere is " << sphere->GetArea() << endl; cout << "The volume of the sphere is " << sphere->GetVolume() << endl; }
bool Renderer::RaySphereIntersection(Ray ray, Sphere sphere) { Vector d = ray.GetDirection(); Vector e = ray.GetOrigin(); Vector c = sphere.GetCenter(); int R = sphere.GetRadius(); float discriminant = (d ^ (e - c)) * (d ^ (e - c)) - ((d ^ d) * (((e - c) ^ (e - c)) - R * R)); if(discriminant >= 0.0) return true; return false; }
void InternalPhysics::Update() { int objectNumber = 0; // Testing collisions after computing following positions list<Object*>::iterator anObject = mCollisionalObjects.begin(); while( anObject != mCollisionalObjects.end() ) //Test collisions { // We don't test collisions for planes if( (*anObject)->GetType() != PLANE) { // If it's a moving shape, we test collisions TestCollision(&anObject,objectNumber); } ++anObject; objectNumber++; } //Managing the collisions list<Intersection>::iterator anIntersection = mListIntersection.begin(); while( anIntersection != mListIntersection.end() ) { // Object getting Object* A,*B; A = (*anIntersection).mA; B = (*anIntersection).mB; // Type getting int typeA = A->GetType(); int typeB = B->GetType(); // Managing the sphere/sphere collision if( typeA == SPHERE && typeB == SPHERE ) { ManageSphereSphereCollision ( (Sphere*) A, (Sphere*) B); } // Managing the sphere/plane collision if( typeA == SPHERE && typeB == PLANE) { ManageSpherePlaneCollision( (Sphere*) A, (Plane *) B); } // Moving to the next one anIntersection++; } mListIntersection.clear(); // Updating speeds and positions anObject = mCollisionalObjects.begin(); while( anObject != mCollisionalObjects.end() ) { // Only if not a plane if( (*anObject)->GetType() != PLANE ) { (*anObject)->SetVelocity( (*anObject)->GetNewVelocity() ); (*anObject)->SetPosition( (*anObject)->GetNewPosition() ); // Adding a break force if( (*anObject)->GetType() == SPHERE ) { Vector3 velocity = (*anObject)->GetVelocity(); Sphere *sphere = (Sphere*)(*anObject); // Applying Stockes lay Vector3 airBreak = -6.0 * Inertia::Pi * Inertia::Nu * sphere->GetRadius() * velocity; Vector3 applicationPoint(sphere->GetPosition()+velocity.Normalize()*sphere->GetRadius()); Force airBreak2(airBreak,applicationPoint); sphere->AddNewVariableForce(airBreak2); } } ++anObject; } }