OpenBve.TrainManager.UpdateFrontBogieObjects C# (CSharp) Method

UpdateFrontBogieObjects() private static method

private static UpdateFrontBogieObjects ( Train Train, int CarIndex, double TimeElapsed, bool ForceUpdate ) : void
Train Train
CarIndex int
TimeElapsed double
ForceUpdate bool
return void
		private static void UpdateFrontBogieObjects(Train Train, int CarIndex, double TimeElapsed, bool ForceUpdate)
		{
			//Same hack: Check if any car sections are defined for the offending bogie
			if (Train.Cars[CarIndex].FrontBogie.CarSections.Length == 0)
			{
				return;
			}
			// calculate positions and directions for section element update
			int c = CarIndex;
			double dx = Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.X - Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.X;
			double dy = Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.Y - Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.Y;
			double dz = Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.Z - Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.Z;
			double t = dx * dx + dy * dy + dz * dz;
			double ux, uy, uz, sx, sy, sz;
			if (t != 0.0)
			{
				t = 1.0 / Math.Sqrt(t);
				dx *= t; dy *= t; dz *= t;
				ux = Train.Cars[c].FrontBogie.Up.X;
				uy = Train.Cars[c].FrontBogie.Up.Y;
				uz = Train.Cars[c].FrontBogie.Up.Z;
				sx = dz * uy - dy * uz;
				sy = dx * uz - dz * ux;
				sz = dy * ux - dx * uy;
			}
			else
			{
				ux = 0.0; uy = 1.0; uz = 0.0;
				sx = 1.0; sy = 0.0; sz = 0.0;
			}
			double px = 0.5 * (Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.X + Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.X);
			double py = 0.5 * (Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.Y + Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.Y);
			double pz = 0.5 * (Train.Cars[c].FrontBogie.FrontAxle.Follower.WorldPosition.Z + Train.Cars[c].FrontBogie.RearAxle.Follower.WorldPosition.Z);
			double d = 0.5 * (Train.Cars[c].FrontBogie.FrontAxlePosition + Train.Cars[c].FrontBogie.RearAxlePosition);
			px -= dx * d;
			py -= dy * d;
			pz -= dz * d;
			// determine visibility
			double cdx = px - World.AbsoluteCameraPosition.X;
			double cdy = py - World.AbsoluteCameraPosition.Y;
			double cdz = pz - World.AbsoluteCameraPosition.Z;
			double dist = cdx * cdx + cdy * cdy + cdz * cdz;
			double bid = Interface.CurrentOptions.ViewingDistance + Train.Cars[c].Length;
			Train.Cars[c].FrontBogie.CurrentlyVisible = dist < bid * bid;
			// brightness
			byte dnb;
			{
				float Brightness = (float)(Train.Cars[c].Brightness.NextTrackPosition - Train.Cars[c].Brightness.PreviousTrackPosition);
				if (Brightness != 0.0f)
				{
					Brightness = (float)(Train.Cars[c].FrontBogie.FrontAxle.Follower.TrackPosition - Train.Cars[c].Brightness.PreviousTrackPosition) / Brightness;
					if (Brightness < 0.0f) Brightness = 0.0f;
					if (Brightness > 1.0f) Brightness = 1.0f;
					Brightness = Train.Cars[c].Brightness.PreviousBrightness * (1.0f - Brightness) + Train.Cars[c].Brightness.NextBrightness * Brightness;
				}
				else
				{
					Brightness = Train.Cars[c].Brightness.PreviousBrightness;
				}
				dnb = (byte)Math.Round(255.0 * (double)(1.0 - Brightness));
			}
			// update current section
			int s = Train.Cars[c].FrontBogie.CurrentCarSection;
			if (s >= 0)
			{
				for (int i = 0; i < Train.Cars[c].FrontBogie.CarSections[s].Elements.Length; i++)
				{
					UpdateFrontBogieSectionElement(Train, CarIndex, s, i, new Vector3(px, py, pz), new Vector3(dx, dy, dz), new Vector3(ux, uy, uz), new Vector3(sx, sy, sz), Train.Cars[c].FrontBogie.CurrentlyVisible, TimeElapsed, ForceUpdate);

					// brightness change
					int o = Train.Cars[c].FrontBogie.CarSections[s].Elements[i].ObjectIndex;
					if (ObjectManager.Objects[o] != null)
					{
						for (int j = 0; j < ObjectManager.Objects[o].Mesh.Materials.Length; j++)
						{
							ObjectManager.Objects[o].Mesh.Materials[j].DaytimeNighttimeBlend = dnb;
						}
					}
				}
			}
		}