Jump to content
  • Advertisement
Sign in to follow this  

Why is my Python SAT implementation breaking?

This topic is 2823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a simple platformer in Python and pyglet to learn the language. I've copied some collision detection/resolution code from the XNA Platformer Starter Kit and translated it into Python. However, when I run the code, the player vibrates, teleports, and the physics is just generally quirky. When I comment out the collision code, however, the physics generally behaves as expected (but the player falls through the floor).

This is my code:

*game loop*

player = objects.Player()
platform = objects.Platform(100, 0, 500, 50)
platform2 = objects.Platform(0, -50, 800, 50)


def update(dt):
player.vel.x += player.force.x - (FRICTION * player.vel.x)
player.vel.y -= GRAVITY + (DRAG * player.vel.y)

player.prevpos.x = player.pos.x
player.prevpos.y = player.pos.y

player.pos.x += player.vel.x;
player.pos.y += player.vel.y;

physics.resolve_collision(player, platform)
physics.resolve_collision(player, platform2)

if player.pos.x == player.prevpos.x:
player.vel.x = 0

if player.pos.y == player.prevpos.y:
player.vel.y = 0

*collision resolution*

def resolve_collision(object1, object2):
hw1 = object1.size.x / 2
hw2 = object2.size.x / 2
hh1 = object1.size.y / 2
hh2 = object2.size.y / 2

center1 = objects.Vector(object1.pos.x + hw1, object1.pos.y + hh1)
center2 = objects.Vector(object2.pos.x + hw2, object2.pos.y + hh2)

x_distance = center1.x - center2.x
y_distance = center1.y - center2.y

hwsum = hw1 + hw2
hhsum = hh1 + hh2

if abs(x_distance) >= hwsum or abs(y_distance) >= hhsum:

x_intersect = hwsum - x_distance if x_distance > 0 else -hwsum - x_distance
y_intersect = hhsum - y_distance if y_distance > 0 else -hhsum - y_distance

if abs(x_intersect) < abs(y_intersect):
object1.pos.x += x_intersect
object1.pos.y += y_intersect

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!