View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Why is my Python SAT implementation breaking?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

### #1UnknownX  Members

Posted 29 January 2012 - 05:52 PM

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:
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


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.