Guest User

Untitled

a guest
Jul 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. float heroX = 0.0;
  2. float heroY = 0.0;
  3. float heroRot = 0.0;
  4. float heroRotIncr = 0.05;
  5. float heroMoveSpeed = 3.0;
  6. float heroSize = 25.0;
  7.  
  8. // The two back points of the hero's triangle are
  9. // 140 degrees away from the forward facing point. (An
  10. // equilateral triangle's three angles are 360.0 / 3
  11. // = 120.0 degrees apart.)
  12. float heroArrow = radians(140.0);
  13.  
  14. boolean[] pressed = new boolean[256];
  15.  
  16. void setup() {
  17. size(512, 256);
  18.  
  19. // The default 2D camera doesn't look at the
  20. // origin (0, 0) in the center like graphing
  21. // calculators do. Instead (0, 0) is the top-left
  22. // corner. Furthermore, the y-axis points down.
  23. heroX = width * 0.5;
  24. heroY = height * 0.5;
  25. background(255.0);
  26. noStroke();
  27. }
  28.  
  29. void draw() {
  30.  
  31. // A
  32. if (pressed[65]) {
  33. heroRot -= heroRotIncr;
  34. }
  35.  
  36. // D
  37. if (pressed[68]) {
  38. heroRot += heroRotIncr;
  39. }
  40.  
  41. float cosRot = cos(heroRot);
  42. float sinRot = sin(heroRot);
  43.  
  44. // The default coordinate system in Processing
  45. // is Cartesian. The formula to convert from
  46. // polar to Cartesian coordinates is
  47. // ( x = translationx + cos(angle) * radius
  48. // y = translationy + sin(angle) * radius ).
  49.  
  50. // W
  51. if (pressed[87]) {
  52. heroX = heroX + cosRot * heroMoveSpeed;
  53. heroY = heroY + sinRot * heroMoveSpeed;
  54. }
  55.  
  56. // S
  57. if (pressed[83]) {
  58. heroX = heroX - cosRot * heroMoveSpeed;
  59. heroY = heroY - sinRot * heroMoveSpeed;
  60. }
  61.  
  62. // Draw avatar.
  63. fill(0.0, 127.0, 255.0);
  64. triangle(
  65.  
  66. // Front point.
  67. heroX + cosRot * heroSize,
  68. heroY + sinRot * heroSize,
  69.  
  70. // We have to add two rotations:
  71. // the angle to get from the front point
  72. // plus the overall avatar angle.
  73. heroX + cos(heroRot + heroArrow) * heroSize,
  74. heroY + sin(heroRot + heroArrow) * heroSize,
  75.  
  76. heroX + cos(heroRot - heroArrow) * heroSize,
  77. heroY + sin(heroRot - heroArrow) * heroSize);
  78. }
  79.  
  80. void keyPressed() {
  81. pressed[keyCode] = true;
  82. }
  83.  
  84. void keyReleased() {
  85. pressed[keyCode] = false;
  86. }
Add Comment
Please, Sign In to add comment