Jump to content
  • Advertisement
Bob Dylan

SAT not working properly

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!