Implemented road & road mesh generator.

This commit is contained in:
2015-06-02 20:50:59 +03:00
parent 61a05289d3
commit f9b20b0226
27 changed files with 730 additions and 53 deletions

View File

@ -43,6 +43,14 @@ namespace TransportGame.Model
}
}
public Vector2 Direction
{
get
{
return (P1 - P0).Normalized;
}
}
public LineSegment(Vector2 p0, Vector2 p1)
: this()
{

View File

@ -80,6 +80,33 @@ namespace TransportGame.Model.Road
[XmlAttribute("lanesTo1")]
public int LanesTo1 { get; set; }
/// <summary>
/// Gets the direction vector of this road segment
/// </summary>
[XmlIgnore]
public Vector2 Direction
{
get
{
if (Terminal1Id == -1 || Terminal2Id == -1 || ParentNetwork == null)
return Vector2.Zero;
return (Terminal2.Position - Terminal1.Position).Normalized;
}
}
/// <summary>
/// Gets the road segment as a line segment
/// </summary>
/// <returns></returns>
public LineSegment AsLineSegment()
{
if (Terminal1Id == -1 || Terminal2Id == -1 || ParentNetwork == null)
throw new InvalidOperationException("Terminals are not initialized.");
return new LineSegment(Terminal1.Position, Terminal2.Position);
}
/// <summary>
/// Initializes road segment
/// </summary>

View File

@ -78,6 +78,22 @@ namespace TransportGame.Model
}
}
/// <summary>
/// Gets the normalized vector raised to second power
/// </summary>
/// <remarks>
/// This is less computationally expensive (no need to calculate square root).
/// </remarks>
/// <returns>Normalized vector</returns>
public Vector2 NormalizedSq
{
get
{
float len2 = LengthSq;
return new Vector2(X * X / len2, Y * Y / len2);
}
}
/// <summary>
/// Rotates vector by given number of radians
/// </summary>
@ -123,6 +139,16 @@ namespace TransportGame.Model
return new Vector2(a.X - b.X, a.Y - b.Y);
}
/// <summary>
/// Negation operator
/// </summary>
/// <param name="a">Vector</param>
/// <returns>Negated vector</returns>
public static Vector2 operator -(Vector2 a)
{
return new Vector2(-a.X, -a.Y);
}
/// <summary>
/// Multiply by constant
/// </summary>
@ -188,6 +214,28 @@ namespace TransportGame.Model
return a.X * b.X + a.Y * b.Y;
}
/// <summary>
/// Returns the magnitude of the cross product between the two vectors (z considered 0)
/// </summary>
/// <param name="a">First vector</param>
/// <param name="b">Second vector</param>
/// <returns>Magnitude of cross product</returns>
public static float Cross(Vector2 a, Vector2 b)
{
return (a.X * b.Y) - (a.Y * b.X);
}
/// <summary>
/// Tests if two vectors are colliniar
/// </summary>
/// <param name="a">a</param>
/// <param name="b">b</param>
/// <returns>True if vectors are colliniar</returns>
public static bool AreColliniar(Vector2 a, Vector2 b)
{
return Math.Abs(Cross(a, b)) < 1e-12;
}
/// <summary>
/// Gets the vector corresponding with specified angle (in radians)
/// </summary>