Physics not working?

So I’m experimenting with physics with the help of this guide; and my ball flies out through the wall?

-- Physics 

function setup()
    diam = 60
    w1 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    w2 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    w3 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    w4 = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    ball = physics.body(CIRCLE, 30)
    ball.gravityScale = 1
    ball.restitution = .8
    ball.x = 50
    ball.y = 50
    ball.linearVelocity = vec2(5000, 5000)

function draw()
    --Set up color stuff
    background(0, 0, 0)
    --Set up ball stuff
    --Draw the ball using x,y position of physics object
    --Set up wall stuff
    stroke(255, 255, 255, 255)
    --Draw walls
    line(0, 0, WIDTH, 0)
    line(0, 0, 0, HEIGHT)
    line(WIDTH, 0, WIDTH, HEIGHT)
    line(0, HEIGHT, WIDTH, HEIGHT)

@warspyking Add this line in setup()


@dave1707 Thank you, what exactly does that do?

Look at the build in manual for physics. At the very bottom is the explanation for physics.continuous.

@warspyking The ball is flying out because you set it to an insane speed…try something much smaller. But, to make it work at such speeds, you could do physics.continuous = true like @dave1707 suggested, but with a lot of physics bodies that might get laggy. Using body.bullet = true will fix the problem too, and only affect that one object that was having the problem.

@SkyTheCoder, I havent noticed any extra lag with physics.continuous, i end up using for almost all physics as things go through edges far too easily otherwise

Not 100% clear on every detail of Codea’s implementation but from the box2d docs:


Game simulation usually generates a sequence of images that are played at some frame rate. This is called discrete simulation. In discrete simulation, rigid bodies can move by a large amount in one time step. If a physics engine doesn’t account for the large motion, you may see some objects incorrectly pass through each other. This effect is called tunneling.

By default, Box2D uses continuous collision detection (CCD) to prevent dynamic bodies from tunneling through static bodies. This is done by sweeping shapes from their old position to their new positions. The engine looks for new collisions during the sweep and computes the time of impact (TOI) for these collisions. Bodies are moved to their first TOI and then halted for the remainder of the time step.

Normally CCD is not used between dynamic bodies. This is done to keep performance reasonable. In some game scenarios you need dynamic bodies to use CCD. For example, you may want to shoot a high speed bullet at a stack of dynamic bricks. Without CCD, the bullet might tunnel through the bricks.

Fast moving objects in Box2D can be labeled as bullets. Bullets will perform CCD with both static and dynamic bodies. You should decide what bodies should be bullets based on your game design. If you decide a body should be treated as a bullet, use the following setting.

bodyDef.bullet = true;

The bullet flag only affects dynamic bodies.

Box2D performs continuous collision sequentially, so bullets may miss fast moving bodies.

@spacemonkey Wow! Thanks for the information. That’s better than anything I got from Codea reference!