Discuss Scratch

Spartan156E
Scratcher
3 posts

Genius's needed for turning script issues!

Hey guys, thanks for looking, this is a toughie.

Right firstly, what is the best way (script) to detect if it is quicker to turn clockwise or anti-clockwise? Example, calculating the best way for a ship to turn in a RTS game and not always turn left until it points the right way!

Secondly, due to Scratch's negative/positive directions, how do I write a script to work out the angle for a sprite to turn based upon the coordinates of the start and end point when half of the directions are negative? I can't use the abs function as I would sometimes need to turn in the negative half.

JUST to make things worse, because I have made a ‘hypothetical’ plane (similar to the scrolling demos) I can't use ‘Point towards X’ or similar;since the sprites don't actually leave the screen and so the pointing angle will be wrong?

PLEASE HELP! :L

Spartan
deck26
Scratcher
1000+ posts

Genius's needed for turning script issues!

https://scratch.mit.edu/projects/55006278/ finds the right direction to turn.

For the second question use arcsin and the differences between the x and y values. Use the value you get in combination with whether or not the destination y is above or below the start y and you can work out the angle.

For the second question use arctan and the x and y differences.

Last edited by deck26 (Feb. 8, 2016 11:13:10)

Spartan156E
Scratcher
3 posts

Genius's needed for turning script issues!

Hi Deck26!

Many thanks for the reply, I'll give it ago and report back!

Thanks again!
Spartan156E
Scratcher
3 posts

Genius's needed for turning script issues!

Hey Deck26,

The linked project is exactly what I was looking for! Quick question however; In my project, different sprites have different turn times based upon an agility variable, what part of the script you showed me varies the speed of the turn?

Revision: I've found that parameter, and the addition of a ‘forever move 2’ gives the desired motion of a vehicle!

Naturally credit will be provided if it's okay to modify the script?

I did the ‘atan’ suggestion, I did the x differences divided by the y differences, is that right? My sprite points 180 in the opposite direction so I have added a turn 180 block to compensate?

Kind Regards.

Last edited by Spartan156E (Feb. 8, 2016 11:33:37)

deck26
Scratcher
1000+ posts

Genius's needed for turning script issues!

Work out the difference in the x values - x-diff and similarly for y-diff.

If y-diff is not 0 calculate arctan (x-diff/y-diff). If y-diff is 0 you can easily work out whether the direction is 90 or -90 by looking at the x values.

You also need to adjust the value (subtract 180) if the abs result you get is less than 90 but should be more than 90. That will depend on which y value is highest.

It may be possible to simplify slightly but the gist of the solution is in there somewhere. I haven't time to pursue further right not.
deck26
Scratcher
1000+ posts

Genius's needed for turning script issues!

Spartan156E wrote:

Hey Deck26,

The linked project is exactly what I was looking for! Quick question however; In my project, different sprites have different turn times based upon an agility variable, what part of the script you showed me varies the speed of the turn?

Revision: I've found that parameter, and the addition of a ‘forever move 2’ gives the desired motion of a vehicle!

Naturally credit will be provided if it's okay to modify the script?

I did the ‘atan’ suggestion, I did the x differences divided by the y differences, is that right? My sprite points 180 in the opposite direction so I have added a turn 180 block to compensate?

Kind Regards.
We crossed since I hadn't reloaded the page. If you're 180 degrees out you could possibly just reverse your calculations - subtract x1 from x2 rather than x2 from x1 etc. Needs experimenting but you could just stick with what you have. Test values in all 4 quadrants though!

Last edited by deck26 (Feb. 8, 2016 11:43:02)

TheLogFather
Scratcher
1000+ posts

Genius's needed for turning script issues!

EDIT: Oh, oops… I see a lot has happened above since I first started this post (and got interrupted for nearly a couple of hours). Ah well, maybe someone will find something useful out of what I've said below…
===============================

To figure out the direction to turn, this code snippet gives you the difference between two directions, but constrained between -180 and +180 degrees:
define dirdiff = direction difference (dir1) (dir2)
set [dirdiff v] to ( ( ( ( (dir1) + (180) ) - (dir2) ) mod (360) ) - (180) )
I expect you'll want to get the direction of a particular point (so you can turn towards that direction):
define dirofpt = direction of point (x) (y)
set [dirofpt v] to ( ( [atan v] of ( ( (x) - (x position) ) / ( (y) - (y position) ) ) ) + ( (180) * ( (y position) > (y) ) )
Once you have the difference you can then turn whatever fraction of it that you want.

And you probably also want to limit the amount of turning, so here's a custom block for that:
define turn (ang) degrees, but limited to (max) degrees
if ( (ang) > (max) ) then // only allow it to turn up to "max" degrees clockwise...
turn cw (max) degrees
else
if ( (ang) < ( (0) - (max) ) ) then // ...or anticlockwise...
turn ccw (max) degrees
else // ...otherwise just turn by required amount
turn cw (ang) degrees //
end
end

Here's an example of how you can put these together to make a sprite turn towards the mouse pointer:
when GF clicked
forever
dirofpt = direction of point ( mouse x ) ( mouse y ) :: custom block // direction of mouse pointer
dirdiff = direction difference (dirofpt) (direction) :: custom block // difference between sprite's direction and mouse pointer direction
turn ( (dirdiff) / (4) ) degrees, but limited to (5) degrees :: custom block // dividing by 4 makes it slow down as it nears required direction
end
Hope that makes sense!

Last edited by TheLogFather (Feb. 8, 2016 12:30:47)

pandacornisis
Scratcher
1 post

Genius's needed for turning script issues!

dang
deck26
Scratcher
1000+ posts

Genius's needed for turning script issues!

pandacornisis wrote:

dang
Please don't necropost.

Powered by DjangoBB