Simple barebones physics collision not happening - any idea why?
  • mystakermystaker February 16

    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
    
  • sanitsanit February 16

    Please see @Ipda41001 posted
    http://twolivesleft.com/Codea/Talk/discussion/547/physics-help

    Just add

     Floor.type = STATIC
    
  • mystakermystaker February 16

    From the documentation, "Static bodies are unaffected by forces and collisions." Sounds discouraging.

  • SimeonSimeon February 16

    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.

  • mystakermystaker February 16

    Ah, thanks. Both STATIC and KINEMATIC make them collide correctly - why doesn't DYNAMIC, do you know?

  • SimeonSimeon February 16

    I think the floor is probably falling due to gravity when it's not static. You'd need to hold it in place with a joint or disable gravity.

  • mystakermystaker February 16

    Ah, of course! Thanks so much!

  • raferguzmanraferguzman February 23

    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?

  • Ipad41001Ipad41001 February 23

    It's the way the floor is drawn

    Floor.y = 20

    But floor.y isn't added when the lines of the floor are drawn.

    The example http://twolivesleft.com/Codea/Talk/discussion/547/physics-help

    uses translate() to correct for floor.x, floor.y

  • raferguzmanraferguzman February 24

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

  • Ipad41001Ipad41001 February 24

    Ipad41001, codea lua addict and geek of mystery .... Spoon! (The Tick cartoon refrenence)

Howdy, Stranger!

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

Sign In with OpenID Sign In with Google

Sign In Apply for Membership

In this Discussion

Tagged