Bouncy creep

Hello. I'm new to Box2D (using pybox2d), and let me first say how impressed I am with how wonderful this engine is.

Here's what is probably a newbie question. I'm dropping square boxes, oriented in various ways, on a flat surface. Once in a while, but not so rarely, I get the following behavior: the box bounces/spins around a few times, and then settles down... but continues to make tiny little regular bounces, and sort of creeps along the flat surface, slowly but regularly. I thought that damping would be the way to go, but this happens for various values of damping, too. I ran into this problem quite quickly, so I'm sure others have seen it, too. I really have the feeling I'm just doing something wrong, but can't find what it is.

Here are some parameters that reproduce the problem.
Box length: 1.3 [i.e., SetAsBox(1.3/2, 1.3/2)]
Initial height: 14.15 above flat surface
Initial angle: 1.1 deg
Density: 1
Friction: 0.2
Restitution: 0.66
Time step, velocity iterations, position iterations: 1/60, 10, 8
... and everything else set to default values.
Oh, and I'm using pybox2d ver. 2.0.2b1.

Here's a Python (2.6) program that reproduces this behavior:

Code:
from __future__ import print_function
import Box2D as box2d
import pygame, math
from pygame.locals import *
from OpenGL.GL import *

screen_width, screen_height = 29.0, 16.3
objS = 1.3
gdW, gdH = screen_width, 1.0
gdY = -screen_height/2 - gdH/2

worldAABB = box2d.b2AABB()
worldAABB.lowerBound = (-20, -10)
worldAABB.upperBound = (+20, +10)
world = box2d.b2World(worldAABB, (0, -50), True)

groundBodyDef = box2d.b2BodyDef()
groundBodyDef.position = (0, gdY)
groundBody = world.CreateBody(groundBodyDef)
groundShapeDef = box2d.b2PolygonDef()
groundShapeDef.SetAsBox(gdW/2, gdH/2)
groundBody.CreateShape(groundShapeDef)

bodyDef = box2d.b2BodyDef()
bodyDef.position = (0, 6)
bodyDef.angle = math.radians(1.1)
#bodyDef.angularDamping = 0
body = world.CreateBody(bodyDef)
shapeDef = box2d.b2PolygonDef()
shapeDef.SetAsBox(objS/2, objS/2)
shapeDef.density = 1
shapeDef.friction = 0.2
shapeDef.restitution = 0.66
body.CreateShape(shapeDef)
body.SetMassFromShapes()

try:
    pygame.init()
    screen = pygame.display.set_mode((0,0), OPENGL|DOUBLEBUF|FULLSCREEN|HWSURFACE)
    glMatrixMode(GL_PROJECTION)
    glOrtho(-screen_width/2, +screen_width/2, -screen_height/2, +screen_height/2, -10, +10)
    glMatrixMode(GL_MODELVIEW)

    #out = open('traj.dat', 'wt')
    keep_going = True
    while keep_going:
        world.Step(1.0/60, 10, 8)
        pos = body.GetPosition()
        ang = body.GetAngle()
        #print(pos.x, pos.y, math.degrees(ang), sep = '\t', file = out)
        glClear(GL_COLOR_BUFFER_BIT)
        glPushMatrix()
        glTranslated(pos.x, pos.y, 0)
        glRotated(math.degrees(ang), 0, 0, 1)
        glRectd(-objS/2, -objS/2, +objS/2, +objS/2)
        glPopMatrix()
        pygame.display.flip()
        for ev in pygame.event.get():
            if ev.type == KEYDOWN and ev.key == K_ESCAPE:
                keep_going = False
                break

finally:
    #out.close()
    pygame.quit()


I'm also attaching two graphs, showing the x coordinate as a function of frame (the creep is the slow, linear rise after frame 200), as well as the angle as a function of time, which shows the small period cycling.

Thanks a lot for any help on this.

Mark Wexler
OK, I get it. I had made gravity too high (-50).

So now I make gravity lower (-5), but I get a different problem: one side of an object sometimes 'sinks' into a solid surface underneath. Is there any way to fix that? Or should I basically always put gravity to -10?

Mark Wexler
Box2D is tuned mainly for the case of restitution = 0. You may have to play with the velocity threshold in b2Setting.h.
In case anyone ever runs into the problem I described several months ago, I solved it and kept the restitution (thank you, Erin, for your reply, however), by dividing all lengths by 10. (Naïvely, I had used lengths like 10 or 20, because I wanted to reproduce my screen's physical size in centimeters.) Everything works fine now, no more bouncy creep. Here's the new code:

Code:
import Box2D as box2d
import pygame, math
from pygame.locals import *
from OpenGL.GL import *

screen_width, screen_height = 2.90, 1.63
objS = 0.13
gdW, gdH = screen_width, 0.3
gdY = -gdH/2

worldAABB = box2d.b2AABB()
worldAABB.lowerBound = (-2, -1)
worldAABB.upperBound = (+2, +2)
world = box2d.b2World(worldAABB, (0, -5), True)

groundBodyDef = box2d.b2BodyDef()
groundBodyDef.position = (0, gdY)
groundBody = world.CreateBody(groundBodyDef)
groundShapeDef = box2d.b2PolygonDef()
groundShapeDef.SetAsBox(gdW/2, gdH/2)
groundBody.CreateShape(groundShapeDef)

bodyDef = box2d.b2BodyDef()
bodyDef.position = (0, 0.9*screen_height)
bodyDef.angle = math.radians(40)
bodyDef.angularDamping = 0
body = world.CreateBody(bodyDef)
shapeDef = box2d.b2PolygonDef()
shapeDef.SetAsBox(objS/2, objS/2)
shapeDef.density = 1
shapeDef.friction = 0.2
shapeDef.restitution = 0.66
body.CreateShape(shapeDef)
body.SetMassFromShapes()

try:
    pygame.init()
    screen = pygame.display.set_mode((0,0), OPENGL|DOUBLEBUF|FULLSCREEN|HWSURFACE)
    glMatrixMode(GL_PROJECTION)
    glOrtho(-screen_width/2, +screen_width/2, 0, screen_height, -10, +10)
    glMatrixMode(GL_MODELVIEW)

    keep_going = True
    while keep_going:
        world.Step(1.0/60, 20, 20)
        pos = body.GetPosition()
        ang = body.GetAngle()
        glClear(GL_COLOR_BUFFER_BIT)
        glPushMatrix()
        glTranslated(pos.x, pos.y, 0)
        glRotated(math.degrees(ang), 0, 0, 1)
        glRectd(-objS/2, -objS/2, +objS/2, +objS/2)
        glPopMatrix()
        pygame.display.flip()
        for ev in pygame.event.get():
            if ev.type == KEYDOWN and ev.key == K_ESCAPE:
                keep_going = False
                break

finally:
    pygame.quit()

Original thread: https://box2d.org/forum/viewtopic.php?t=3855