Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created October 17, 2013 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/7019250 to your computer and use it in GitHub Desktop.
Save dermotbalson/7019250 to your computer and use it in GitHub Desktop.
Arrow
--Physics 6a
--Arrow
displayMode(FULLSCREEN)
function setup()
arrows={}
end
function AddArrow()
table.insert(arrows,Ballistics(vec2(50,10+math.random(30,100)),
vec2(.95+.1*math.random(),.95+.1*math.random()),
math.random(225,275),
.01,color(255,255,0),2,30))
end
function draw()
background(0)
AddArrow()
for i,p in pairs(arrows) do
p:draw()
if p.position.x>WIDTH or p.position.y<0 then table.remove(arrows,i) end
end
end
-- Physics Library class below ---
Ballistics = class()
--constants which apply to all physics objects
Ballistics.gravityStrength=vec2(0,-133) --not accurate
--parameters are position,velocity,speed,mass,color,diameter,length
function Ballistics:init(position,direction,speed,mass,colour,diameter,length)
--these properties belong to our physics object
self.position=position
self.velocity=direction*speed
self.mass=mass
self.color=colour or color(0)
self.width,self.length=diameter,length
--initialise
self.damping= 0.99
self.acceleration = vec2(0,0) --w.accel
Ballistics.gravityOn=true
end
function Ballistics:draw()
self:AdjustPosition(DeltaTime)
pushMatrix()
fill(self.color)
pushMatrix()
local a=math.deg(math.atan(self.velocity.x/self.velocity.y))
translate(self.position.x, self.position.y)
rotate(-a)
ellipse( 0,0, self.width ,self.length)
popMatrix()
popMatrix()
end
function Ballistics:AdjustPosition(t)
--update speed
if Ballistics.gravityOn then
self.acceleration = self.acceleration + Ballistics.gravityStrength
end
self.velocity = self.velocity + self.acceleration * t
--set (x,y) position
self.position = self.position + self.velocity * t
--reset acceleration to 0
self.acceleration=vec2(0,0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment