Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. //returns the angle between two vectors
  2. function angleBetween(%v1, %v2)
  3. {
  4.     //cos(theta) = (v1*v2)/(|v1|*|v2|)
  5.     //theta = arccos(cos(thetha))
  6.     return mAcos(VectorDot(%v1, %v2)/(VectorLen(%v1)*VectorLen(%v2)));
  7. }
  8.  
  9. //only use x, y components since we're working on a 2D plane
  10. %v1 = getWords(%player.getPosition(), 0, 1);
  11. %v2 = getWords(%puck.getPosition(), 0, 1);
  12.  
  13. %lookVector = vectorSub(%v2, %v1); //vector that points from %v1 to %v2 with length |%v2-%v1|
  14. %angle = angleBetween(%player.getForwardVector(), %lookVector);
  15.  
  16. //%theta being some acceptable view-bounds angle
  17. //0 would mean looking right at the puck's position
  18. //pi would mean looking anywhere
  19. if(%angle <= %theta)
  20. {
  21.     //...
  22. }
  23.  
  24. //takes 2D vector %v and returns the closest global axis-locked vector
  25. function alignVector(%v)
  26. {
  27.     %x = getWord(%v, 0);
  28.     %y = getWord(%v, 1);
  29.     %sx = %x < 0 ? -1 : 1;
  30.     %sy = %y < 0 ? -1 : 1;
  31.  
  32.     if(mAbs(%x) >= mAbs(%y))
  33.         %vec = %sx SPC "0";
  34.     else
  35.         %vec = "0" SPC %sy;
  36.    
  37.     return %vec;
  38. }
  39.  
  40. //say a player is looking mainly eastward
  41. alignVector(%player.getForwardVector()) -> "1 0"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement