Objects sticking to the walls

Hi!

I'm playing around with some box2d in flash and I thought I'd create a simple demo of balls bouncing off walls. I was imagining a kind of top down view of a pool table. My idea was to set the gravity to (0,0), the restitution of all the objects (including the static walls) to 1.0 and the friction to 0. However, when I do this and the balls hit the wall with a pretty low velocity, they stick to the wall and start to slide along them instead of bouncing off.

Here's a link to the demo: http://www.d.kth.se/~bruse/flash/stick.html

Does anyone know what this behaviour comes from and how to prevent it?

Here's the source code for it as well. It's basicly a mix between the hello world project and the video tutorials from Todd.

Code:
package{
   import flash.display.Sprite;
   import flash.events.Event;
   // Classes used in this example
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;   

   public class HelloWorld extends Sprite{
      private var world:b2World;
      private var RATIO:Number = 30;

      private function rnd(low:Number, high:Number):Number {
         return low+Math.random() * (high-low+1);
      }

      private function initB2World():void {
         var worldAABB:b2AABB = new b2AABB();
         worldAABB.lowerBound.Set(-3000.0/RATIO, -3000.0/RATIO);
         worldAABB.upperBound.Set(3000.0/RATIO, 3000.0/RATIO);

         var gravity:b2Vec2 = new b2Vec2(0,0);
         var doSleep:Boolean = false;

         world = new b2World(worldAABB, gravity, doSleep);
      }

      private function buildWalls():void {

         var hShape:b2PolygonDef = new b2PolygonDef();
         
         hShape.SetAsBox(stage.stageWidth/2/RATIO, 5/RATIO);

         hShape.friction = 0;
         hShape.restitution = 1.0;
         hShape.density = 0.0;

         var floorBodyDef:b2BodyDef = new b2BodyDef();
         floorBodyDef.position.Set(stage.stageWidth/2/RATIO,
               (stage.stageHeight-5)/RATIO);

         var floorBody:b2Body = world.CreateBody(floorBodyDef);
         floorBody.CreateShape(hShape);
         floorBody.SetMassFromShapes();

         var roofBodyDef:b2BodyDef = new b2BodyDef();
         roofBodyDef.position.Set(stage.stageWidth/2/RATIO,
               5/RATIO);

         var roofBody:b2Body = world.CreateBody(roofBodyDef);
         roofBody.CreateShape(hShape);
         roofBody.SetMassFromShapes();

         var vShape:b2PolygonDef = new b2PolygonDef();
         vShape.SetAsBox(5/RATIO, stage.stageHeight/2/RATIO);

         hShape.friction = 0;
         hShape.restitution = 1.0;
         hShape.density = 0.0;

         var wallBodyDef:b2BodyDef = new b2BodyDef();
         wallBodyDef.position.Set(5/RATIO, stage.stageHeight/2/RATIO);
         
         var lWallBody:b2Body = world.CreateBody(wallBodyDef);
         lWallBody.CreateShape(vShape);
         lWallBody.SetMassFromShapes();

         wallBodyDef.position.Set((stage.stageWidth-5)/RATIO,
               stage.stageHeight/2/RATIO);

         var rWallBody:b2Body = world.CreateBody(wallBodyDef);
         rWallBody.CreateShape(vShape);
         rWallBody.SetMassFromShapes();
      }
      
      public function HelloWorld(){
         initB2World();
         buildWalls();         

         // Add event for main loop
         addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
         
         // set debug draw
         var dbgDraw:b2DebugDraw = new b2DebugDraw();
         var dbgSprite:Sprite = new Sprite();
         addChild(dbgSprite);
         dbgDraw.m_sprite = dbgSprite;
         dbgDraw.m_drawScale = 30.0;
         dbgDraw.m_fillAlpha = 0.0;
         dbgDraw.m_lineThickness = 1.0;
         dbgDraw.m_drawFlags = 0xFFFFFFFF;
         world.SetDebugDraw(dbgDraw);
         
         
         
         // Vars used to create bodies
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;

         // Add some objects
         for (var i:int = 1; i < 10; i++){
            bodyDef = new b2BodyDef();
            bodyDef.position.x = Math.random() * 15 + 5;
            bodyDef.position.y = Math.random() * 10;
            var rX:Number = 20/RATIO;
            var rY:Number = 20/RATIO;

            circleDef = new b2CircleDef();
            circleDef.radius = rX;
            circleDef.density = 1.0;
            circleDef.friction = 0.0;
            circleDef.restitution = 1.0
            bodyDef.userData = new Sprite();
            bodyDef.userData.graphics.beginFill(0xFF0000);
            bodyDef.userData.graphics.drawCircle(0,0,20);
            bodyDef.userData.width = rX * 2 * 30;
            bodyDef.userData.height = rX * 2 * 30;
            body = world.CreateBody(bodyDef);
            body.CreateShape(circleDef);
            body.SetMassFromShapes();
            body.SetLinearVelocity(new b2Vec2(1,1));
            addChild(bodyDef.userData);
         }

      }

      public function Update(e:Event):void{

         world.Step(m_timeStep, m_iterations);

         // Go through body list and update sprite positions/rotations
         for (var bb:b2Body = world.m_bodyList; bb; bb = bb.m_next){
            if (bb.m_userData is Sprite){
               bb.m_userData.x = bb.GetPosition().x * 30;
               bb.m_userData.y = bb.GetPosition().y * 30;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
            }
         }
         
      }
      
      public var m_iterations:int = 10;
      public var m_timeStep:Number = 1.0/30.0;
      
   }

}
Have you found a solution to this yet? We're currently having the same problems in a Top-Down game.
Also the balls tend to stick to static balls (it's not a billard game 🙂 )

The balls stick, if they hit the surface very flat. They seem to pick up rotational energy.
No I have not. Right now I'm using an ugly hack where I make sure every ball has a force acting on it that points away from the nearest wall. It works, but it's not a pretty solution.
Look in b2Settings - one of the parameters there determines when bounces are ignored. Set it to zero for billiards.
interesting. i suppose that setting prevents some jittery-ness?

i have a somewhat similar question

ASCII drawing of the situation

______________/ /___
_____________/ /____
---------------O/ /
---------------/ /
--------------/ /

Controlling a ball as the hero of a game, using Left and Right arrows to apply Torque, and also use a small amount of linear force to avoid getting stuck in most situations.

when i have two rectangles crossing at an angle like shown above, as soon as the top of the ball touches that ceiling block, the ball seems to get wedged and will not respond to key controls anymore.

I have considered trying to detect if a key is being pressed but the ball is not moving, and then applying a special effect for 1 or 2 frames to get unstuck, like lower friction, or maybe even higher restitution.

This ball uses 0 restitution and 100 friction to allow it to climb walls.

anyone have a simpler or cleaner way to either detect that it is stuck, or get it unstuck.
BorisTheBrave wrote:
Look in b2Settings - one of the parameters there determines when bounces are ignored. Set it to zero for billiards.


Thanks!

Setting b2_velocityThreshold to 0 fixed it.

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