pausing messing up bullet acceleration

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.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

pausing messing up bullet acceleration

Post by ITISITHEBKID »

Pausing in my game messes up bullet acceleration. I know why, but I'm not sure how to fix it. It is happening because I calculate acceleration by using the formula (love.timer.getTime() - bullets creation time), but while the game is paused, love.Timer.getTime is still increasing, leading to bullets zooming away as soon as the game is unpaused. What can I do about this problem? I am using love 11.0. Included are useful code snippets and the .love file for my game.

Pausing the game:

Code: Select all

function love.keypressed(key)
	if key == "escape" then 
		if gamestate == "paused" then 
			gamestate = "playing"
		else
			gamestate = "paused"
		end
	end
end
Bullet acceleration code:

Code: Select all

for i=#bullets,1,-1 do
		b = bullets[i]
		local timedif = love.timer.getTime() - b.creation_time
		if gamestate == "playing" then 
			b.x = math.round(b.x + (b.dx * dt)* (timedif/3+0.6))
			b.y = math.round(b.y + (b.dy * dt)* (timedif/3+0.6))
		end
end
Note: when running the .love file you can use f1 to spawn more enemies
Attachments
GameGameGame!.love
The game's .love file
(689.88 KiB) Downloaded 131 times
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: pausing messing up bullet acceleration

Post by ITISITHEBKID »

I accidentally uploaded this to general and not support and development, but I am not allowed to delete it. I apologize for this inconvenience.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: pausing messing up bullet acceleration

Post by zorg »

You could try passing dt from love.update into your bullet acceleration code through the code path, and in that case, you'd just need to edit love.update to include something like this:

Code: Select all

function love.update(dt)
    if gamestate == 'paused' then return end
    -- do normal processing
    updateBullets()
    -- ...etc.
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: pausing messing up bullet acceleration

Post by ITISITHEBKID »

zorg wrote: Mon May 28, 2018 10:56 pm You could try passing dt from love.update into your bullet acceleration code through the code path, and in that case, you'd just need to edit love.update to include something like this:

Code: Select all

function love.update(dt)
    if gamestate == 'paused' then return end
    -- do normal processing
    updateBullets()
    -- ...etc.
end
This did not work for me, as love.timer.getTime() still increases
User avatar
pgimeno
Party member
Posts: 3683
Joined: Sun Oct 18, 2015 2:58 pm

Re: pausing messing up bullet acceleration

Post by pgimeno »

Using love.timer.getTime() is a weird way to go about this. But if you really want to do it that way, you could subtract the total pause time from the time, to compensate (untested):

Code: Select all

local pauseTime
local totalPauseTime = 0

function love.keypressed(key)
	if key == "escape" then 
		if gamestate == "paused" then 
			gamestate = "playing"
			totalPauseTime = totalPauseTime + (love.timer.getTime() - pauseTime)
		else
			gamestate = "paused"
			pauseTime = love.timer.getTime()
		end
	end
end

...

for i=#bullets,1,-1 do
		b = bullets[i]
		local timedif = love.timer.getTime() - totalPauseTime - b.creation_time
		if gamestate == "playing" then 
			b.x = math.round(b.x + (b.dx * dt)* (timedif/3+0.6))
			b.y = math.round(b.y + (b.dy * dt)* (timedif/3+0.6))
		end
end
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: pausing messing up bullet acceleration

Post by ITISITHEBKID »

pgimeno wrote: Tue May 29, 2018 12:37 am Using love.timer.getTime() is a weird way to go about this. But if you really want to do it that way, you could subtract the total pause time from the time, to compensate (untested):
I only used love.timer.getTime() because it's what I thought of. A better way to do it would be great!
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: pausing messing up bullet acceleration

Post by zorg »

ITISITHEBKID wrote: Mon May 28, 2018 11:33 pm This did not work for me, as love.timer.getTime() still increases
Of course it does.

Apologies, it seems i missed the most important statement in my previous post;

Instead of using love.timer.getTime, You could try passing dt from love.update into your bullet acceleration code through the code path, and in that case, you'd just need to edit love.update to include something like this: <Code from my previous post>

Because the dt parameter in love.update is basically the same value you get from love.timer.getTime; if you don't call update code there, nothing will change, which is what you want... more or less.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3683
Joined: Sun Oct 18, 2015 2:58 pm

Re: pausing messing up bullet acceleration

Post by pgimeno »

I have taken a look at the full code and I'm finding it difficult to understand what you're trying to do with the different patterns. At first sight it appears that it's intended to apply an acceleration to the bullets, as if they were missiles. Is that it?

If so, the simplest way to apply acceleration is to add the acceleration times dt to the velocity. Something of this kind (which is just schematic, not adapted to your code):

Code: Select all

  vx = vx + ax * dt
  vy = vy + ay * dt
  x = x + vx * dt
  y = y + vy * dt
To adapt that to your code, you probably need to add a current acceleration variable in the bullets, which should have the same direction (cos/sin) times an acceleration that depends on the pattern.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: pausing messing up bullet acceleration

Post by ITISITHEBKID »

pgimeno wrote: Tue May 29, 2018 1:00 am I have taken a look at the full code and I'm finding it difficult to understand what you're trying to do with the different patterns. At first sight it appears that it's intended to apply an acceleration to the bullets, as if they were missiles. Is that it?
Yes, bullets have acceleration. I am implementing your changes now.
ITISITHEBKID
Prole
Posts: 18
Joined: Tue Apr 24, 2018 7:37 pm

Re: pausing messing up bullet acceleration

Post by ITISITHEBKID »

I have implemented the changes, but it still does not work properly.
Attachments
ChangedGameGameGame!.love
version with implemented changes
(691.43 KiB) Downloaded 124 times
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests