How to Move a Sprite in a Spiral
|Описание||Как организовать движение агента по спирали|
|Область знаний||Информатика, Робототехника|
|Область использования (ISTE)|
|Близкие рецепту понятия|
|Среды и средства для приготовления рецепта:||Scratch, Snap!, Лого|
This tutorial explains how to move a sprite in a spiral in Scratch. A spiral can mean any path that turns around a point, and that always approaches or always recedes from the point, so there are many types of spirals. This tutorial is about Logarithmic spirals and Archimedean spirals.
A logarithmic spiral multiplies its distance from the center by the same number whenever it revolves by the same angle around the center. This means that a line from the center will intersect any point on the spiral at the same angle, so a sprite can move in a logarithmic spiral by moving in a direction that is a fixed angle from towards the center.
The following script makes a sprite move in a logarithmic spiral clockwise and towards the Center sprite. The angle the sprite must turn by depends on the factor by which the distance from the center should be multiplied for each revolution around the center. Replace
factor :: grey reporter with this number.
when green flag clicked go to (Start v) set [Angle v] to ([atan v] of ((6.283) / ([ln v] of (factor :: grey)))) forever point towards (Center v) turn ccw (Angle) degrees move (1) steps // If this is too many, the sprite will not reach the center end
To make the sprite move counterclockwise and towards the center, replace the
turn ccw () degrees block with a
turn cw () degrees block. To make the sprite move away from the center, replace the number in the
move () steps block with a negative number.
An Archimedean spiral changes its distance from the center by the same amount for each revolution. This means that a sprite can move in an Archimedean spiral by keeping variables for direction and distance from the center, changing them at constant rates, and calculating x and y positions using trigonometric functions.
The following script makes a sprite move in an Archimedean spiral clockwise and away from the point (36, 28). The change in distance depends on how far apart the loops of the spiral should be. Replace
separation :: grey reporter with this amount, and set the initial direction and distance to correspond to where the spiral should start (this might not be the center).
when green flag clicked set [direction v] to (0) set [distance v] to (0) set [direction change v] to (1) // this controls the sprite's speed set [distance change v] to ((separation :: grey) * (([abs v] of (direction change)) / (360))) forever go to x: (((distance :: variables) * ([sin v] of (direction))) + (36)) y: (((distance :: variables) * ([cos v] of (direction))) + (28)) change [direction v] by (direction change) change [distance v] by (distance change) end
To make the sprite move towards the center, replace the value of
separation :: grey reporter with a negative number and set the initial distance to a large number. To make the sprite turn counterclockwise, set direction change to a negative number.