Howdy, Stranger!

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

Problems with contact function

edited May 2014 in Questions Posts: 16

I am trying to use the contact function to determine whether two objects have collided or not. When I run my program it prints "hello" regardless of whether the two objects are touching or not. Here is the code I have produced so far. I am new to Codea so if its really simple then I apologise.

function setup()

ball = physics.body(CIRCLE, 50)
ball.x = 0
ball.y = 0
ball.restitution = .5
ball.info = "ball"

enemyBall = physics.body(CIRCLE, 50)
enemyBall.x = 0
enemyBall.y = 0
enemyBall.restitution = .5
enemyBall.info = "enemyBall"

function collide( contact )

    if contact.state == BEGAN then
    if contact.bodyA.info == "ball" and contact.bodyB.info == "enemyBall"
    or contact.bodyA.info == "enemyBall" and contact.bodyB.info == "ball" then 
        print("hello")
        end
    end

function draw()

physics.gravity(Gravity)
fill(27, 255, 0, 255)
ellipse(x, playerImagePosition.y, ball.radius*1.5)

physics.gravity(Gravity)   
fill(0, 255, 29, 255)
ellipse(enemy1Position.x, 500, enemyBall.radius*1.5)

Comments

  • dave1707dave1707 Mod
    Posts: 7,533

    @jonesy166 Here's an example showing how little code is needed for collision of physics bodies. Maybe this will help some.


    function setup() ball = physics.body(CIRCLE, 50) ball.x = 270 ball.y = 500 enemyBall = physics.body(CIRCLE, 50) enemyBall.x = 200 enemyBall.y = 200 enemyBall.gravityScale=0 end function collide( contact ) if contact.state == BEGAN then print("collision") end end function draw() background(40,40,50) fill(27, 255, 0, 255) ellipse(ball.x, ball.y, ball.radius*2) ellipse(enemyBall.x, enemyBall.y, enemyBall.radius*2) end
  • Posts: 198

    I have found using the codea included physics example that draws squares around bodies to be quite useful. You don't have to understand it, just copy the physics debug draw tab and use it

  • Posts: 16

    Thanks for your help

Sign In or Register to comment.