Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. public bool Visable(Vector2 Position, Vector2 ObjectPosition, float LineOfSight, float Rotation, float Angle)
  2. {
  3. if (Utilities.Distance(Position, ObjectPosition) <= LineOfSight)
  4. {
  5. float rotation = Utilities.UnitAngle(Rotation);
  6. Vector2 Direction = Utilities.VectorDirection(Position, ObjectPosition);
  7. float Theta = Utilities.VectorDirection(Direction);
  8.  
  9.  
  10. if (rotation - Angle < 0)
  11. {
  12. float lol = Utilities.UnitAngle(rotation - Angle);
  13. if (Theta > lol)
  14. return true;
  15. }
  16.  
  17.  
  18. if (Direction.X > 0)
  19. Theta = (float)(2 * Math.PI - Theta);
  20.  
  21. if (Theta > rotation - Angle & Theta < rotation + Angle)
  22. return true;
  23. else return false;
  24. }
  25. else return false;
  26. }
  27.  
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using Microsoft.Xna.Framework;
  32. using Microsoft.Xna.Framework.Audio;
  33. using Microsoft.Xna.Framework.Content;
  34. using Microsoft.Xna.Framework.GamerServices;
  35. using Microsoft.Xna.Framework.Graphics;
  36. using Microsoft.Xna.Framework.Input;
  37. using Microsoft.Xna.Framework.Media;
  38. using Microsoft.Xna.Framework.Net;
  39. using Microsoft.Xna.Framework.Storage;
  40.  
  41. namespace Testing_Project
  42. {
  43. public static class Utilities
  44. {
  45. private static Random random = new Random((int)DateTime.Now.Ticks);
  46.  
  47. static Utilities() { }
  48.  
  49. //Random
  50.  
  51. /// <summary> A random value between min and max </summary>
  52. public static int Random(int min, int max)
  53. {
  54. return random.Next(min, max);
  55. }
  56. //// <summary> A random double; If negitive is true then value is between -max and max. If false then value is between 0 and max. </summary>
  57. /// <summary>
  58. /// A random double.
  59. /// </summary>
  60. /// <param name="max">The maximum value the function can return</param>
  61. /// <param name="negitive">If false, the minimum value will be 0. If true, the minimum value will be -max.</param>
  62. public static double Random(double max, bool negitive)
  63. {
  64. if (negitive) return (random.NextDouble() * max * 2) - max;
  65. else return random.NextDouble() * max;
  66. }
  67.  
  68. //Vector Math
  69. public static float Distance(Vector2 Position1, Vector2 Position2)
  70. {
  71. float x = Position1.X - Position2.X;
  72. float y = Position1.Y - Position2.Y;
  73. return (float)Math.Sqrt(x * x + y * y);
  74. }
  75. public static Vector2 Normalize(Vector2 input)
  76. {
  77. input.Normalize();
  78. if (float.IsNaN(input.X))
  79. input = new Vector2(0);
  80. return input;
  81. }
  82. public static Vector2 VectorDirection(float Rotation)
  83. {
  84. Vector2 oh = new Vector2(-1 * (float)Math.Sin(Rotation), (float)Math.Cos(Rotation));
  85. Vector2 lol = Utilities.Normalize(new Vector2(-1 * (float)Math.Sin(Rotation), (float)Math.Cos(Rotation)));
  86. return Utilities.Normalize(new Vector2(-1 * (float)Math.Sin(Rotation), (float)Math.Cos(Rotation)));
  87. }
  88. public static Vector2 VectorDirection(Vector2 PointOfReferance, Vector2 ObjectPosition)
  89. {
  90. return Utilities.Normalize(ObjectPosition - PointOfReferance);
  91. }
  92. public static float VectorDirection(Vector2 Direction)
  93. {
  94. Direction = Utilities.Normalize(Direction);
  95. return (float)Math.Acos(Direction.Y);
  96. }
  97.  
  98. public static float UnitAngle(float Rotation)
  99. {
  100. while (Rotation > 2 * Math.PI)
  101. Rotation -= (float)(2 * Math.PI);
  102. while (Rotation < 0)
  103. Rotation += (float)(2 * Math.PI);
  104.  
  105. return Rotation;
  106. }
  107.  
  108. public static float VectorDirectionPropper(Vector2 Direction)
  109. {
  110. double pi = Math.PI;
  111. float x = Direction.X;
  112. float y = Direction.Y;
  113. float theta = Utilities.VectorDirection(Direction);
  114.  
  115. /*
  116. if (x > 0 & y > 0) theta = -(float)(Math.PI - theta);
  117. if (x < 0 & y > 0) theta = (float)(Math.PI - theta);
  118. if (x < 0 & y < 0) theta = (float)(Math.PI + theta);
  119. if (x > 0 & y < 0) theta = theta;
  120. */
  121. return theta;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement