How to Move a Sprite in a Spiral

Материал из Поле цифровой дидактики
Описание Как организовать движение агента по спирали
Область знаний Информатика, Робототехника
Область использования (ISTE)
Возрастная категория 10


Поясняющее видео
Близкие рецепту понятия
Среды и средства для приготовления рецепта: 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.

Spiral Types

Logarithmic Spiral

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 with this number.

whenclickedgotoStartsetAngletoatanof6.283/lnoffactorforeverpointtowardsCenterturnAngledegreesmove1stepsIfthisistoomany,thespritewillnotreachthecenterend

To make the sprite move counterclockwise and towards the center, replace the turndegrees block with a turndegrees block. To make the sprite move away from the center, replace the number in the movesteps block with a negative number.

Archimedean Spiral

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 with this amount, and set the initial direction and distance to correspond to where the spiral should start (this might not be the center).

whenclickedsetdirectionto0setdistanceto0setdirectionchangeto1thiscontrolsthesprite'sspeedsetdistancechangetoseparation*absofdirectionchange/360forevergotox:distance*sinofdirection+36y:distance*cosofdirection+28changedirectionbydirectionchangechangedistancebydistancechangeend

To make the sprite move towards the center, replace the value of separation 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.