Sprite in Scratch is non-smoothly "skipping" when being rotated

145 Views Asked by At

I am making a Squash game in scratch and I want the racket-sprite to always be straight "infront of me" as I move (with the mouse) around the court. This almost work with my code only that the racket is not rotating smoothly as I change direction but rather skipping erratically. It is difficult to see but it appears to skip between increments of 90 degrease (0, 90, 180 etc.) instead of rotating smootly between all the degrees (0, 1, 2, 3, 4 deg etc.) as I change direction. The unsmooth skipping is worse when moving slowly. Please see attched code

Code for make racket sprite move ("set x to -> mouse x" and "set y to -> mouse y")and rotate (point towards -> Mouse Pointer) with cursor and

When I take away the "set x to (mouse x)" and "set y to (mouse y)"-block it rotates nice and smoothly so it is probably something there with the interaction between these two instructions of rotating and following the cursor that causes the problem.

I have also tried for example to add "set rotation style" to all around but no differens.

https://scratch.mit.edu/projects/942479436/

2

There are 2 best solutions below

3
On

You don't need the set rotation style or the go to block. Just this will suffice:

enter image description here

[scratchblocks]
when green flag clicked
point towards [mouse pointer v]
set x to (mouse x)
set y to (mouse y)
[/scratchblocks]
1
On

point towards (mouse pointer) becomes more sensitive as sprite and mouse pointer come closer together. To make matters worse, mouse coordinates are rounded to the nearest integer value. The slightest hand movement may cause mouse x or mouse y to change by 1. That may not seem like a lot, but if the distance between sprite and mouse pointer is 1 or less, then this tiny mouse movement could easily correspond to an angle of 45° or even 90°.

From your code it is clear that sprite and mouse are likely to be very close together. The sprite is moved to the mouse position and is pointing towards the mouse pointer. That is problematic; you are basically asking the sprite to point at its own location. The only reason why there is still some distance between sprite and mouse pointer, is because loops in Scratch (in this case forever) have a built-in delay of 0.03 seconds. The sprite is always pointing where the mouse pointer was 0.03 seconds ago. The slower you move the mouse, the closer the sprite will be to the mouse pointer, and the more sensitive and jumpy the direction of the sprite will be.

There are various ways to improve this. The easiest way is to let the sprite rest when close to the mouse pointer:

forever
    wait until <(distance to (mouse pointer) > 10>
    point towards (mouse pointer)
    set x to (mouse x)
    set y to (mouse y)

Live demo: https://scratch.mit.edu/projects/943574145/