Answered by:
Simplest way to code bullet trajectory?
Question

My 10 year old just recently picked up Small Basic again. He likes shooter games, so he asked about writing a simple bullet trajectory. My physics/math is rusty and he hasn't covered trig math yet (sin/cos), so I was curious if there is a super simple way to do this, so a kid can understand, with minimum variables & math. The program below is a quick attempt at it, but it's obviously not right. Any suggestions? Thanks! :)
'Initialize graphics window GraphicsWindow.Hide() gw = 800 gh = 600 GraphicsWindow.CanResize = "False" GraphicsWindow.Top = (Desktop.Heightgh)/2 GraphicsWindow.Left = (Desktop.Widthgw)/2 GraphicsWindow.Title = "Trajectory" GraphicsWindow.Width = gw GraphicsWindow.Height = gh GraphicsWindow.BackgroundColor = "LightBlue" 'Initialize bullet bullet = Shapes.AddEllipse(20,20) xvel = 1 yvel = 1 grav = 1 x = 0 y = gh/2 'Draw bullet in flight GraphicsWindow.Show() While (x < gw And y < gh) x = x+xvel y = y+yvel+grav Shapes.Move(bullet,x,y) Program.Delay(10) EndWhile Program.End()
Answers

Looks very close, the gravity acts to increase the velocity downwards (positive using the SmallBasic screen coordinates). Gravity is just an acceleration downwards, where acceleration is just the rate of change of velocity  if the acceleration is constant as gravity is then the increase in y velocity is constant for each timestep. If there are no other forces on the bullet, then the X velocity remains constant  a reasonable approximation if there is little air resistance.
Try:
yvel = yvel+grav
x = x+xvel
y = y+yvel
You may need to scale things a bit for it to look good, ie change the initial values for xvel, yvel and grav, such as grav = 0.005
I don't have SmallBasic to test at present, but your code should work with this mod.
 Marked as answer by PeterDad Tuesday, December 21, 2010 6:12 AM
All replies

Looks very close, the gravity acts to increase the velocity downwards (positive using the SmallBasic screen coordinates). Gravity is just an acceleration downwards, where acceleration is just the rate of change of velocity  if the acceleration is constant as gravity is then the increase in y velocity is constant for each timestep. If there are no other forces on the bullet, then the X velocity remains constant  a reasonable approximation if there is little air resistance.
Try:
yvel = yvel+grav
x = x+xvel
y = y+yvel
You may need to scale things a bit for it to look good, ie change the initial values for xvel, yvel and grav, such as grav = 0.005
I don't have SmallBasic to test at present, but your code should work with this mod.
 Marked as answer by PeterDad Tuesday, December 21, 2010 6:12 AM
