public static Facing ToFacing(this Angle heading, Vector relativePosition) { var azimuth = Math.Atan2(relativePosition.Y, relativePosition.X); var relativeAzimuth = azimuth - heading.Value; relativeAzimuth = Angle.ReduceAngle(relativeAzimuth); Facing facing = Facing.None; // err on the side of gnerosity and consider all edge cases in both facings if (relativeAzimuth >= -PiOnFour && relativeAzimuth <= PiOnFour) { facing = facing | Facing.Bow; } if (relativeAzimuth <= -PiOnFour && relativeAzimuth >= -3 * PiOnFour) { facing = facing | Facing.Starboard; } if (relativeAzimuth >= PiOnFour && relativeAzimuth <= 3 * PiOnFour) { facing = facing | Facing.Port; } if (relativeAzimuth >= 3* PiOnFour || relativeAzimuth <= -3 * PiOnFour) { facing = facing | Facing.Stern; } return facing; }
/// <summary> /// Time based constant velocity linear motion /// </summary> /// <param name="startTime">The begining time of this motion</param> /// <param name="velocity">The velocity per second or 1000 time units</param> /// <param name="initialPosition">The starting point of the line</param> public LinearMotion(ulong startTime, Vector velocity, Vector initialPosition) { // the starting time for this set of motion m_StartTime = startTime; // the velocity per second or 1000 time units m_Velocity = velocity; m_InitialPosition = initialPosition; }
public bool IntersectsPoint(Vector worldPosition, ulong time) { IMotion motion = m_Path.GetCurrentMotion(time); var objectPosition = motion.GetCurrentPosition(time); return m_Bounds.Contains(worldPosition, objectPosition); }
public bool Contains(Vector positionOne, Vector positionTwo) { var relativePosition = positionOne - positionTwo; return relativePosition.X >= TopLeft.X && relativePosition.X <= BottomRight.X && relativePosition.Y >= BottomRight.Y && relativePosition.Y <= TopLeft.Y; }
public void Test_Get_Motion_Initial_North() { var motion = new CircularMotion(0, 1, new Angle(0), new Angle(0), 1, Vector.Zero); Vector velocity = new Vector(0, 0); Vector motionVelocity = motion.GetMotion(0); Assert.AreEqual(velocity.X, motionVelocity.X, 0.000000000001); Assert.AreEqual(velocity.Y, motionVelocity.Y, 0.000000000001); }
public void Test_Get_Motion_Initial_South_Clockwise() { var motion = new CircularMotion(0, 0, new Angle(0), new Angle(-Math.PI), 1, Vector.Zero); Vector velocity = new Vector(0, 0); Vector motionVelocity = motion.GetMotion(2000); Assert.AreEqual(velocity.X, motionVelocity.X, 0.000000000001); Assert.AreEqual(velocity.Y, motionVelocity.Y, 0.000000000001); }
public void ConvertToAndFromVector3() { var vector = new Vector(5.7, 6.6); var vector3 = VectorConversions.ToOgreVector(vector); var returnedVector = VectorConversions.FromOgreVector(vector3); Assert.AreEqual(vector, returnedVector); }
public List<IMotion> CreatePathTo(Vector destination, Vector initialVelocity, Vector initialPosition, ulong startTime) { // get initial velocity //determine turning circles Vector circleOnePosition, circleTwoPosition; double circleRadius = CalcRadius(initialVelocity.Length, m_Acceleration); DetermineTurningCircles(initialVelocity, circleRadius, out circleOnePosition, out circleTwoPosition); //select a turning circle Vector destinationRelativeToInitialPosition = destination - initialPosition; Vector selectedTuringCicle = SelectTuriningCircle(circleOnePosition, circleTwoPosition, destinationRelativeToInitialPosition, circleRadius); //determine turn direction TurnDirection turnDirection = DetermineTurnDirection(initialVelocity, selectedTuringCicle); //determine turn end Angle turnStart = new Angle(-selectedTuringCicle); // zero the destination around the turning circle var destinationRelativeToTurningCircle = destination - (initialPosition + selectedTuringCicle); //use the relative destination to pick an end point for the turn Angle turnEnd = DetermineTurnEnd(destinationRelativeToTurningCircle, circleRadius, turnDirection); Angle turnRate = DetermineTurnRate(initialVelocity.Length, circleRadius, turnDirection); var turnDuration = DetermineDurationOfTurn(turnStart, turnEnd, turnRate, turnDirection); turnDuration += startTime; // create circular motion var circle = new CircularMotion(startTime, circleRadius, turnStart, turnRate, initialVelocity.Length, initialPosition); Vector startOfLine = initialPosition + selectedTuringCicle + CoordinateConversions.RadialToVector(turnEnd, circleRadius); //Vector startOfLine = circle.GetCurrentPosition(turnDuration); // create linear motion var velocity = (destination - startOfLine); ulong destinationTime = (ulong)((velocity.Length / initialVelocity.Length) * 1000.0); velocity = velocity.Normalise(); velocity = velocity * initialVelocity.Length; var linear = new LinearMotion(turnDuration, velocity, startOfLine); var linear2 = new LinearMotion(turnDuration + destinationTime, velocity, destination); //Assert.AreEqual(circle.GetVelocity(turnDuration), linear.GetVelocity(turnDuration)); List<IMotion> path = new List<IMotion> { circle, linear, linear2, }; return path; }
/// <summary> /// primary constructor for circular motion objects /// </summary> /// <param name="startTime">The begining time for the motion</param> /// <param name="radius">The radius of the turning circle</param> /// <param name="startAngle">The angle to the start point on the circle</param> /// <param name="turnRate">The turn rate in radians</param> /// <param name="initialSpeed">Speed used to determine the current velocity</param> /// <param name="initialPosition">The position when this motion started</param> public CircularMotion(ulong startTime, double radius, Angle startAngle, Angle turnRate, double initialSpeed, Vector initialPosition) { m_StartTime = startTime; m_Radius = radius; m_StartAngle = startAngle; m_TurnRate = turnRate; m_InitialSpeed = initialSpeed; m_CircleOffset = CoordinateConversions.RadialToVector(startAngle, m_Radius); m_InitialPosition = initialPosition; }
public IList<SelectableObject> GetObjectAt(Vector worldPosition, ulong time) { var selected = new List<SelectableObject>(); foreach (var selectableObject in m_Objects) { if (selectableObject.IntersectsPoint(worldPosition, time)) { selected.Add(selectableObject); } } return selected; }
public void ClearsOldSelectedObject() { var message = new SelectObjectAtMessage(m_Position, m_TestTime); var newPosition = new Vector(50, 5); var secondObject = CreateSelectableObjectAt(newPosition); m_Repository.AddObject(secondObject); m_Repository.OnSelectObject(message); m_Bus.DidNotReceive().Send(Arg.Any<DeselectedObjectMessage>()); m_Repository.OnSelectObject(new SelectObjectAtMessage(newPosition, m_TestTime)); m_Bus.Received().Send(Arg.Any<DeselectedObjectMessage>()); }
public void CreateTurningCirclesBasicPositiveY() { var path = new Path(); Vector initialVelocity = new Vector(0, 1); double acceleration = 1; Vector circleOne; Vector circleTwo; path.DetermineTurningCircles(initialVelocity, acceleration, out circleOne, out circleTwo); var circleOneResultShouldBe = new Vector(-1, 0); var circleTwoResultShouldBe = new Vector(1, 0); Assert.AreEqual(circleOneResultShouldBe.X, circleOne.X, 0.000000001); Assert.AreEqual(circleOneResultShouldBe.Y, circleOne.Y, 0.000000001); Assert.AreEqual(circleTwoResultShouldBe.X, circleTwo.X, 0.000000001); Assert.AreEqual(circleTwoResultShouldBe.Y, circleTwo.Y, 0.000000001); }
public OverlayScene(Vector screenSize) { m_ScreenSize = screenSize; //note we need to load a font if we want to use a font var font = FontManager.Singleton.Create("Arial", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME); font.SetParameter("type", "truetype"); font.SetParameter("source", "Arial.ttf"); font.SetParameter("size", "16"); font.SetParameter("resolution", "96"); font.Load(); m_Container = (OverlayContainer)OverlayManager.Singleton.CreateOverlayElement("Panel", "PanelName"); //note positions and sizes are in relative screen space m_Container.SetPosition(0.35, 0.3); m_Container.SetDimensions(0.3, 0.5); m_Container.MaterialName = "triangle/red"; m_Overlay = OverlayManager.Singleton.Create("bob"); m_Overlay.Add2D(m_Container); m_Overlay.Show(); }
public HighlightTargetMessage(Vector worldPosition, ulong time) { WorldPosition = worldPosition; Time = time; }
public void SetUp() { m_Bus = Substitute.For<IBus>(); m_Id = new PlayerId(); m_VesselRepo = Substitute.For<IVesselRepository>(); m_Repository = new SelectableObjectRepository(m_Bus, m_Id, m_VesselRepo); m_Position = new Vector(5, 7); m_TestTime = 500; m_SelectableObject = CreateSelectableObjectAt(m_Position); m_Repository.AddObject(m_SelectableObject); }
private SelectableObject CreateSelectableObjectAt(Vector position) { var path = Get<IPath>(); var motion = Get<IMotion>(); motion.GetCurrentPosition(m_TestTime).Returns(position); path.GetCurrentMotion(m_TestTime).Returns(motion); return new SelectableObject("fred", path); }
/// <summary> /// Select the closest turning circle to the desitnation unless the closest circle is within the /// radius of the turning circle. /// All positions need to be in the same reference frame /// </summary> /// <param name="circleCentreOne"></param> /// <param name="circleCentreTwo"></param> /// <param name="targetPosition">The destination</param> /// <param name="turningRadius">The radius of the circles</param> /// <returns></returns> public Vector SelectTuriningCircle(Vector circleCentreOne, Vector circleCentreTwo, Vector targetPosition, double turningRadius) { double displacementOne = (circleCentreOne - targetPosition).Length; double displacementTwo = (circleCentreTwo - targetPosition).Length; Vector selectedCircle; if (displacementOne < displacementTwo && displacementOne >= turningRadius) { selectedCircle = circleCentreOne; } else { selectedCircle = displacementTwo < turningRadius ? circleCentreOne : circleCentreTwo; } return selectedCircle; }
/// <summary> /// Determines the positions of the two possible turning circles /// Relative to the initial position which is not passed into the function /// </summary> /// <param name="initialVelocity">The current velocity of the object</param> /// <param name="radius">The desired radius of the object</param> /// <param name="circleOne">The first of the two possible turning circles</param> /// <param name="circleTwo">The second of the two possible turning circles</param> public void DetermineTurningCircles(Vector initialVelocity, double radius, out Vector circleOne, out Vector circleTwo) { Angle velocityAngle; // calculate the angle of the current velocity velocityAngle = new Angle(initialVelocity); velocityAngle.ReduceAngle(); //double vesselSpeed = initialVelocity.Length; // calculate the radius if the turning circle based on the acceleration and current speed //double radius = CalcRadius(vesselSpeed, acceleration); // calcualte both of the turning circles Angle angleOne = new Angle(velocityAngle.Value + (Math.PI / 2)); circleOne = CoordinateConversions.RadialToVector(angleOne, radius); Angle angleTwo = new Angle(velocityAngle.Value - (Math.PI / 2)); circleTwo = CoordinateConversions.RadialToVector(angleTwo, radius); }
public static Vector3 ToOgreVector(Vector vector) { return new Vector3(vector.X, 0.0, vector.Y); }
public void CreateTurningCirclesOnAngle() { var path = new Path(); Vector initialVelocity = new Vector(3, 4); double acceleration = 5; Vector circleOne; Vector circleTwo; path.DetermineTurningCircles(initialVelocity, acceleration, out circleOne, out circleTwo); var circleOneResultShouldBe = new Vector(-4, 3); var circleTwoResultShouldBe = new Vector(4, -3); Assert.AreEqual(circleOneResultShouldBe.X, circleOne.X, 0.000000001); Assert.AreEqual(circleOneResultShouldBe.Y, circleOne.Y, 0.000000001); Assert.AreEqual(circleTwoResultShouldBe.X, circleTwo.X, 0.000000001); Assert.AreEqual(circleTwoResultShouldBe.Y, circleTwo.Y, 0.000000001); }
/// <summary> /// A box around 0,0 with X being the horizontal /// And Y being the vertiacal /// </summary> /// <param name="x">the width of the box</param> /// <param name="y">the height of the box</param> public Box(double x, double y) { const double two = 2.0; m_TopLeftCorner = new Vector(-x / two, y / two); m_BottomRightCorner = new Vector(x / two, -y / two); }
public SetPathToTargetMessage(Vector worldPosition, ulong time) { WorldPosition = worldPosition; Time = time; }
private Vector FromWinScreenToWorldPosition(IntPtr hwnd, IntPtr param) { RECT rect; // unpack x and y from the param double x = unchecked((short)(long)param); double y = unchecked((short)((long)param >> 16)); Vector worldPos = Vector.Zero; // get the size of the screen so we can tell how big we are. // this could be cahced in the future if we wanted to // should be done if we need the performance if(GetWindowRect( hwnd, out rect)) { double width = rect.Right - rect.Left; double height = rect.Bottom - rect.Top; //convert the positions to relative to top left corner. top left 0,0 bottom right 1,1 double relx = x/ width; double rely = y / height; double cameraWidth = m_Camera.OrthoWindowWidth; double cameraHeight = m_Camera.OrthoWindowHeight; //calc a corner for the camera. //should be top left corner Vector3 cornerPosition = m_Camera.Position - new Vector3(cameraWidth / 2.0, 0, cameraHeight / 2.0); //camera coordinates decrease as the go down so subtract the z value Vector3 inputPosition = new Vector3( relx * cameraWidth, 0 ,(1 - rely) * cameraHeight); Vector3 worldVector = cornerPosition + inputPosition; worldPos = new Vector(worldVector.z, worldVector.x); } return worldPos; }
/// <summary> /// Determines if a turn is clockwise or anti clockwise /// </summary> /// <param name="velocity">The current velocity of the object</param> /// <param name="turiningCircleOffset">The position of the turning circle relative to the initial position</param> /// <returns>Clockwise or anti clockwise</returns> public TurnDirection DetermineTurnDirection(Vector velocity, Vector turiningCircleOffset) { int velocityFacing = Angle.FacingNumber(velocity); int offsetFacing = Angle.FacingNumber(turiningCircleOffset); TurnDirection turnDirection = TurnDirection.Unknown; if ((velocityFacing == (offsetFacing + 3)) || (velocityFacing == (offsetFacing - 1))) { turnDirection = TurnDirection.AntiClockwise; } else if ((velocityFacing == (offsetFacing - 3)) || (velocityFacing == (offsetFacing + 1))) { turnDirection = TurnDirection.Clockwise; } return turnDirection; }
/// <summary> /// /// </summary> /// <param name="destination">The position of the destination relative to the turning point</param> /// <param name="radius">The radius of the turning circle</param> /// <param name="turnDirection">If the turn is clockwise or anti clockwise</param> /// <returns></returns> public Angle DetermineTurnEnd(Vector destination, double radius, TurnDirection turnDirection) { Angle angleTowardsDestination = new Angle(destination); Angle tangentAngles = new Angle(Math.Acos(radius / destination.Length)); Angle desiredEndPoint; if (turnDirection == TurnDirection.Clockwise) { desiredEndPoint = angleTowardsDestination + tangentAngles; } else { desiredEndPoint = angleTowardsDestination - tangentAngles; } desiredEndPoint.ReduceAngle(); return desiredEndPoint; }
public bool Equals(Vector other) { return other.X.Equals(X) && other.Y.Equals(Y); }
public void MoveToDestination(Vector destination, ulong currentTime) { var currentMotion = m_Motion.GetCurrentMotion(currentTime); var initialPosition = currentMotion.GetCurrentPosition(currentTime); var initialVelocity = currentMotion.GetVelocity(currentTime); var newPath = CreatePathTo(destination, initialVelocity, initialPosition, currentTime); if (newPath[0].GetCurrentPosition(currentTime) != initialPosition) { throw new InvalidOperationException("The positions do not match up"); } m_Motion = new CombinedMotion( new List<IMotion>(newPath)); }
public void SelectTurningCircleCloserToSecondPointWithinRadius() { var path = new Path(); Vector circleOne = new Vector(1, 0); Vector circleTwo = new Vector(-1, 0); Vector targetPosition = new Vector(-1, 1); double turningRadius = 1.5; Vector selectedCircle = path.SelectTuriningCircle(circleOne, circleTwo, targetPosition, turningRadius); Assert.AreEqual(selectedCircle.X, circleOne.X, 0.000000001); Assert.AreEqual(selectedCircle.Y, circleOne.Y, 0.000000001); }
public Attack(ulong firingTime, string target, Vector firingPosition) { m_FiringTime = firingTime; m_Target = target; m_FiringPosition = firingPosition; }
public void AddButton( string text, Vector positon, Vector dimensions ) { var button = new Button(); button.Size = new Size(); }