//-----------------------------------------------------------------------------
// Vector3Helper.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace RacingGame.Helpers
{
///
/// Vector 3 helper
///
class Vector3Helper
{
///
/// Private constructor to prevent instantiation.
///
private Vector3Helper()
{
}
///
/// Return angle between two vectors. Used for visbility testing and
/// for checking angles between vectors for the road sign generation.
///
/// Vector 1
/// Vector 2
/// Float
public static float GetAngleBetweenVectors(Vector3 vec1, Vector3 vec2)
{
// See http://en.wikipedia.org/wiki/Vector_(spatial)
// for help and check out the Dot Product section ^^
// Both vectors are normalized so we can save deviding through the
// lengths.
return (float)Math.Acos(Vector3.Dot(vec1, vec2));
}
///
/// Distance from our point to the line described by linePos1 and linePos2.
///
/// Point
/// Line position 1
/// Line position 2
/// Float
public static float DistanceToLine(Vector3 point,
Vector3 linePos1, Vector3 linePos2)
{
// For help check out this article:
// http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
Vector3 lineVec = linePos2 - linePos1;
Vector3 pointVec = linePos1 - point;
return Vector3.Cross(lineVec, pointVec).Length() / lineVec.Length();
}
///
/// Signed distance to plane
///
/// Point
/// Plane position
/// Plane normal
/// Float
public static float SignedDistanceToPlane(Vector3 point,
Vector3 planePosition, Vector3 planeNormal)
{
Vector3 pointVec = planePosition - point;
return Vector3.Dot(planeNormal, pointVec);
}
}
}