bullets with physics? how should gravity work?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
xThomas
Prole
Posts: 16
Joined: Tue May 02, 2017 2:43 am

bullets with physics? how should gravity work?

Post by xThomas »

so , how would you do this?

The first ways I tried were to have a time variable for each bullet, it would step that little bit (say, 0.0333333..) and then add the sine of that to the x position. i.e.

Code: Select all

function Bullet(x,y)
    return {x=x, y=y, time=0, xvel=math.random()>=0.5 and -1 or 1}
end
bullets = {Bullet(512,384);}
function step()
    for i,v in ipairs(bullets) do
        v.time = v.time + 1/30
        v.x = v.x + math.sin(v.time) * v.xvel
    end
end
That was great, it makes a swaying back and forth motion. . If you add a negative yvelocity you get a helix. spiral is easiest, just multiply time by the sine/cosine of time...

But, it was too simple. I realized that I wanted the bullet to slow down more when it reached the ends. Also, it was a bit convoluted to control how fast it swayed side to side, and how far apart the swaying would be. i.e, increasing the time faster makes it sway faster, but now it covers less distance in each swing.

So, gravity. I think i can set a gravity variable. My question is, how should gravity work? I can probably throw out all of the sine,cosine stuff... I was thinking... I'd have the inital x position of the bullet, and to swing it between that initial position and another... well, look.

Code: Select all

--gx is center of gravity along x axis, pullX is the force it pulls per second, velX is the speed you move along x per second
function Bullet(x,y,gx) return {x=x, gx=gx, pullX=1,velX=0, y=y} end
function step()
    for i,v in ipairs(bullets) do
        v.x=v.x + v.velX/30
        v.velX=v.x > v.gx and v.velX - v.pullX/30 or v.velX + v.pullX/30
    end
end
just jotting down ideas.
But yeha... I'm not sure. Is this how gravity would work? Are these good variable names? Do you think this can be a lot better? etc. I'm still pretty new to programming but I think I have a grasp of some basics at this point.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: bullets with physics? how should gravity work?

Post by ivan »

Generally speaking, changing the position in discrete steps is NOT a good way to handle bullets. You see, bullets in games are typically small and move fast. When your step get bigger then you are bound to get tunneling. So keep in mind that the targets you are trying to hit need to be reasonably large and round (no thinner than math.max(delta)*maxBulletVelocity).
But, it was too simple. I realized that I wanted the bullet to slow down more when it reached the ends
math.sine already does that for you, it's only a question of making the result look more exaggerated.

Code: Select all

-- definition:
bullet.offset = 200 -- initial x-offset of the bullet
bullet.amp = 100 -- amplitude or swaying distance along x
bullet.elapsed = 0 -- elapsed time in seconds
bullet.period = .5 -- period of oscillation in seconds

-- update:
local r = bullet.elapsed/bullet.period
local n = math.sin(r*math.pi*2)
-- n = normalized wave in range -1 to 1
-- you could use something like n = n^2 to make it more exaggerated
bullet.x = bullet.offset + (n*bullet.amp) * dt
There are other forms of wave-forms you could try too: http://2dengine.com/doc/gs_wave.html
If you want to have gravity along the axis of swaying, my first reaction would be something like: o = o + gravity*dt
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 108 guests