Here is my implementation of SAT, it does not work properly as the title says, in that it quite frequently registers a collision when there is not one, can you check and see if there is anything blatantly wrong with the code? Thanks. I have my own custom vector class, and poly class.
normals = self.normals
isCollision = []
MTVs = []
for i in other.normals:
normals.append(i)
for j in normals:
selfProj = []
otherProj = []
for k in self.vertices:
selfProj.append(j * k)
for l in other.vertices:
otherProj.append(j * l)
minS = min(selfProj)
maxS = max(selfProj)
minO = min(otherProj)
maxO = max(otherProj)
if (minS > maxO or minO > maxS):
"""No collision on THIS axis"""
isCollision.append(False)
else:
isCollision.append(True)
MTVmag = min(maxO - minS, maxS - minO)
MTV = j * MTVmag
MTVs.append(MTV)
if (all(x == True for x in isCollision)):
vectorMags = []
for m in MTVs:
vectorMags.append(m.magSquared)
actMTV = MTVs[vectorMags.index(min(vectorMags))]
"""Finds the MTV with the smallest magnitude"""
indexOfNorm = MTVs.index(actMTV)
CN = normals[indexOfNorm]
"""CN = Collision Normal
RETURN [CN, 0] for now, the 0 is a placeholder for the collision point"""
if(actMTV.y >= 0 and other.pos.y > self.pos.y):
self.translate(actMTV * -0.4)
other.translate(actMTV * 0.4)
return [CN, 0]
elif (actMTV.y >= 0 and other.pos.y < self.pos.y):
self.translate(actMTV * 0.4)
other.translate(actMTV * -0.4)
return [CN, 0]
elif(actMTV.y <= 0 and other.pos.y > self.pos.y):
self.translate(actMTV * 0.4)
other.translate(actMTV * -0.4)
return [CN, 0]
elif (actMTV.y <= 0 and other.pos.y < self.pos.y):
self.translate(actMTV * -0.4)
other.translate(actMTV * 0.4)
return [CN, 0]