Hi all,
Still messing with physics and classes. I have stars bouncing around the screen, and I’m trying to get them to rotate as a normal ball would as it hits walls and stuff. Can anyone give me a few tips on how to do this? I know it has to do with translate() and rotate(), but it’s still a bit over my head.
Any tips would be greatly appreciated.
Thanks in advance!
Here is the code I’m working with:
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
physics.continuous=true
physics.gravity(0,0)
w1 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
w2 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
w4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
balls={}
for i=1,6 do
table.insert(balls, Ball(math.random(128, WIDTH-128),math.random(128,HEIGHT-128)))
end
end
function draw()
background(0,0,0,255)
for a,b in pairs(balls) do
b:draw()
end
end
Ball = class()
function Ball:init(x, y)
self.p=physics.body(CIRCLE, 32)
self.p.friction=0
self.p.restitution=1
self.p.position=vec2(x, y)
self.p.bullet=true
self.p.linearVelocity=vec2(math.random(-256, 256),256)
end
function Ball:draw()
fill(255, 0, 0, 255)
sprite("Planet Cute:Star",self.p.x,self.p.y,96,96)
--ellipse(self.p.x, self.p.y, 64)
end