private void RotateCameraAroundLookDir(double curX, double curY, double dx, double dy) { const double ROTATESPEED = .005d; // Calculate angle //double radians = ROTATESPEED * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)); double radians = ROTATESPEED * Math.Sqrt((dx * dx) + (dy * dy)); double degrees = Math1D.RadiansToDegrees(radians); if (radians < 0) { MessageBox.Show("ya"); } // See if I should negate the angle //NOTE: This logic is flawed. I fixed this later by taking curXY cross prevXY if (curX >= 0 && curY >= 0) { // Q1 if (dx > 0 || dy < 0) { degrees *= -1; } } else if (curX <= 0 && curY >= 0) { // Q2 if (dx > 0 || dy > 0) { degrees *= -1; } } else if (curX <= 0 && curY <= 0) { // Q3 if (dx < 0 || dy > 0) { degrees *= -1; } } else if (curX >= 0 && curY <= 0) { // Q4 if (dx < 0 || dy < 0) { degrees *= -1; } } // Create a matrix that will perform the rotation Matrix3D matrix = new Matrix3D(); matrix.Rotate(new Quaternion(_camera1.LookDirection, degrees)); // Rotate the camera _camera1.UpDirection = matrix.Transform(_camera1.UpDirection); }
/// <summary> /// This converts 2D movement into a spherical rotation. This is used for converting mouse movement into rotating an object /// </summary> public static Quaternion GetSphericalMovement(double dx, double dy) { //TODO: Put this in Math3D const double ROTATESPEED = .01d; // Get axis of rotation double mouseAngle = 0d; if (dx != 0d && dy != 0d) { mouseAngle = Math.Asin(Math.Abs(dy) / Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2))); if (dx < 0 && dy > 0) { mouseAngle += Math.PI / 2; } else if (dx < 0 && dy < 0) { mouseAngle += Math.PI; } else if (dx > 0 && dy < 0) { mouseAngle += Math.PI * 1.5; } } else if (dx == 0 && dy != 0) { mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5; } else if (dx != 0 && dy == 0) { mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI; } double axisAngle = mouseAngle + Math.PI / 2; Vector3D axis = new Vector3D(Math.Cos(axisAngle) * 4, Math.Sin(axisAngle) * 4, 0); // Get angle double radians = ROTATESPEED * Math.Sqrt((dx * dx) + (dy * dy)); double degrees = Math1D.RadiansToDegrees(radians); // Exit Function return(new Quaternion(axis, degrees)); }
private void joint_Hinge(object sender, CHingeEventArgs e) { double newAngle = Math1D.RadiansToDegrees(NewtonJoint.HingeAngle); double angle = this.Angle + AngleDiffence(this.Angle, newAngle); SetValue(AnglePropertyKey, angle); if (this.SetAngle != null) { e.Desc.m_Accel = NewtonJoint.HingeCalculateStopAlpha(e.Desc, Math1D.DegreesToRadians((float)MathUtils.MinMax(this.MinAngle, (double)this.SetAngle, this.MaxAngle))) * (float)this.SetAngleStiffness; e.ApplyConstraint = true; } else if ((this.MinAngle != null) && (angle < (double)this.MinAngle)) { e.Desc.m_Accel = NewtonJoint.HingeCalculateStopAlpha(e.Desc, (float)Math1D.DegreesToRadians((double)this.MinAngle)); e.ApplyConstraint = true; } else if ((this.MaxAngle != null) && (angle > (double)this.MaxAngle)) { e.Desc.m_Accel = NewtonJoint.HingeCalculateStopAlpha(e.Desc, (float)Math1D.DegreesToRadians((double)this.MaxAngle)); e.ApplyConstraint = true; } else { if (this.AngularDamperning != 0) { // -(NewtonJoint.HingeOmega * e.Desc.m_Timestep * (float)this.AngularDamperning); e.Desc.m_Accel = -(NewtonJoint.HingeOmega / e.Desc.m_Timestep) * (float)AngularDamperning; e.ApplyConstraint = true; } if (this.Torque != 0) { e.Desc.m_Accel += (float)this.Torque; e.ApplyConstraint = true; } } }