/// <summary> /// Find the index of the given generator. /// </summary> /// <returns></returns> private static int FindGenIndex(RoadGenerator gen) { for (int i = 0; i < generators.Length; ++i) { if (gen == generators[i]) { return(i); } } /// If we didn't find it, fall back on the invisible plain path. return(0); }
/// <summary> /// Build transforms from world space into section space. In section /// space, the section rect is an axis aligned box dimensioned /// edgeLength X CollWidth centered at (0,0). /// </summary> /// <param name="gen"></param> protected void CacheCollisionXfms(RoadGenerator gen) { worldToSection = Matrix.Identity; sectionToWorld = Matrix.Identity; // Center at the point between the nodes Vector2 nodeCenter = (Edge.Node1.Position2d + Edge.Node0.Position2d) * 0.5f; sectionToWorld.M41 = nodeCenter.X; sectionToWorld.M42 = nodeCenter.Y; // Align the section's X axis with the axis between the nodes. // Apply no scaling, so computations based on length can still // be done using world space units. Vector2 nodeAxis = Edge.Node1.Position2d - Edge.Node0.Position2d; edgeLength = nodeAxis.Length(); if (edgeLength < 0.01f) { Vector3 position = edge.Node1.Position; position.Y = edge.Node0.Position2d.Y + 0.01f; edge.Node1.Position = position; nodeAxis = edge.Node1.Position2d - edge.Node0.Position2d; edgeLength = nodeAxis.Length(); } nodeAxis /= edgeLength; sectionToWorld.M11 = nodeAxis.X; sectionToWorld.M12 = nodeAxis.Y; sectionToWorld.M21 = -nodeAxis.Y; sectionToWorld.M22 = nodeAxis.X; // Rotation is just the transpose worldToSection.M11 = sectionToWorld.M11; worldToSection.M12 = sectionToWorld.M21; worldToSection.M21 = sectionToWorld.M12; worldToSection.M22 = sectionToWorld.M22; // Translation is negated and rotated worldToSection.M41 = -sectionToWorld.M41 * sectionToWorld.M11 - sectionToWorld.M42 * sectionToWorld.M12; worldToSection.M42 = -sectionToWorld.M41 * sectionToWorld.M21 - sectionToWorld.M42 * sectionToWorld.M22; }
/// <summary> /// Constructor, takes in owning path, which this road represents. /// </summary> /// <param name="inPath"></param> public Road(WayPoint.Path inPath) { generator = generators[genIndex]; path = inPath; }