#### Howdy, Stranger!

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

# Question-Looking for code to help with a collision

Posts: 1

I know how to detect if two sprites collide but I am still looking for how to make, let's say a ball and a player, if the player collides with the ball then the ball needs to move in the opposite direction that the sprite came from. Just got codea recently and I am still learning any help would be greatly appreciated.

• Posts: 8,388

@dRaXuS Here's a simple pong type example. Slide your finger left/right anywhere on the screen to move the paddle Left/right.

``````displayMode(FULLSCREEN)

function setup()
physics.continuous=true
-- create ball
b=physics.body(CIRCLE,20)
b.x=WIDTH/2
b.y=HEIGHT-100
b.restitution=1
-- create screen edges
e1=physics.body(CHAIN,true,vec2(0,0),vec2(0,HEIGHT),
vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
p=physics.body(POLYGON,vec2(-30,0),vec2(30,0),vec2(30,-5),vec2(-30,-5))
p.x=WIDTH/2
p.y=200
p.type=KINEMATIC
end

function draw()
background(40, 40, 50)
fill(255)
ellipse(b.x,b.y,40)
stroke(255)
strokeWidth(5)
line(p.x-30,p.y,p.x+30,p.y)
end

function touched(t)
if t.state==MOVING then
p.linearVelocity=vec2(t.deltaX*50,0)
end
end
``````