//-------------------------------------------------------------------------------- void Cone3f::SampleNormal( Vector3f& normal, float theta, float height ) const { // Define the axis of the cylinder, and find its length. Vector3f v = P1 - P2; Vector3f vnorm = Vector3f::Normalize( v ); float l = v.Magnitude(); Vector3f up = Vector3f( 0.0f, 1.0f, 0.0f ); // Find the unit vector perpendicular to the axis, that is pointing closest to the (0,1,0) direction. // If the axis points in the (0,1,0) direction, then use (1,0,0) as the unit vector. Vector3f unit; if ( vnorm == up || vnorm == -up ) { unit = Vector3f( 1.0f, 0.0f, 0.0f ); } else { Vector3f temp = Vector3f::Cross( vnorm, up ); unit = Vector3f::Cross( temp, vnorm ); unit.Normalize(); } float slope = ( R2 - R1 ) / l; float radius = R2 + slope * height * l; // Calculate the position, which is a combination of an offset to the p2 endpoint, // shifted along the axis of the cylinder, and a rotated and scaled radius component. Matrix3f r; r.RotationEuler( vnorm, theta ); // Calculate the normal vector, which is dependent on the delta in radii at each // end of the cylinder. normal = r * unit * radius + vnorm * slope * radius; normal.Normalize(); }
const Vector3f GravitationalForce::CalculateForce( const Body &a, const Body &b ) const { const Vector3f vDiff = b.Position - a.Position; const float numerator = Constant * ( a.Mass * b.Mass ); const float denominator = std::pow( vDiff.Magnitude(), 2 ); return vDiff.Normalised() * ( numerator / denominator ); }
void Camera::LookAt(const Vector3f &point, Vector3f up) { m_look=m_position-point; if(up.Magnitude()!=0.0f) { m_up=up; } m_right=m_up.Cross(m_look); }
bool Intersect::BoundsSphere (const Bounds& bounds, const Vector3f& pos, float radius) { const Vector3f& center = bounds.GetCenter(); Vector3f dir (center - pos); float mag = dir.Magnitude(); if (mag > radius) { dir /= mag; dir *= radius; return bounds.Contains(pos + dir); } return true; }
void Arcball::Drag(const Point3f* _point, Quaternion* qNewRotation) { MapToSphere(_point, &endVector); if(qNewRotation) { Vector3f vPerpendicular; //compute the vector perpendicular to the begin and end vectors vPerpendicular = Vector3f::Cross(&startVector, &endVector); //compute the length of the perpendicular vecotr if(vPerpendicular.Magnitude() > 0) { //return the perpendicular vector as the transform *qNewRotation = Quaternion(vPerpendicular.X(), vPerpendicular.Y(), vPerpendicular.Z(), Vector3f::Dot(&startVector, &endVector)); } else { //the begin and end vectors coincide, so return an identity transform *qNewRotation = Quaternion(0, 0, 0, 0); } } }