private static void UpdateRearBogieObjects(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].RearBogie.CarSections.Length == 0)
{
return;
}
// calculate positions and directions for section element update
int c = CarIndex;
double dx = Train.Cars[c].RearBogie.FrontAxle.Follower.WorldPosition.X - Train.Cars[c].RearBogie.RearAxle.Follower.WorldPosition.X;
double dy = Train.Cars[c].RearBogie.FrontAxle.Follower.WorldPosition.Y - Train.Cars[c].RearBogie.RearAxle.Follower.WorldPosition.Y;
double dz = Train.Cars[c].RearBogie.FrontAxle.Follower.WorldPosition.Z - Train.Cars[c].RearBogie.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].RearBogie.Up.X;
uy = Train.Cars[c].RearBogie.Up.Y;
uz = Train.Cars[c].RearBogie.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].RearBogie.FrontAxle.Follower.WorldPosition.X + Train.Cars[c].RearBogie.RearAxle.Follower.WorldPosition.X);
double py = 0.5 * (Train.Cars[c].RearBogie.FrontAxle.Follower.WorldPosition.Y + Train.Cars[c].RearBogie.RearAxle.Follower.WorldPosition.Y);
double pz = 0.5 * (Train.Cars[c].RearBogie.FrontAxle.Follower.WorldPosition.Z + Train.Cars[c].RearBogie.RearAxle.Follower.WorldPosition.Z);
double d = 0.5 * (Train.Cars[c].RearBogie.FrontAxlePosition + Train.Cars[c].RearBogie.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].RearBogie.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].RearBogie.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].RearBogie.CurrentCarSection;
if (s >= 0)
{
for (int i = 0; i < Train.Cars[c].RearBogie.CarSections[s].Elements.Length; i++)
{
UpdateRearBogieSectionElement(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].RearBogie.CurrentlyVisible, TimeElapsed, ForceUpdate);
// brightness change
int o = Train.Cars[c].RearBogie.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;
}
}
}
}
}