bool Quaternion::FromLookRotation(const Vector3& direction, const Vector3& upDirection)
{
    Quaternion ret;
    Vector3 forward = direction.Normalized();

    Vector3 v = forward.CrossProduct(upDirection);
    // If direction & upDirection are parallel and crossproduct becomes zero, use FromRotationTo() fallback
    if (v.LengthSquared() >= M_EPSILON)
    {
        v.Normalize();
        Vector3 up = v.CrossProduct(forward);
        Vector3 right = up.CrossProduct(forward);
        ret.FromAxes(right, up, forward);
    }
    else
        ret.FromRotationTo(Vector3::FORWARD, forward);

    if (!ret.IsNaN())
    {
        (*this) = ret;
        return true;
    }
    else
        return false;
}
示例#2
0
void DebugRenderer::AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps, bool depthTest)
{
    Quaternion orientation;
    orientation.FromRotationTo(Vector3::UP, normal.Normalized());
    Vector3 p = orientation * Vector3(radius, 0, 0) + center;
    unsigned uintColor = color.ToUInt();

    for(int i = 1; i <= steps; ++i)
    {
        const float angle = (float)i / (float)steps * 360.0f;
        Vector3 v(radius * Cos(angle), 0, radius * Sin(angle));
        Vector3 c = orientation * v + center;
        AddLine(p, c, uintColor, depthTest);
        p = c;
    }

    p = center + normal * (radius / 4.0f);
    AddLine(center, p, uintColor, depthTest);
}