This code creates a ball which falls onto a long rectangle - except it actually falls right through the rectangle. Any clue what’s happening? Thanks.
-- Ball.lua
Ball = class()
function Ball:init(x,y,r)
self.r = r
self.body = physics.body(CIRCLE,r)
self.body.x = x
self.body.y = y
end
function Ball:draw()
pushStyle()
fill(255, 15, 0, 255)
ellipse(self.body.x,self.body.y,2*self.r,2*self.r)
popStyle()
end
-- Main.lua
function setup()
floor = physics.body(POLYGON, vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,20), vec2(0,20))
floor.x = 0
floor.y = 20
ball = Ball(144,267,10)
end
function draw()
background(40, 40, 50)
-- Draw the rectangle
pushStyle()
stroke(38, 255, 0, 255)
strokeWidth(5)
old = floor.points[4]
for k,v in pairs(floor.points) do
line(old.x, old.y, v.x, v.y)
old = v
end
popStyle()
ball:draw()
end
function collide(contact)
print("collision")
end
STATIC just means the body can not move (by force, you can still move it manually). Other bodies can collide with it, but gravity won’t cause it to fall, and other collisions won’t cause it to move. STATIC is best used for things like walls, floors, platforms, and so on.
I realize this is a slightly older thread, but I’m hoping someone might answer a question. I ran this code, but the ball seems to land slightly ABOVE the box. I can’t seem to figure out why this is. Anyone able to help?
Thanks to Ipda41001, whoever you are! I wondered if that was the problem but couldn’t quite tell. I’ll give your suggestion a try. I don’t quite understand the translate function (or any of this, really) but learning by doing seems like a good way…!