# Solarus Forum

## Solarus => Bugs & Feature requests => Topic started by: llamazing on September 08, 2017, 07:31:41 am

Title: Straight movement bug with set_max_distance?
Post by: llamazing on September 08, 2017, 07:31:41 am
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
1. local obj = {x=0, y=0}
2. sol.timer.start(4000, function()
3.   print(obj.x) --0 at 4 seconds
4.
5.   local movt1 = sol.movement.create"straight"
6.   movt1:set_speed(100)
7.   movt1:set_angle(0)
8.   movt1:set_max_distance(100)
9.   movt1:start(obj)
10.
11.   function movt1:on_finished() print(obj.x) end --obj.x=100 at 5 seconds
12.
13.   sol.timer.start(3000, function()
14.     print(obj.x) --100 at 7 seconds
15.
16.     local movt2 = sol.movement.create"straight"
17.     movt2:set_speed(100)
18.     movt2:set_angle(math.pi)
19.     movt2:set_max_distance(100)
20.     movt2:start(obj)
21.
22.     function movt2:on_finished() print(obj.x) end --obj.x=-100 at 9 seconds; expected obj.x=0 at 8 seconds
23.   end)
24. end)
Title: Re: Straight movement bug with set_max_distance?
Post by: Christopho on September 08, 2017, 08:58:35 am
It looks like a bug to me. Can you report it on https://github.com/solarus-games/solarus/issues/new?
I will fix this for 1.6.
Title: Re: Straight movement bug with set_max_distance?
Post by: llamazing on September 08, 2017, 02:56:26 pm
Issue 1075 (https://github.com/solarus-games/solarus/issues/1075) created