bullets with physics? how should gravity work?
Posted: Sat Nov 04, 2017 12:27 am
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.
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.
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.
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
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
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.