Ejemplo n.º 1
0
        private void mapToSphere(Point point, Vector3f vector)
        {
            Point2f tempPoint = new Point2f(point.X, point.Y);

            //Adjust point coords and scale down to range of [-1 ... 1]
            tempPoint.x = (tempPoint.x * this.adjustWidth) - 1.0f;
            tempPoint.y = 1.0f - (tempPoint.y * this.adjustHeight);

            //Compute square of the length of the vector from this point to the center
            float length = (tempPoint.x * tempPoint.x) + (tempPoint.y * tempPoint.y);

            //If the point is mapped outside the sphere... (length > radius squared)
            if (length > 1.0f)
            {
                //Compute a normalizing factor (radius / sqrt(length))
                float norm = (float)(1.0 / Math.Sqrt(length));

                //Return the "normalized" vector, a point on the sphere
                vector.x = tempPoint.x * norm;
                vector.y = tempPoint.y * norm;
                vector.z = 0.0f;
            }
            //Else it's inside
            else
            {
                //Return a vector to a point mapped inside the sphere sqrt(radius squared - length)
                vector.x = tempPoint.x;
                vector.y = tempPoint.y;
                vector.z = (float)System.Math.Sqrt(1.0f - length);
            }
        }
Ejemplo n.º 2
0
        //Mouse drag, calculate rotation
        public void drag(Point NewPt, Quat4f NewRot)
        {
            //Map the point to the sphere
            this.mapToSphere(NewPt, EnVec);

            //Return the quaternion equivalent to the rotation
            if (NewRot != null)
            {
                Vector3f Perp = new Vector3f();

                //Compute the vector perpendicular to the begin and end vectors
                Vector3f.cross(Perp, StVec, EnVec);

                //Compute the length of the perpendicular vector
                if (Perp.length() > Epsilon)
                //if its non-zero
                {
                    //We're ok, so return the perpendicular vector as the transform after all
                    NewRot.x = Perp.x;
                    NewRot.y = Perp.y;
                    NewRot.z = Perp.z;
                    //In the quaternion values, w is cosine (theta / 2), where theta is the rotation angle
                    NewRot.w = Vector3f.dot(StVec, EnVec);
                }
                //if it is zero
                else
                {
                    //The begin and end vectors coincide, so return an identity transform
                    NewRot.x = NewRot.y = NewRot.z = NewRot.w = 0.0f;
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Cross Product of Two Vectors.
 /// </summary>
 /// <param name="Result">Resultant Vector</param>
 /// <param name="v1">Vector 1</param>
 /// <param name="v2">Vector 2</param>
 public static void cross(Vector3f Result, Vector3f v1, Vector3f v2)
 {
     Result.x = (v1.y * v2.z) - (v1.z * v2.y);
     Result.y = (v1.z * v2.x) - (v1.x * v2.z);
     Result.z = (v1.x * v2.y) - (v1.y * v2.x);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Dot Product of Two Vectors.
 /// </summary>
 /// <param name="v1">Vector 1</param>
 /// <param name="v2">Vector 2</param>
 /// <returns></returns>
 public static float dot(Vector3f v1, Vector3f v2)
 {
     return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
 }
Ejemplo n.º 5
0
        private Vector3f StVec; //Saved click vector

        #endregion Fields

        #region Constructors

        public arcball(float NewWidth, float NewHeight)
        {
            StVec = new Vector3f();
            EnVec = new Vector3f();
            setBounds(NewWidth, NewHeight);
        }