Not sure this would be helpful but here's what I have:
Code: Select all
function Entity:resolveCollision(col, notDo)
self:changeVelocityByCollisionNormal(col.normal.x, col.normal.y, nil, col.other)
if col.normal.y == -1 then
if not self.isMini and (not self.isPlayer or (self.isPlayer and self.vy>100)) and not self.isDebris then
--causes puff on land
if not self.onGround then
if not self.noPuff then--self.vy<-15 then
self:createPuff(3,5)
end
self:playSound("punch1")--getValue({"punch1","punch2","punch3"}))
end
self.gravity = 0
self.vy = 0
self.onGround = true
end
else
--self.onGround = false
end
if notDo then self:changeVelocityByImpulse(col) end
end
function Entity:changeVelocityByImpulse(col)
local other = col.other
if not other.isTile then--
--push
local xx, yy = self.vx, other.vx
local y,x = col.normal.y, col.normal.x
if y<0 and self.vy>other.vy then
other:applyImpulseY(self.vy)
elseif y<0 and self.vy<other.vy then
self:applyImpulseY(other.vy)
end
if y>0 and self.vy<other.vy then
other:applyImpulseY(self.vy)
elseif y>0 and self.vy>other.vy then
self:applyImpulseY(other.vy)
end
if x<0 and self.vx>other.vx then
other:applyImpulseX(self.vx)
elseif x<0 and self.vx<other.vx then
self:applyImpulseX(other.vx)
end
if x>0 and self.vx<other.vx then
other:applyImpulseX(self.vx)
elseif x>0 and self.vx>other.vx then
self:applyImpulseX(other.vx)
end
end
function Entity:changeVelocityByCollisionNormal(nx, ny, bounciness, other)
local bounciness = bounciness or self.elasticity
if other.isCrate or (other.isBlock and other.dynamic)
or self.isBullet or not bounciness or bounciness == 0 then
return
end
local vx, vy = self.vx, self.vy
if (nx < 0 and vx > 0) or (nx > 0 and vx < 0) then
vx = -vx * bounciness
self.bounced.x = true
end
if (ny < 0 and vy > 0) or (ny > 0 and vy < 0) then
vy = -vy * bounciness
self.bounced.y = true
end
self.vx, self.vy = vx, vy
end
function Entity:move(x,y)
local actualX, actualY, collisions, len = self.world:move(self,x,y,
self.checkCollision)
local col
for i = 1, len do
col = collisions[i]
self:resolveCollision(col)
end
return actualX, actualY
end