Trying to get a triangle to point to the mouse

user3667111

I hate to admit it, but I am awfully stuck on a game I have only just bleeding started.

The concept is quite simple really, I have a triangle that I want to point to the mouses position. Naturally I use the atan2 function in p5.js as this is recommended for such tasks. I know I am calculating the amount to rotate wrong, I want it to rotate via the midpoint of the triangle but point the tip towards the mouse. If somebody could walk me through the maths behind this I would greatly appreciate it.

var player;
function setup() {
  createCanvas(400, 400);
  player = new Player();
}

function draw() {
  background(0)
  fill(255);
  player.update();
}


function Player() {
   this.pos = createVector(width/2, height/2);
   this.r = 20;
   this.direction = 0;

   this.radians = 0;
   this.update = function() {
     push();
     stroke(255);
     noFill();
     translate(this.pos.x, this.pos.y);
     this.radians = atan2(mouseY-this.pos.y, mouseX-this.pos.x);
     rotate(this.radians);
     triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
     ellipse(0, 0, 20, 20);
     pop();
   }
}
George Profenza

Your calculation is correct!

You might be missing one minor detail related to rendering: 0 radians/degrees points to the right and your assumption might be that 0 degrees points up as you draw your triangle.

I'd keep the calculation as is just in case you need to use this angle for other game objects in later development. Instead, I'd simply offset the rotation by 90 degrees (HALF_PI) for rendering to account for the fact that triangle is drawn tip up and not tip right for 0 degrees:

rotate(this.radians + HALF_PI);

Alternatively off course, you can update the triangle call to draw the triangle pointing to the right.

Have fun!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get 2 bars from mouse click and project 3rd point in time

分類Dev

Move user mouse to a certain point

分類Dev

Move user mouse to a certain point

分類Dev

C++: Trying to understand constexpr through combinations and Pascal's Triangle

分類Dev

Get the duration of continuous mouse movement

分類Dev

Can one get a mouse for the console?

分類Dev

Moving a square from a starting point to the position of a mouse click at a fixed speed

分類Dev

D3 linechart mouse move point position is wrong

分類Dev

Rotating a Box2D entity to mouse input point?

分類Dev

Mouse undetected, Graphics crazy - trying to save my dinosaur desktop

分類Dev

When trying to print pascals triangle 13th iteration prints wrong answer

分類Dev

jQuery、Move Point、Get Coordination

分類Dev

Mouse Events get Ignored on the Underlying Layer

分類Dev

How to get an "mouse over" event in kivy

分類Dev

Change mouse pointer on click and get back on release

分類Dev

get text of element id using mouse click

分類Dev

trying to get the average of input numbers

分類Dev

Get ImportError when trying to runserver

分類Dev

Trying to get LetsEncrypt to run on GlassFish

分類Dev

Null point exception when trying to add a List<String> to HashMap

分類Dev

Checking if a triangle is a right triangle

分類Dev

Highcharts, How to prevent point's marker changes its color on mouse hover?

分類Dev

How to store and mark all mouse-clicked-point on an Image loaded in JPanel?

分類Dev

Get floating point numbers from a list

分類Dev

Get latitude/longitude from BigQuery Geography Point

分類Dev

Android get position of view for animation start point

分類Dev

Highcharts Boxplots How to get five point summary?

分類Dev

Elm How to make custom event decoder to get mouse x/y position at mouse-wheel-move

分類Dev

Trying to get total amount based on user input

Related 関連記事

  1. 1

    Get 2 bars from mouse click and project 3rd point in time

  2. 2

    Move user mouse to a certain point

  3. 3

    Move user mouse to a certain point

  4. 4

    C++: Trying to understand constexpr through combinations and Pascal's Triangle

  5. 5

    Get the duration of continuous mouse movement

  6. 6

    Can one get a mouse for the console?

  7. 7

    Moving a square from a starting point to the position of a mouse click at a fixed speed

  8. 8

    D3 linechart mouse move point position is wrong

  9. 9

    Rotating a Box2D entity to mouse input point?

  10. 10

    Mouse undetected, Graphics crazy - trying to save my dinosaur desktop

  11. 11

    When trying to print pascals triangle 13th iteration prints wrong answer

  12. 12

    jQuery、Move Point、Get Coordination

  13. 13

    Mouse Events get Ignored on the Underlying Layer

  14. 14

    How to get an "mouse over" event in kivy

  15. 15

    Change mouse pointer on click and get back on release

  16. 16

    get text of element id using mouse click

  17. 17

    trying to get the average of input numbers

  18. 18

    Get ImportError when trying to runserver

  19. 19

    Trying to get LetsEncrypt to run on GlassFish

  20. 20

    Null point exception when trying to add a List<String> to HashMap

  21. 21

    Checking if a triangle is a right triangle

  22. 22

    Highcharts, How to prevent point's marker changes its color on mouse hover?

  23. 23

    How to store and mark all mouse-clicked-point on an Image loaded in JPanel?

  24. 24

    Get floating point numbers from a list

  25. 25

    Get latitude/longitude from BigQuery Geography Point

  26. 26

    Android get position of view for animation start point

  27. 27

    Highcharts Boxplots How to get five point summary?

  28. 28

    Elm How to make custom event decoder to get mouse x/y position at mouse-wheel-move

  29. 29

    Trying to get total amount based on user input

ホットタグ

アーカイブ