Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Physics not working?

edited January 2015 in General Posts: 100

So I'm experimenting with physics with the help of this guide; https://coolcodea.wordpress.com/2013/03/21/applying-gravity/ 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)
end

function draw()
    --Set up color stuff
    background(0, 0, 0)

    --Set up ball stuff
    fill(255,0,0)
    strokeWidth(0)


    --Draw the ball using x,y position of physics object
    ellipse(ball.x,ball.y,diam)

    --Set up wall stuff
    stroke(255, 255, 255, 255)
    strokeWidth(2)

    --Draw walls
    line(0, 0, WIDTH, 0)
    line(0, 0, 0, HEIGHT)
    line(WIDTH, 0, WIDTH, HEIGHT)
    line(0, HEIGHT, WIDTH, HEIGHT)
end

Comments

  • dave1707dave1707 Mod
    Posts: 7,606

    @warspyking Add this line in setup()

        physics.continuous=true
    
  • @dave1707 Thank you, what exactly does that do?

  • dave1707dave1707 Mod
    Posts: 7,606

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

  • Posts: 1,976

    @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.

  • Posts: 2,042

    @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

  • edited January 2015 Posts: 454

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

    Bullets

    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!

Sign In or Register to comment.