Hi all,
I’m trying to get a snake like movement working in my game which I cannot solve. The movement I’m after is exactly like Nimble Quest:

I’m pretty close I think here is what I’ve got:
function M.update(self, dt, mself)
local position = go.get_position()
if self.current_direction == MOVEMENT_DIRECTION.up then
position.y = position.y + self.movement_speed * dt
elseif self.current_direction == MOVEMENT_DIRECTION.down then
position.y = position.y - self.movement_speed * dt
elseif self.current_direction == MOVEMENT_DIRECTION.left then
position.x = position.x - self.movement_speed * dt
elseif self.current_direction == MOVEMENT_DIRECTION.right then
position.x = position.x + self.movement_speed * dt
end
position.z = -position.y / 100
go.set_position(position)
if self.leader ~= hash('/nil') and #self.turning_points > 0 then
local pos = go.get_position()
local turn_point = self.turning_points[1]
if defmath.dist2d(pos.x, pos.y, turn_point.position.x, turn_point.position.y) < 0.055 then
go.set_position(turn_point.position)
change_direction(self, turn_point.direction, mself)
table.remove(self.turning_points, 1)
end
end
end
So the character continue to move forward until it hits a turning point, which is just position and direction stored in a table. The problem I think is because position.y + speed * dt sets the position to a floating number the character either misses the point or continues to get a little closer/further away to the leader character and eventually ends up on top of each other.
Here is how I add a turning point:
function change_direction(self, new_direction, mself)
if self.current_direction == new_direction then
return
end
self.current_direction = new_direction
local turn_point = {
position = go.get_position(),
direction = self.current_direction
}
if self.follower ~= hash('/nil') then
msg.post(self.follower, 'leader_turned', { turning_point = turn_point })
end
end
Tried just doing position.y = position.y + 2 so I get whole numbers on the movement but it’s too fast and I’d like different characters to have different movement speeds. Another way I tried was too add
self.t = self.t + dt
if self.t >= 0.333 then
end
Which slowed the movement down using position.y + 2 but made the movement really choppy. Finally I used defmath.round(position) and stored it in self.rounded_position and did my checks with that but had the same issue of following characters getting to close to each other / missing the turning point.
Could anyone give me any advice?
Thanks.