Example_-_Turret_TurnToTarget.c 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "raylib.h"
  2. #include <math.h> // for cos and sin
  3. // Return on which side of the line the point is. l-1 0= r=1
  4. // lineback x,y linefront x,y point x,y
  5. int orientation(int ax,int ay,int bx, int by, int cx, int cy);
  6. typedef struct turret{
  7. float isactive;
  8. float angle;
  9. Vector2 position;
  10. Vector2 barreltip; // tip of the cannon
  11. Vector2 target;
  12. }turret;
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib example.");
  20. turret myturret={0};
  21. myturret.isactive = true;
  22. myturret.angle = 0.0f;
  23. myturret.position = (Vector2){400,200};
  24. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  25. //--------------------------------------------------------------------------------------
  26. // Main game loop
  27. while (!WindowShouldClose()) // Detect window close button or ESC key
  28. {
  29. // Update
  30. //----------------------------------------------------------------------------------
  31. // The turret section.
  32. if(myturret.isactive){
  33. // Get the target coordinates.
  34. myturret.target = GetMousePosition();
  35. // Figure out if the target is left or right or center of the barrel.
  36. int o = orientation(myturret.position.x,myturret.position.y,myturret.barreltip.x,myturret.barreltip.y,myturret.target.x,myturret.target.y);
  37. if(o==-1){ // target is left of the barrel so turn left.
  38. myturret.angle-=0.01f;
  39. }else if(o==1){//target is right of the barrel so turn right.
  40. myturret.angle+=0.01f;
  41. }else{
  42. }
  43. // Keep the angle value between 0..PI*2.0f
  44. if(myturret.angle>PI*2.0f)myturret.angle=0; //needs to be above the line here below..
  45. if(myturret.angle<0)myturret.angle=PI*2.0f;
  46. // Get the new barreltip vector.
  47. myturret.barreltip = (Vector2){myturret.position.x+(cos(myturret.angle)*16),myturret.position.y+(sin(myturret.angle)*16)};
  48. }
  49. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. //draw the turret.
  55. DrawCircle(myturret.position.x,myturret.position.y,10,DARKGRAY);
  56. DrawLineEx(myturret.position,myturret.barreltip,5,RED);
  57. DrawText("Move the mouse around the turret to see it turn.",0,0,30,DARKGRAY);
  58. DrawText(FormatText("Turret Angle : %f",myturret.angle),0,50,30,DARKGRAY);
  59. EndDrawing();
  60. //----------------------------------------------------------------------------------
  61. }
  62. // De-Initialization
  63. //--------------------------------------------------------------------------------------
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }
  68. //
  69. // This is the orientation function. It returns -1 if the point is left of the inputted line.
  70. // 0 if on the same and 1 if on the right of the line.
  71. // aa,bb,point
  72. int orientation(int ax,int ay,int bx, int by, int cx, int cy){
  73. if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))<0)return -1;
  74. if(((bx-ax)*(cy-ay)-(by-ay)*(cx-ax))>0)return 1;
  75. return 0;
  76. }