@@ -949,16 +949,11 @@ private void UpdateAnimation(ElapsedTime elapsedTime)
949949 // Bogie angle animation
950950 Matrix inverseLocation = Matrix . Invert ( Car . WorldPosition . XNAMatrix ) ;
951951
952- foreach ( TrainCarPart p in Car . Parts )
952+ foreach ( var p in Car . Parts )
953953 {
954- if ( p . iMatrix <= 0 || p . iMatrix >= TrainCarShape . SharedShape . Matrices . Count ( ) )
954+ if ( p . iMatrix <= 0 )
955955 continue ;
956956
957- // Seperate the rotation and translation of the part matrix to reconstruct later
958- Matrix partMat = TrainCarShape . SharedShape . Matrices [ p . iMatrix ] ;
959- partMat . Translation = Vector3 . Zero ;
960-
961- // Determine orientation of bogie in absolute space
962957 Matrix m = Matrix . Identity ;
963958
964959 // Bogie rotation calculation doesn't work on turntables
@@ -973,34 +968,20 @@ private void UpdateAnimation(ElapsedTime elapsedTime)
973968 fwd . Normalize ( ) ;
974969 Vector3 side = Vector3 . Cross ( Vector3 . Up , fwd ) ;
975970 if ( ! ( side . X == 0 && side . Y == 0 && side . Z == 0 ) )
976- side . Normalize ( ) ;
971+ side . Normalize ( ) ;
977972 Vector3 up = Vector3 . Cross ( fwd , side ) ;
978973 m . Right = side ;
979974 m . Up = up ;
980975 m . Backward = fwd ;
981-
976+
982977 // Convert absolute rotation into rotation relative to train car
983- m *= inverseLocation ;
984-
985- // Roll the bogie for superelevation about the bogie's 0,0,0
986- m . Translation = Vector3 . Zero ;
987- m = Matrix . CreateRotationZ ( p . Roll ) * m ;
988- // Reset translation to offset given by shape file
989- m . Translation = TrainCarShape . SharedShape . Matrices [ p . iMatrix ] . Translation ;
990- // Incorporate scale and rotation from shape file last to reduce distortions
991- m = partMat * m ;
992- }
993- else // Position wasn't calculated, set default
994- {
995- m = TrainCarShape . SharedShape . Matrices [ p . iMatrix ] ;
978+ m = Matrix . CreateRotationZ ( p . Roll ) * m * inverseLocation ;
996979 }
997980 }
998- else // On a turntable, set default
999- {
1000- m = TrainCarShape . SharedShape . Matrices [ p . iMatrix ] ;
1001- }
981+ // Insert correct translation (previous step likely introduced garbage data)
982+ m . Translation = TrainCarShape . SharedShape . Matrices [ p . iMatrix ] . Translation ;
1002983
1003- // Finally, cancel out any vibrations
984+ // To cancel out any vibration, apply the inverse here. If no vibration is present, this matrix will be Matrix.Identity.
1004985 TrainCarShape . XNAMatrices [ p . iMatrix ] = Car . VibrationInverseMatrix * m ;
1005986 }
1006987
0 commit comments