#### 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 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)

physics.gravity(Gravity)
fill(0, 255, 29, 255)
``````
Tagged:

• Posts: 8,625

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