simple_move.script 1.1 KB

12345678910111213141516171819202122232425262728
  1. function init(self)
  2. self.center = vmath.vector3(360, 360, 0) -- <1>
  3. self.radius = 160 -- <2>
  4. self.speed = 2 -- <3>
  5. self.t = 0 -- <4>
  6. end
  7. function update(self, dt)
  8. self.t = self.t + dt -- <5>
  9. local dx = math.sin(self.t * self.speed) * self.radius -- <6>
  10. local dy = math.cos(self.t * self.speed) * self.radius
  11. local pos = vmath.vector3() -- <7>
  12. pos.x = self.center.x + dx -- <8>
  13. pos.y = self.center.y + dy
  14. go.set_position(pos) -- <9>
  15. end
  16. --[[
  17. 1. Store the center of rotation in the script instance (available through `self`).
  18. 2. Store the movement radius.
  19. 3. Store the movement speed.
  20. 4. Store the elapsed time, in seconds.
  21. 5. Increase the elapsed time with `dt`, the delta time elapsed since last call to `update()`.
  22. 6. Compute offsets along the X and Y axis. We're using `sinus` and `cosinus` of the current time, scaled with `self.speed`, which will plot points along a circle with radius `self.radius`.
  23. 7. Create a new `vector3` which will contain the computed position.
  24. 8. Set the `x` and `y` components of the vector to the rotation center plus offsets along X and Y axis.
  25. 9. Set the computed position on the current game object.
  26. --]]