Object Attraction

Материал из Поле цифровой дидактики
Описание Как имитировать притяжение объектов друг к другу?
Область знаний Биология, Информатика, Физика
Область использования (ISTE) Computational Thinker
Возрастная категория 8


Поясняющее видео
Близкие рецепту понятия Преследовать другого агента
Среды и средства для приготовления рецепта: Scratch, Snap!, StarLogo Nova

In Object Attraction two or more objects are attracted to each other; or are pulling on each other. This can also be referred to as gravity.

Scratch

Elliptical Orbit Method

One way to make one object attracted to another object is to make a sprite "Orbit" around the other object. This method is always 100% accurate and will automatically move when the object that it is being rotated about moves. Here is a script that creates an elliptical orbit:

when green flag clicked
set [semi-major-axis v] to [110] //longest radius
set [semi-minor-axis v] to [50]  //shortest radius
set [t v] to [0] // current angle
set [inclination v] to [45] //inclination of the ellipse
forever
  repeat (360)
    set [x v] to ((([cos v] of (t)) * (semi-major-axis)) + ([x position v] of [Sprite3 v]))
    set [y v] to ((([sin v] of (t)) * (semi-minor-axis)) + ([y position v] of [Sprite3 v]))
    set x to (((x) * ([cos v] of (inclination))) - ((y) * ([sin v] of (inclination))))//tilt 'inclination' degrees. Trigonometric additive theorey
    set y to (((x) * ([sin v] of (inclination))) + ((y) * ([cos v] of (inclination))))
    change [t v] by (5)
  end
end

Remember that this script goes in the object that is being rotated.

One example of the elliptical orbit method can be found here.

Velocity Method

Another method is to have a sprite change its velocity automatically based on its position. This method is very inaccurate however it can be very useful if your creating a game where you have to avoid multiple sprites. The Following scripts will be placed into the item that is "following" another sprite:

when green flag clicked
set [y speed v] to [0]
set [x speed v] to [0]
forever
  if <([x position v] of [Sprite2 v]) < (x position)> then
    change [x speed v] by (-0.1)
  end
  if <([x position v] of [Sprite2 v]) > (x position)> then
    change [x speed v] by (0.1)
  end
  if <([y position v] of [Sprite2 v]) < (y position)> then
    change [y speed v] by (-0.1)
  end
  if <([y position v] of [Sprite2 v]) > (y position)> then
    change [y speed v] by (0.1)
  end
end

when green flag clicked
forever
  go to x: ((x position) + (x speed)) y: ((y position) + (y speed))
end
Пример проекта с притяжением объектов
http://scratch.mit.edu/projects/dazman/1346019

Trigonometric Method

  • Simulating Gravity

Direct Movement Method

This method although far more basic than the previous methods, can be very useful in certain situations. This method is very accurate and will move automatically with the sprite. Here is an example script that would be place in the sprite that is "following" the other sprite:

when green flag clicked
forever
  point towards [Sprite2 v]
  move (5) steps
end
http://scratch.mit.edu/projects/dazman/1346041

Wall Method

This method sort of has a fast wall attraction.

when green flag clicked
forever
  set x to ((10)*(mouse x))
  set y to ((10)+(mouse y))
end


StarLogo Nova