Hi everyone, this is my first post so bear with me! I am trying to create a camera to follow my ball and I can’t figure it out… If anyone has any tips please feel free to share! I have provided the code below feel free to use and improve it. Thanks!
--# Main
-- Phisics Project
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_RIGHT)
-- Use this function to perform your initial setup
function setup()
-- parameter.boolean("UseAccelerometer")
bullets = {}
ball = physics.body(CIRCLE, 28)
ball.x = WIDTH/1.9
ball.y = HEIGHT/2
ball.restitution = .9
ball.gravityScale = 0
ball.sensor = true
floor = physics.body(EDGE, vec2(0,0), vec2(WIDTH, 0))
floor.restitution = 0
rightWall = physics.body(EDGE, vec2(WIDTH,0), vec2(WIDTH,HEIGHT))
rightWall.restitution = 0
leftWall = physics.body(EDGE, vec2(0,0), vec2(0, HEIGHT))
leftWall.restitution = 0
ceiling = physics.body(EDGE, vec2(0,HEIGHT), vec2(WIDTH, HEIGHT))
ceiling.restitution = 0
defaultGravity = physics.gravity()
bgLines = StreamingLines()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0, 0, 0, 255)
bgLines.spawnRate = BackgroundSpawnRate
bgLines:update()
bgLines:draw()
physics.gravity(Gravity)
-- This sets the line thickness
strokeWidth(5)
ellipse(ball.x, ball.y, 55)
pushStyle()
fill(251, 1, 35, 255)
for i, bullet in ipairs(bullets) do
ellipse(bullet.x, bullet.y, 10)
-- bullet.y = bullet.y + 5
end
popStyle()
-- if UseAccelerometer == true then
-- physics.gravity(Gravity)
--else
-- physics.gravity(defaultGravity)
-- end
fill(0, 0, 0, 255)
ball.x = ball.x +Gravity.x *5
ball.y = ball.y +Gravity.y*5
-- Do your drawing here
end
function touched(touch)
if touch.state == BEGAN then
dx = touch.x - ball.x
dy = touch.y - ball.y
theta = math.atan(dx,dy)
deg = (theta * (180 / math.pi) -90)*-1
--angle = math.tan(theta)
print("degree"..deg)
y = math.sin(deg)*100
x = (math.cos(deg)*100)*-1
print("x"..x)
print(y)
bullet = physics.body(CIRCLE, 10)
bullet.x = ball.x
bullet.y = ball.y
bullet.gravityScale = 0
bullet:applyForce(vec2(dx,dy))
table.insert(bullets, bullet)
end
end
--# Lines
--parameter.boolean("UseAccelerometer", true)
StreamLine = class()
function StreamLine:init(pos, vel)
self.position = pos
self.velocity = vel
end
function StreamLine:update()
self.position.y = self.position.y - self.velocity
end
function StreamLine:draw()
p = self.position
line(p.x + Gravity.x*200,p.y,p.x + Gravity.x * 200,p.y + self.velocity)
end
function StreamLine:shouldCull()
-- Check if off the bottom of the screen
if (self.position.y + self.velocity) < 0 then
return true
end
return false
end
StreamingLines = class()
function StreamingLines:init()
self.minSpeed = 5
self.speed = 30
self.spawnRate = 2
self.lines = {}
end
function StreamingLines:updateAndCull()
toCull = {}
for i,v in ipairs(self.lines) do
if v:shouldCull() then
-- table.insert( toCull, i )
table.remove( self.lines, i )
else
v:update()
end
end
-- print("Removing ", #toCull)
--for i = #toCull,1,-1 do
-- table.remove( self.lines, i )
--end
end
function StreamingLines:update()
-- Create spawnRate lines per update
i = 1,self.spawnRate do
-- Generate random spawn location
vel = math.random(self.minSpeed, self.speed)
spawn = vec2( math.random(WIDTH), HEIGHT + vel )
table.insert(self.lines, StreamLine(spawn, vel))
end
-- Update and cull offscreen lines
self:updateAndCull()
end
function StreamingLines:draw()
--print("Num lines = ", #self.lines)
pushStyle()
noSmooth()
stroke(179, 153, 180, 173)
strokeWidth(2)
lineCapMode(SQUARE)
for i,v in ipairs(self.lines) do
v:draw()
end
popStyle()
-- if UseAccelerometer == true then
--physics.gravity(Gravity)
--else
-- physics.gravity(defaultGravity)
-- end
end