@@ -778,16 +778,17 @@ protected override void ZoomIn(float speed)
778
778
}
779
779
}
780
780
781
- public class ViewerCamera : FreeRoamCamera
781
+ public class OrbitingCamera : FreeRoamCamera
782
782
{
783
- Vector3 RotationOrigin ;
784
- Vector3 RotationDirection ;
785
- WorldLocation RotationLocation ;
786
- float RotationRadius ;
787
- float RotationReferenceAngleX ;
788
- float RotationReferenceAngleY ;
789
-
790
- public ViewerCamera ( Viewer viewer )
783
+ Vector3 StoredOrigin ;
784
+ Vector3 StoredDirection = Vector3 . UnitZ ;
785
+ Vector3 StoredForward = Vector3 . UnitZ ;
786
+ WorldLocation StoredLocation ;
787
+ float StoredRadius ;
788
+ float StoredAngleX ;
789
+ float StoredAngleY ;
790
+
791
+ public OrbitingCamera ( Viewer viewer )
791
792
: base ( viewer )
792
793
{
793
794
}
@@ -827,44 +828,40 @@ public override void RotateByMouse()
827
828
RotationXRadians = MathHelper . WrapAngle ( RotationXRadians + GetMouseDelta ( UserInput . MouseMoveY ) ) ;
828
829
RotationYRadians = MathHelper . WrapAngle ( RotationYRadians + GetMouseDelta ( UserInput . MouseMoveX ) ) ;
829
830
830
- // Method 1
831
- //var deltaAngleX = MathHelper.WrapAngle(GetMouseDelta(UserInput.MouseMoveY));
832
- //var deltaAngleY = MathHelper.WrapAngle(GetMouseDelta(UserInput.MouseMoveX));
833
- //var direction = Vector3.Normalize(XnaLocation(CameraWorldLocation) - RotationOrigin);
834
- //var transform = Matrix.CreateFromYawPitchRoll(-deltaAngleY, -deltaAngleX, 0);
835
- //var newLocation = RotationOrigin + RotationRadius * Vector3.Transform(direction, transform);
836
- //newLocation.Z *= -1;
837
- //var newWorldLocation = CameraWorldLocation;
838
- //newWorldLocation.Location = newLocation;
831
+ var deltaAngleX = MathHelper . WrapAngle ( RotationXRadians - StoredAngleX ) ;
832
+ var deltaAngleY = MathHelper . WrapAngle ( RotationYRadians - StoredAngleY ) ;
839
833
840
- // Method 2
841
- // var deltaAngleX = MathHelper.WrapAngle(RotationXRadians - RotationReferenceAngleX) ;
842
- // var deltaAngleY = MathHelper.WrapAngle(RotationYRadians - RotationReferenceAngleY );
843
- //var transform = Matrix.CreateFromYawPitchRoll(-deltaAngleY, -deltaAngleX, 0 );
844
- //var newLocation = RotationOrigin + RotationRadius * Vector3.Transform(RotationDirection, transform );
834
+ var dax = StoredForward . Z * deltaAngleX ;
835
+ var daz = StoredForward . X * deltaAngleX ;
836
+ var transform = Matrix . CreateRotationX ( dax ) ;
837
+ transform * = Matrix . CreateRotationZ ( daz ) ;
838
+ transform *= Matrix . CreateRotationY ( - deltaAngleY ) ;
845
839
846
- // Method 3
847
- //var deltaAngleX = MathHelper.WrapAngle(RotationXRadians);
848
- //var deltaAngleY = MathHelper.WrapAngle(RotationYRadians);
849
- //var transform = Matrix.CreateFromYawPitchRoll(-deltaAngleY, -deltaAngleX, 0);
850
- //var newLocation = RotationOrigin + RotationRadius * Vector3.Transform(Vector3.UnitZ, transform);
840
+ // FIXME: The math is still wrong here somewhere...
851
841
852
- //newLocation.Z *= -1 ;
853
- //var newWorldLocation = RotationLocation ;
854
- // newWorldLocation.Location = newLocation ;
842
+ var newWorldLocation = StoredLocation ;
843
+ newWorldLocation . Location = StoredOrigin + StoredRadius * Vector3 . Transform ( StoredDirection , transform ) ;
844
+ newWorldLocation . Location . Z *= - 1 ;
855
845
856
- //SetLocation( newWorldLocation) ;
846
+ cameraLocation = newWorldLocation ;
857
847
}
858
848
859
849
public void StoreRotationOrigin ( Vector3 rotationOrigin )
860
850
{
861
- RotationOrigin = Viewer . TerrainPoint ;
862
- RotationLocation = CameraWorldLocation ;
863
- RotationDirection = XnaLocation ( CameraWorldLocation ) - RotationOrigin ;
864
- RotationRadius = RotationDirection . Length ( ) ;
865
- RotationDirection = Vector3 . Normalize ( RotationDirection ) ;
866
- RotationReferenceAngleX = MathHelper . WrapAngle ( RotationXRadians ) ;
867
- RotationReferenceAngleY = MathHelper . WrapAngle ( RotationYRadians ) ;
851
+ StoredOrigin = Viewer . TerrainPoint ;
852
+ StoredLocation = CameraWorldLocation ;
853
+ StoredDirection = XnaLocation ( CameraWorldLocation ) - StoredOrigin ;
854
+ StoredRadius = StoredDirection . Length ( ) ;
855
+ StoredDirection = Vector3 . Normalize ( StoredDirection ) ;
856
+ StoredAngleX = MathHelper . WrapAngle ( RotationXRadians ) ;
857
+ StoredAngleY = MathHelper . WrapAngle ( RotationYRadians ) ;
858
+ StoredForward = xnaView . Forward ;
859
+ }
860
+
861
+ public void SetOrientation ( float rotationXRadians , float rotationYRadians )
862
+ {
863
+ RotationXRadians = rotationXRadians ;
864
+ RotationYRadians = rotationYRadians ;
868
865
}
869
866
870
867
public void PanByMouse ( )
0 commit comments