Page 1 of 1

Calculating a constant random speed within love.update

Posted: Sun Feb 15, 2015 11:39 pm
by redsled
If you view the .love file I provided, then you can see that the food's speed is switching between speeds rapidly. I know this is because it's inside the game loop. However, I'm trying to figure it out so that the food speed is at a constant min 4 out of 5 times the food respawns, and at a constant max 1 out of 5 times the food spawns. I know the max speed is too fast, but I made it high enough to show the distinction.

This is the bit of code for the physics:

Code: Select all

function food.physics(dt)
	for i,v in ipairs(food) do
		local min = 75
		local max = 500
		local rand = math.random(1, 5)
		if rand == 1 then
			v.y = v.y + max * dt
		else
			v.y = v.y + min * dt
		end

		if v.y + food.h > love.graphics.getHeight() then
			table.remove(food, i)
			food.spawn(math.random(0,love.graphics.getWidth() - food.w),
				math.random(0,love.graphics.getHeight() - respawnX))
		end
	end
end
And this is the bit of code for the spawning of the food:

Code: Select all

function food.spawn(x, y)
	table.insert(food, {x = x, y = y})
end
I'm guessing I have to create a speed variable within "food.spawn", but whenever I do that and call it, the program doesn't recognize it. If I haven't explained my problem clearly please let me know. Any guesses as how to fix the problem at hand?

Re: Calculating a constant random speed within love.update

Posted: Mon Feb 16, 2015 12:09 am
by undef
You didn't upload a .love file, but I think you might just want to decrease max, right?
If max is 500 then your food would traverse 500 pixels in one second, which is a lot faster than min.

Edit: Oh sry you made that on purpose. So I don't really understand the problem I guess... :/

Re: Calculating a constant random speed within love.update

Posted: Mon Feb 16, 2015 12:29 am
by redsled
undef wrote:You didn't upload a .love file, but I think you might just want to decrease max, right?
If max is 500 then your food would traverse 500 pixels in one second, which is a lot faster than min.

Edit: Oh sry you made that on purpose. So I don't really understand the problem I guess... :/
Oops! I guess I got so caught up in making the post I forgot to even include the .love file! Hope it's available now :oops:

Re: Calculating a constant random speed within love.update

Posted: Mon Feb 16, 2015 1:32 am
by Evine
I've changed these 2 functions so that "rand = math.random(1, 5)" is only called when the food is removed by the player or by the floor.
Thinking about it. "local min" , "local max" and "local rand" should all just be in "food.spawn"

Code: Select all

function food.collision()
	for i,v in ipairs(food) do
		if v.x < player.x + player.w and
			v.x + food.w > player.x and 
			v.y < player.y + player.h and 
			v.y + food.h > player.y then
				table.remove(food, i)
				rand = math.random(1, 5)
				food.spawn(math.random(0,love.graphics.getWidth() - food.w),
					math.random(0,love.graphics.getHeight() - respawnX))
		end
	end
end
function food.physics(dt)
	for i,v in ipairs(food) do
		local min = 75
		local max = 500
		
		if v.y + food.h > love.graphics.getHeight() then
			table.remove(food, i)
			rand = math.random(1, 5)
			food.spawn(math.random(0,love.graphics.getWidth() - food.w),
				math.random(0,love.graphics.getHeight() - respawnX))
		end
		print(rand)
		if rand == 1 then
			v.y = v.y + max * dt
		else
			v.y = v.y + min * dt
		end
	end
end

Re: Calculating a constant random speed within love.update

Posted: Mon Feb 16, 2015 1:44 am
by redsled
Ahh... Ok thanks a bunch! So basically, if I'm getting this right, each time there is a new food spawned, rand gets reset once instead of changing each frame. Thanks a lot, I guess I just didn't know where exactly to set the 'rand' variable.