From Test-Scratch-Wiki

Revision as of 23:31, 12 June 2016 by InterwikiBot (talk | contribs) (Uploading Page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

Warning Tip: Please remember to adjust the scripts shown in this tutorial as necessary to best fit your project.

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 [distance v] to [75]
set [rotation v] to [0]
forever
  repeat (360)
    set x to ((([sin v] of (rotation)) * (distance)) + ([x position v] of [Sprite2 v]))
    set y to ((([cos v] of (rotation)) * (distance)) + ([y position v] of [Sprite2 v]))
    change x by ((([cos v] of (rotation)) * (distance)) * (1))
    change y by ((([cos v] of (rotation)) * (distance)) * (1))
    change [rotation v] by (1)
  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

One example of the velocity method can be seen here.

Trigonometric Method

Main article: 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

One example of the direct movement method can be seen here.

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

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.