Discuss Scratch

WarriorFlame_Studios
Scratcher
2 posts

Moving Position Towards with certain Direction

Moving Position Towards with certain Direction

So the basic one is you could move your sprite towards with its own direction using this block
move () steps

Example 1
SpritePosition: 0:0
SpriteDirection: 90
After MoveToward SpritePosition: 10:0

Example 2
SpritePosition: 0:0
SpriteDirection: 45
After MoveToward SpritePosition: 7:7

But I want a script that moves Position towards with its own Direction.
Just same as the Examples above, but without using Sprites, how about with different Directions?

VariablePosition: 0:0
VariableDirection: 30
After MoveToward VariablePosition: __:__ ?

Too Complicated? Here's actually what I want:
(move position x:() y:() direction:() towards with () steps )
–Returns position

Please help me solve this problem. Thanks!

Last edited by WarriorFlame_Studios (Feb. 12, 2022 10:14:13)

Scratch-Minion
Scratcher
1000+ posts

Moving Position Towards with certain Direction

You can use maths directions which start at angle 0 along the X axis, then turning anticlockwise to 90 up the Y axis, 180 in the negative X direction, 270 in the -Y direction and through to 360 at the X axis again. (This includes all the angles in between from 0 to 360).

Then the formula to move Distance in direction Angle is:

X = X + Distance * cos(Angle)
Y = Y + Distance * sin(Angle)

eg. for Distance = 10 and Angle = 45
X = X + 10 * cos(45)
Y = Y + 10 * sin(45)
WarriorFlame_Studios
Scratcher
2 posts

Moving Position Towards with certain Direction

Scratch-Minion wrote:

You can use maths directions which start at angle 0 along the X axis, then turning anticlockwise to 90 up the Y axis, 180 in the negative X direction, 270 in the -Y direction and through to 360 at the X axis again. (This includes all the angles in between from 0 to 360).

Then the formula to move Distance in direction Angle is:

X = X + Distance * cos(Angle)
Y = Y + Distance * sin(Angle)

eg. for Distance = 10 and Angle = 45
X = X + 10 * cos(45)
Y = Y + 10 * sin(45)

Actually, Cos is for Y-Axis and Sin is for X-Axis.
So my script is going to be like this:
define Move position X:(XPosition)Y:(YPosition)direction:(Angle)towards with(Steps) steps
set [XPositionResult] to (((XPosition) + (Steps)) * ([sin] of (Angle)))
set [YPositionResult] to (((YPosition) + (Steps)) * ([cos] of (Angle)))

when green flag clicked
Move position X:(0)Y:(0)direction:(90)towards with(10) steps
say (join [Position 0:0 after move toward at direction 90 is ] (join (round (XPositionResult)) (join [:] (round (YPositionResult)))))
wait (5) secs
Move position X:(0)Y:(0)direction:(45)towards with(10) steps
say (join [Position 0:0 after move toward at direction 45 is ] (join (round (XPositionResult)) (join [:] (round (YPositionResult)))))
wait (5) secs
Move position X:(0)Y:(0)direction:(30)towards with(10) steps
say (join [Position 0:0 after move toward at direction 30 is ] (join (round (XPositionResult)) (join [:] (round (YPositionResult)))))
wait (5) secs
Move position X:(0)Y:(0)direction:(0)towards with(10) steps
say (join [Position 0:0 after move toward at direction 0 is ] (join (round (XPositionResult)) (join [:] (round (YPositionResult)))))
wait (5) secs
Move position X:(0)Y:(0)direction:(180)towards with(10) steps
say (join [Position 0:0 after move toward at direction 180 is ] (join (round (XPositionResult)) (join [:] (round (YPositionResult)))))
wait (5) secs
say [Thanks to Scratch-Minion for the Explanation!]

It works! Thank you so much!

Last edited by WarriorFlame_Studios (Feb. 12, 2022 10:16:03)

Powered by DjangoBB