Moving one point to another at a static speed

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
SudoCode
Citizen
Posts: 61
Joined: Fri May 04, 2012 7:05 pm

Moving one point to another at a static speed

Post 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.
Attachments
withtween.love
(1.75 MiB) Downloaded 265 times
notween.love
(1.75 MiB) Downloaded 265 times
Last edited by SudoCode on Sat Sep 29, 2012 6:46 pm, edited 1 time in total.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Moving one point to another at a static speed

Post 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)
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
SudoCode
Citizen
Posts: 61
Joined: Fri May 04, 2012 7:05 pm

Re: Moving one point to another at a static speed

Post by SudoCode »

What? Everything works fine for me.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Moving one point to another at a static speed

Post by Lafolie »

My mistake, I'm really tired, here's the error I get:

http://cl.ly/image/2f2w3F1k1O0L
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
FrogsFriend
Prole
Posts: 6
Joined: Mon Aug 27, 2012 10:21 am

Re: Moving one point to another at a static speed

Post 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.
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Moving one point to another at a static speed

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Moving one point to another at a static speed

Post 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?
When I write def I mean function.
SudoCode
Citizen
Posts: 61
Joined: Fri May 04, 2012 7:05 pm

Re: Moving one point to another at a static speed

Post 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.
Last edited by SudoCode on Sat Sep 29, 2012 6:56 pm, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Moving one point to another at a static speed

Post 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.
Help us help you: attach a .love.
SudoCode
Citizen
Posts: 61
Joined: Fri May 04, 2012 7:05 pm

Re: Moving one point to another at a static speed

Post by SudoCode »

Sorry, I wasn't aware of that :|

I just deleted the folder since I'm not using it yet anyway.
Attachments
AStar.love
(1.75 MiB) Downloaded 286 times
Post Reply

Who is online

Users browsing this forum: No registered users and 77 guests