Howdy, Stranger!

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

Collisions Between Circles

edited July 2014 in Questions Posts: 145

I have 2 Circle Bodies and I am trying to detect a collision between them, however it always detects a collision. Does anyone know why this would be?

Here is my code:


  function setup()


   end

  function draw()


   local circle = physics.body(CIRCLE, 50)
    -- enable smooth motion
   circle.interpolate = true
   circle.x = 100
   circle.y = 200
   circle.restitution = 0.25
   circle.sleepingAllowed = false

  local circle1 = physics.body(CIRCLE, 50)
   -- enable smooth motion
   circle1.interpolate = true
   circle1.x = 800
   circle1.y = 900
   circle1.restitution = 0.25
   circle1.sleepingAllowed = false


  end



 function collide(contact)

      if contact.state == BEGAN then 
        print("hit")
    end
 end
Tagged:

Comments

  • dave1707dave1707 Mod
    edited July 2014 Posts: 10,055

    supportedOrientations(PORTRAIT_ANY) function setup() circle = physics.body(CIRCLE, 50) -- enable smooth motion circle.interpolate = true circle.x = 300 circle.y = 100 circle.sleepingAllowed = false circle.type=STATIC circle1 = physics.body(CIRCLE, 50) -- enable smooth motion circle1.interpolate = true circle1.x = 300 circle1.y = 900 circle1.restitution = 0.8 circle1.sleepingAllowed = false end function draw() background(40,40,50) fill(255) ellipse(circle.x,circle.y,100) ellipse(circle1.x,circle1.y,100) end function collide(contact) if contact.state == BEGAN then print("hit") end end
  • edited July 2014 Posts: 282

    @austinmccoy Not sure, but maybe it's because you created the bodies in the draw function, creating them 60 times per second. This would cause a LOT of bodies to be in the same place, 'colliding'. It should be fixed if you create the bodies in the setup function. Also, if you are wondering why they are not visible, you need to create ellipses and draw them with the position of the physics bodies. I'll get back to you with some code.
    EDIT : I see @dave1707 beat me to it. Simultaneous posts!

  • Jmv38Jmv38 Mod
    Posts: 3,297

    @austinmccoy by putting the object definition in the draw, you create new ones at the same position every 16ms, so they hit each other!
    Also dont make them local, or they will get quickly destroyed

  • Posts: 145

    Thanks for the tips!

Sign In or Register to comment.