[Help] Circle Collision detection and reaction
Posted: Mon Jun 17, 2013 9:43 am
I'm still very new to LUA/Love2D, so let me know if there are any glaring code structure issues. I have come from a heavy OO background, which I believe is quite bad for LUA.
My project is creating a simple circle collision detection and rebound system. It detects the collision fine, and I'm pretty sure my maths is right, I think my logic is very wrong though. My aim is to have the rebound occur close to real life -- taking into account changes in angle as well as speed.
If anyone could help me out with suggestions or links to examples that'd be a great help. Doesn't have to be LUA examples, can be virtually any language -- even just theory.
BTW I looked through hardon collider and it went way over my head.
The code below is the important bit, within object:update(dt). Each shape has the obvious vars (x, y, speeds, size etc) and also an ID to act as a unique identifier for each object to make this stuff easier.
edit: forgot to say I've already read http://www.wildbunny.co.uk/blog/2011/04 ... r-dummies/ and I just don't understand the last little bit about the vector.
My project is creating a simple circle collision detection and rebound system. It detects the collision fine, and I'm pretty sure my maths is right, I think my logic is very wrong though. My aim is to have the rebound occur close to real life -- taking into account changes in angle as well as speed.
If anyone could help me out with suggestions or links to examples that'd be a great help. Doesn't have to be LUA examples, can be virtually any language -- even just theory.
BTW I looked through hardon collider and it went way over my head.
The code below is the important bit, within object:update(dt). Each shape has the obvious vars (x, y, speeds, size etc) and also an ID to act as a unique identifier for each object to make this stuff easier.
edit: forgot to say I've already read http://www.wildbunny.co.uk/blog/2011/04 ... r-dummies/ and I just don't understand the last little bit about the vector.
Code: Select all
-- Check if any shapes are colliding with each other
-- Objects are on top of each other if the distance between them is greater than the sum of their radii.
-- For every shape in the shape table
for di,dv in ipairs(shapes) do
if (self.id ~= dv.id) then -- If we're not colliding with ourself
-- Distance between the two circles
H = math.distanceBetween(self.x, self.y, dv.x, dv.y)
local A = self.x - dv.x -- Distance of adjacent edge
-- Angle given when a right angle triangle is created with self and dv
local angleOfCollision = math.deg(1/(math.cos(A/H)))
-- Sum of the radii
local sumOfRadii = (self.radius*scale) + (dv.radius*scale)
local levelOfPenetration = H - sumOfRadii -- Level of penetration (-ve if collision)
local forceOfHit = (math.max(self.xSpeed, self.ySpeed) - math.max(dv.xSpeed, dv.ySpeed))
if levelOfPenetration < 0 and forceOfHit > 0 then
-- super basic ejection
if (self.x > dv.x) then
self.x = self.x - levelOfPenetration
else
self.x = self.x + levelOfPenetration
end
if (self.y > dv.y) then
self.y = self.y - levelOfPenetration
else
self.y = self.y + levelOfPenetration
end
-- Apply base speed modifications
local temp = self.xSpeed
if (dv.xSpeed > 0) then
self.xSpeed = self.xSpeed - dv.xSpeed*bounciness
else
self.xSpeed = self.xSpeed + dv.xSpeed*bounciness
end
if (temp > 0) then
dv.xSpeed = dv.xSpeed + temp*bounciness
else
dv.xSpeed = dv.xSpeed - temp*bounciness
end
temp = self.ySpeed
if (dv.ySpeed > 0) then
self.ySpeed = self.ySpeed - dv.ySpeed*bounciness
else
self.ySpeed = self.ySpeed + dv.ySpeed*bounciness
end
if (temp > 0) then
dv.ySpeed = dv.ySpeed + temp*bounciness
else
dv.ySpeed = dv.ySpeed - temp*bounciness
end
--TODO
-- Angle of rebound
-- Ejection
end
end
end