Advertisement
Guest User

Lazy -Path- Finding

a guest
Apr 6th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. int travelForFrames = 0;
  2.         int focusForFrames = 0;
  3.         Random ran = GlobalVariables.ran;
  4.         private Quaternion NewDirection()
  5.         {
  6.             // Get the target object based on the name
  7.             SceneInterface.ActiveSceneInterface.ObjectManager.Find("PlayerRepEntity", true, out targetSceneObject);
  8.  
  9.             // Get the details of the child matrix
  10.             sceneObject.World.Decompose(out parentObjectScale, out parentObjectRotationQ, out parentObjectTranslation);
  11.  
  12.             // Get the details of the target
  13.             targetSceneObject.World.Decompose(out targetScale, out targetRotationQ, out targetTranslation);
  14.  
  15.            
  16.             if (travelForFrames <= 0)
  17.             {
  18.                 // Calculate the difference (direction) between the child to the target
  19.                 // Subtract the child from the target (we want the child to face the target)
  20.                 distance = targetTranslation - parentObjectTranslation;
  21.  
  22.                 // If the zombie is further than 10 meters
  23.                 if (distance.Length() > 15 && distance.Length() < 50)
  24.                 {
  25.                     if (focusForFrames <= 0)
  26.                     {
  27.                         // 20% of the time we will
  28.                         if (ran.Next(10) > 8)
  29.                         {
  30.                             // Stuff around with the new direction for the zombie to face
  31.                             targetTranslation = targetTranslation + GetRandomVector3(0, 30, true, true, false, true);
  32.                             distance = targetTranslation - parentObjectTranslation;
  33.  
  34.                             // Set the counter with a some what random amount of time before the new direciton is recalculated
  35.                             travelForFrames = ran.Next(30, 260);
  36.                             focusForFrames = ran.Next(10, 160);
  37.                         }
  38.                     }
  39.                     else
  40.                     {
  41.                         focusForFrames--;
  42.                     }
  43.                 }
  44.  
  45.             }
  46.             else
  47.             {
  48.                 travelForFrames--;
  49.             }
  50.  
  51.  
  52.             //return Math.Atan2(distance.Y, distance.X);
  53.  
  54.             newLook = distance;
  55.             // Normalize the distance to turn it to a direction
  56.             newLook.Normalize();
  57.  
  58.             // Matrix of the direction we want the enemy to face
  59.             rotate = Matrix.CreateWorld(Vector3.Zero, newLook, Vector3.Up);
  60.  
  61.             // Lerp towards the direction we want the enemy to face, from it's current facing direction
  62.             Quaternion newRotation = Quaternion.Lerp(parentObjectRotationQ, Quaternion.CreateFromRotationMatrix(rotate), 0.125f);
  63.  
  64.             // Update the scene objects rotation to face this new direction
  65.             sceneObject.World = Matrix.CreateScale(parentObjectScale) * Matrix.CreateFromQuaternion(newRotation) * Matrix.CreateTranslation(parentObjectTranslation);
  66.  
  67.             return newRotation;
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         private Vector3 GetRandomVector3(float Minimum, float Maximum, bool NegativeRangeRequired, bool x, bool y, bool z)
  74.         {
  75.             Vector3 returnVector = new Vector3();
  76.  
  77.             if (x)
  78.             {
  79.                 returnVector.X = (float)ran.Next((int)Minimum, (int)Maximum);
  80.             }
  81.             if (y)
  82.             {
  83.                 returnVector.Y = (float)ran.Next((int)Minimum, (int)Maximum);
  84.             }
  85.             if (z)
  86.             {
  87.                 returnVector.Z = (float)ran.Next((int)Minimum, (int)Maximum);
  88.             }
  89.  
  90.  
  91.             if (NegativeRangeRequired)
  92.             {
  93.                 if (x)
  94.                 {
  95.                     returnVector.X = returnVector.X * ReturnPositiveOrNegative();
  96.                 }
  97.  
  98.                 if (y)
  99.                 {
  100.                     returnVector.Y = returnVector.Y * ReturnPositiveOrNegative();
  101.                 }
  102.  
  103.                 if (z)
  104.                 {
  105.                     returnVector.Z = returnVector.Z * ReturnPositiveOrNegative();
  106.                 }
  107.             }
  108.  
  109.             return returnVector;
  110.         }
  111.  
  112.         private float ReturnPositiveOrNegative()
  113.         {
  114.             float numberToReturn = 0;
  115.             while (numberToReturn == 0.0f)
  116.             {
  117.                 numberToReturn = (float)ran.Next(-1, 2);
  118.             }
  119.             return numberToReturn;
  120.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement