Page 1 of 2

Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 3:40 am
by SudoCode
I tried messing around with vectors for a bit to attempt to move the player towards the selected grid at a static speed, but no dice. I have attached two versions of the code - one in which the played is moved towards the intended position by continuously subtracting the difference between it's current position and intended position from it's current position (the problem with this is that player speed decreases the closer it gets to the target), and another in which I attempted to use tween to interpolate the path on which to move, which just results in a delay before the player is instantaneously teleported, along with bugging out a bit.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:07 am
by Lafolie
Your love files are incorrectly packaged. Here you go: Game Distribution :)

(common error is archiving the source directory, you should be archiving the source directory contents)

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:27 am
by SudoCode
What? Everything works fine for me.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:51 am
by Lafolie
My mistake, I'm really tired, here's the error I get:

http://cl.ly/image/2f2w3F1k1O0L

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 9:10 am
by FrogsFriend
I had a similar problem when using gridlocked movement (only moving single hexes in my case but similar enough).

Try calculating the move_distance_x and move_distance_y between the start point and end point of the move, then use something like:

Code: Select all

if player.moving == true then
	player.act_x = player.act_x - (player.move_distance_x / grid_square_width / player.speed * dt)
	player.act_y = player.act_y - (player.move_distance_y / grid_square_width / player.speed * dt)

	-- force final actual x & y to exact grid x & y of movement aim point
	if math.abs(player.act_x - player.grid_x) < 1 then player.act_x = player.grid_x end
	if math.abs(player.act_y - player.grid_y) < 1 then player.act_y = player.grid_y end

	if player.act_x == player.grid_x and player.act_y == player.grid_y then
		player.moving = false
	end
end
..where player.speed is the number of seconds to move one grid square.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 9:38 am
by Przemator
Hi, try this:

Code: Select all

function moveControlled(dt)
	local maxspeed = 200
	-- where do we want to go
	local dx = controlled.grid_x - controlled.act_x
	local dy = controlled.grid_y - controlled.act_y
	
	-- how far is it
	local len = math.sqrt(dx^2 + dy^2)
	-- if it's too far then limit the speed
	if len > maxspeed * dt then
		controlled.act_x = controlled.act_x + dx * dt * maxspeed / len
		controlled.act_y = controlled.act_y + dy * dt * maxspeed / len
	else
		controlled.act_x = controlled.grid_x
		controlled.act_y = controlled.grid_y
	end
end
BTW, I don't know what this whole Beholder is (I'm new), but you should fix the way the clicked tile is being detected. I think there is a small delay between the click and the measurement, and it makes you overshoot the desired tile if you aim far.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:37 pm
by kikito
SudoCode wrote:What? Everything works fine for me.
I have the same error as Lafolie. Are you sure you are trying the .love files, and not on your uncompressed folder instead?

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:45 pm
by SudoCode
kikito wrote:
SudoCode wrote:What? Everything works fine for me.
I have the same error as Lafolie. Are you sure you are trying the .love files, and not on your uncompressed folder instead?
Yep, main.lua is in the root directory and world.lua is in src/game. I tried downloading the files from those links and they worked fine as well, unless Chrome is launching different versions of the files or something.

In any case, I managed to get movement at a static speed working thanks to Przemator, but I can't seem to reproduce overshooting the tiles. I would prefer to use tween if it can be used for this sort of thing, because I'll probably want to use it for something else in the future.

edit: it appears that overshooting the tiles is linked to maxSpeed. Doesn't seem to overshoot with a value of 100.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:51 pm
by Robin
I get the same error. I see you have a directory src/game/world as well as a module src/game/world.lua. That could be the source of the problem: Lua tries to load "src/game/world" before it tries "src/game/world.lua" and it sees that it exists, but when it tries to open "src/game/world" it gets an error because it's a directory, not a file.

Renaming one or the other should fix things.

Re: Moving one point to another at a static speed

Posted: Sat Sep 29, 2012 6:56 pm
by SudoCode
Sorry, I wasn't aware of that :|

I just deleted the folder since I'm not using it yet anyway.