Making physics object rotate as it bounces

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

Also, a little side question. Does anyone know why the balls get stuck to the walls lining the screen sometimes? They kind of attach to the walls and only move parallel along the wall, and won’t bounce off the wall when hit. Seems like if the physics body is moving under a certain velocity when it touches a wall it sticks to the wall. If you turn the amount of balls up in setup you will see it more often.

Is this a bug @Simeon ?

@Crumble Here are changes to the ball class. See the lines of code marked – added.

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)
    self.p.friction=1   -- added
end

function Ball:draw()
    fill(255, 0, 0, 255)
    pushMatrix()    -- added
    translate(self.p.x,self.p.y)    -- added
    rotate(self.p.angle)    -- added
    sprite("Planet Cute:Star",0,0,96,96)
    --ellipse(self.p.x, self.p.y, 64)
    popMatrix() -- added
end

@Crumble The stars don’t attach themselves to the wall. They will move away from the wall if they get hit just right.

@dave1707 Thanks Dave! You are awesome! That will be super useful!

Oddly enough, adding friction seems to fix the bug in my second post.

@dave1707 You’ll notice in the first code that I posted that some of the balls, when approaching the walls very slowly, will stick to the walls and not bounce off at an angle even though they approached at an angle. They will then only move parallel to the wall even if hit. Boost the amount of balls up to 15 or more and you will see it happen a lot.

Same kind of bug happens when you have device gravity on and let a ball rest in a corner, it gets stuck.

I’m guessing that it’s just a shortcoming of Box2D physics, and not Codea’s fault.

@Crumble I don’t know anything about the workings of Box 2D, so I can’t say if any of that is a bug or just the way it works. One thing I know is you can balance one ball on top of another and it will stay there. That doesn’t happen in real life unless one of the balls has a flat spot on it.