SmallJoker / boost_cart

The original version of minetest_game's carts (since 2018).
15 stars 14 forks source link

Make a stopped cart resume in its last direction. #10

Closed Hawk777 closed 8 years ago

Hawk777 commented 8 years ago

As currently written, if a cart completely stops moving on top of a power rail, then when the power rail is powered, the cart starts searching for which direction to move based on an arbitrary direction vector of (1,0,0). This pull request keeps track of the direction in which the cart was most recently moving, allowing the search to start from that direction instead. That means a cart that stops on straight track will resume motion in the same direction when the power rail is powered up. The last-nonzero-direction vector is persistent, so the cart will even move in the proper direction after a world save/load cycle.

Hawk777 commented 8 years ago

The problem with old_dir as boost_cart was originally written is that it can become the zero vector in on_step when the cart stops moving. I just tried modifying that code to avoid setting old_dir if it would become zero; that kept the behaviour I wanted in terms of resuming motion in the right direction, but it broke other things; for example, after placing a fresh cart, if old_dir was initially zero then punching the cart would not work, whereas if old_dir was initialized to (1,0,0) punching or powering the newly placed cart would make it move slowly or sometimes in the wrong direction. Perhaps I don’t fully understand what old_dir is for; this is why I decided to keep things simple by just adding a new variable that was solely for recording the most recent direction in which the cart was moving, without affecting any of the other pathfinding, acceleration, etc. logic—this new variable would always record the most recent direction of motion, and would only ever be used in one place, the “powered cart while stationary” scenario.

Do you have any other advice on why using old_dir might not have worked?

SmallJoker commented 8 years ago

old_dir is used to reconstruct the skipped nodes when the lag is high. The other purpose is to turn the cart direction (detecting curves). This means that the variable can be modificated without problems when the cart stopped. Now the initial value is set to 1, 0, 0, so it's still possible to punch the cart.

947d863

Hawk777 commented 8 years ago

Thanks for this! I think what I missed out in my attempt was updating old_dir in on_punch.