• Advertisement
Sign in to follow this  

Why is my Python SAT implementation breaking?

This topic is 2183 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*
[code]
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
[/code]

*collision resolution*
[code]
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:
pass

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
else:
object1.pos.y += y_intersect
[/code]

Share this post


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

  • Advertisement