🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

SAT not working properly

Started by
1 comment, last by Nypyren 6 years, 6 months ago

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]
Advertisement

What have you tried?  Did you use a debugger to investigate what the values are which lead to the problem?  It should be pretty easy to identify the issue if you have a repro case.

This topic is closed to new replies.

Advertisement