Straight movement bug with set_max_distance?

Started by llamazing, September 08, 2017, 07:31:41 AM

Previous topic - Next topic
I'm trying to use a straight movement, and the straight_movement:set_max_distance(max_distance) function is not working the way that I'd expect.

Below is simplified code of what I am trying to do. At 4 seconds the table obj is moved 100 pixels to the right, and then 3 seconds after that it is moved 100 pixels to the left to return it to its original position.

The problem is that the set_max_distance function appears to be calculating the distance from (0,0), when I'd expect it to calculate the distance from what the coordinates were at the start of the movement. The result is that the second movement ends up being a 200 pixel movement to the left instead of the 100 pixels that I wanted (the object stops when x=-100, which is 100 pixels from (0,0)).

Code ( lua) Select
local obj = {x=0, y=0}
sol.timer.start(4000, function()
  print(obj.x) --0 at 4 seconds
 
  local movt1 = sol.movement.create"straight"
  movt1:set_speed(100)
  movt1:set_angle(0)
  movt1:set_max_distance(100)
  movt1:start(obj)
 
  function movt1:on_finished() print(obj.x) end --obj.x=100 at 5 seconds
 
  sol.timer.start(3000, function()
    print(obj.x) --100 at 7 seconds
   
    local movt2 = sol.movement.create"straight"
    movt2:set_speed(100)
    movt2:set_angle(math.pi)
    movt2:set_max_distance(100)
    movt2:start(obj)
   
    function movt2:on_finished() print(obj.x) end --obj.x=-100 at 9 seconds; expected obj.x=0 at 8 seconds
  end)
end)