Enforcing the player to follow a polyline (More challenging than it looks)
Posted: Sat Apr 23, 2016 9:40 pm
Hi guys/gals,
I'm working on a Qix/Volfied like game.
So the player is only allowed to move on the white line shown above. I've managed that easily with the code below except with one big problem.
Some of you can already see the the problem: Corners.
The position update expression that I use gives a player step of 1.13 pixels per frame for 60 FPS. That means I will most probably miss the corner positions by fractions of pixels (meaning I will never really reach the corner) thus my check_point_collision() method will never see the other line that makes up the corner so the player will get locked.
I've also tried snapping to exact position of the corner when near it but at high FPS because the player step per frame is really really small, you can't escape the corner's snap force.
Also trying to detect an upcoming corner with a collision check (point in box) gives problems with the very small cornery areas like shown in above picture: It will detect more than one corner.
I feel like I'm missing a really obvious way to do it. If you have any pointers I would really appreciate it.
Thanks
I'm working on a Qix/Volfied like game.
So the player is only allowed to move on the white line shown above. I've managed that easily with the code below except with one big problem.
Code: Select all
-- Check where will the player be next frame
-- But don't update the actual position yet
local x = player_pos.x + dt * player_vector.x * player_speed
local y = player_pos.y + dt * player_vector.y * player_speed
-- Check if the future position is still on the polyline
-- check_point_collision() returns the single line that the player is on if the point is on it
-- otherwise return nil
local line = polyline:check_point_collision(x, y)
if line ~= nil then
-- Check if the line is horizontal or vertical
if line[1].x == line[2].x then
-- If it's a vertical line then the future position's x should
-- be the same as this vertical line (snap to line)
x = line[1].x
else
-- If it's a horizontal line then the future position's y should
-- be the same as this horizontal line (snap to line)
y = line[1].y
end
-- Update the actual position of the player
player_pos:set(x, y)
end
-- If the future position is out of the polyline then do nothing (don't update the actual position)
The position update expression that I use gives a player step of 1.13 pixels per frame for 60 FPS. That means I will most probably miss the corner positions by fractions of pixels (meaning I will never really reach the corner) thus my check_point_collision() method will never see the other line that makes up the corner so the player will get locked.
I've also tried snapping to exact position of the corner when near it but at high FPS because the player step per frame is really really small, you can't escape the corner's snap force.
Also trying to detect an upcoming corner with a collision check (point in box) gives problems with the very small cornery areas like shown in above picture: It will detect more than one corner.
I feel like I'm missing a really obvious way to do it. If you have any pointers I would really appreciate it.
Thanks