Is there a way to get a triangle to rotate to face the mouse on a circle?

69 views Asked by At

I'm trying to make it so that a triangle, with its two base points always staying on the outer edge of a circle, will point towards where the mouse is. I'm convinced it's mostly a specific math problem I'm missing, but I haven't been able to figure it out. To reiterate, I'm trying to make something kind of like those .io games, where the player appears as a circle, and an arrow comes off the circle always pointing the direction the mouse is.

Since scratch uses a three-point system for drawing triangles, I've been trying to find a formula that will use the circle's circumference and the variables mouseX and mouseY to find the bottom two points for the triangle, no matter where the mouse is on the screen. Because of this I've been using this formula:

mouseXRelative = mouseY-(mouseX-(x+25));
mouseYRelative = mouseX-(mouseY-(y+25));
cEdgeXL = x + 25 * cos(mouseXRelative*radians(1));
cEdgeYL = y + 25 * sin(mouseYRelative*radians(1));
cEdgeXR = x - 25 * cos(mouseXRelative*radians(1));
cEdgeYR = y - 25 * sin(mouseYRelative*radians(1));
  
stroke(0,0,0);
triangle(cEdgeXL,cEdgeYL,mouseX,mouseY,cEdgeXR,cEdgeYR);

MouseXRelative and mouseYRelative are meant to hold the distance relative to the endpoint of the triangle.

I've gotten it pretty close to what I want, but the main bug I've come to find is that the points will almost spin around the edge of the circle? It looks really weird.

2

There are 2 answers

1
Vlad Feinstein On

Are you over-engineering it?

If you draw that triangle on the "circle" sprite, you could just instruct it to point to the mouse.

See my demo on Scratch.

Here is an example of the sprite's costume: sprite

And here is the code that rotates the sprite: code

0
Justin Neugebauer On

Like Vlad suggests, you are in fact, over-engineering it, though I don't think he captured your original need. It appears to me that you are looking to make a triangle revolve around a centered player, you seem to have tried to use the pen tool to make the triangle appear in every frame and move corresponding to the mouse. Let me discuss the pros and cons of pen vs sprite, so as to better understand how to solve this problem;

Pros

  • Pen can be fast and efficient for high-clone endeavors.
  • Pen can pass the clone limit of 301, becoming a useful tool in 3D design.
  • Pen can be of various sizes and shapes depending on what you draw and will never decrease picture quality.
  • Pen can erase all instances at once.

Cons

  • Pen is really difficult to program.
  • Pen becomes resource intensive.
  • Pen can not draw in specific layers.
  • Pen can not exceed the drawing area.
  • Pen can not erase separate instances.
  • Pen is not reliable for following costumes in the context of "x":"y" cords.

You want a penned triangle that follows the cursor while maintaining a circular revolution. This, by context alone, will be difficult to program. The Circular motion is a complex constraint to have on really any object, and making it point somewhere at the same time is even more so. In reality, a simple triangle costume will be more efficient. By first offsetting the triangle in the offset you can already gain the complex circular constraint, but pointing in the direction of the mouse would interfere with the movement. so instead, make a tiny circle and set it to full transparency, offset this in the editor from the center point. You now have a circle revolving the center of the player. Now make the triangle and make it follow the circle, and at the same time have it pointing towards the mouse. This should give you what you were looking for in 2 costumes and 2 scripts.