Reputation: 1294
Here is my code, and I need to make the two balls collide. The two balls are ball.png and soccer.png. I have also assigned arrow keys/wasd to move the balls. It would also be great if someone could help create a collission if the balls touch the edges of the sc
function love.load()
ball = love.graphics.newImage('ball.png')
speed = 300
x, y = 0,0
soccer = love.graphics.newImage('soccer.png')
speed2 = 300
x1, y1 = 20,20
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed*dt)
end
if love.keyboard.isDown("left") then
x = x - (speed*dt)
end
if love.keyboard.isDown("up") then
y = y - (speed*dt)
end
if love.keyboard.isDown("down") then
y = y + (speed*dt)
end
if love.keyboard.isDown("d") then
x1 = x1 + (speed2*dt)
end
if love.keyboard.isDown("a") then
x1 = x1 - (speed2*dt)
end
if love.keyboard.isDown("w") then
y1 = y1 - (speed2*dt)
end
if love.keyboard.isDown("s") then
y1 = y1 + (speed2*dt)
end
end
function love.draw()
love.graphics.draw(ball, x,y,0,0.5,0.5)
love.graphics.draw(soccer, x1,y1,0,0.25,0.25)
love.graphics.setBackgroundColor(0,255,0)
end
Upvotes: 0
Views: 74
Reputation: 14541
2D Collision detection between circles is fairly simple: Just calculate the squared distance of the center points (because using the square, you don't need a square root, the formula is Δx²+Δy². If this is smaller than the square of the sum of both radii, (r1+r2)², a collision is taking place.
Next thing you probably want to do is to calculate the impact velocity exchange, and maybe even consider spin (I didn't when I last wrote a game where you had to make spheres collide).
Upvotes: 1