Kon
Kon

Reputation: 43

box2dweb: Walls don't bounce a slow object

I use box2dweb for my game. I located four boxes around a circle and gave the circle a initial velocity. I expected that the circle would bounces off the wall but the circle moves parallel with the wall after a collision. It happens only when a circle moves slow. A circle always stops on a wall in the end. How can I make a circle behave correctly? The below script shows the problem.

<!DOCTYPE HTML>
<html lang="ja-JP">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://jsdo.it/Kon/box2dweb/js"></script>
    <script type="text/javascript">

var Ball_Speed = 2.0;

var b2Vec2 = Box2D.Common.Math.b2Vec2;
var b2Body = Box2D.Dynamics.b2Body;
var b2World = Box2D.Dynamics.b2World;
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;

$(function(){    
    "use strict";
    var canvas = $("#canvas");
    canvas.css("width", 500);
    canvas.css("height", 500);
    canvas.get(0).width  = 500;
    canvas.get(0).height = 500;

    var world = new b2World(new b2Vec2(0, 0), true);

    var debugDraw = new b2DebugDraw();
    debugDraw.SetSprite(canvas.get(0).getContext("2d"));
    debugDraw.SetDrawScale(100);
    debugDraw.SetFillAlpha(0.9);
    debugDraw.SetLineThickness(1.0);
    debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
    world.SetDebugDraw(debugDraw);

    var fixDef = new Box2D.Dynamics.b2FixtureDef;
    fixDef.restitution = 1;

    var bodyDef = new Box2D.Dynamics.b2BodyDef;
    bodyDef.linearDamping = 0.2;

    // Create Wall //////////////////////////////////////////////////////////////////
    function createWall(x, y, w, h){
        bodyDef.type = b2Body.b2_staticBody;
        bodyDef.position.x = x + w / 2;
        bodyDef.position.y = y + h / 2;
        fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
        fixDef.shape.SetAsBox(w / 2, h / 2);
        world.CreateBody(bodyDef).CreateFixture(fixDef);
    }    
    createWall(0, 0, 2, 0.1);
    createWall(0, 0, 0.1, 2);
    createWall(2, 0, 0.1, 2);
    createWall(0, 2, 2, 0.1);

    // Create Ball
    fixDef.shape = new b2CircleShape(0.2);
    bodyDef.type = b2Body.b2_dynamicBody;
    bodyDef.position = new b2Vec2(1.5, 1.0);
    bodyDef.linearVelocity = new b2Vec2(Ball_Speed, Ball_Speed);
    world.CreateBody(bodyDef).CreateFixture(fixDef);

    (function loop(){
        world.Step(1 / 60, 10, 10);
        world.DrawDebugData();
        world.ClearForces();
        setTimeout(loop, 15);
    })();
});    

    </script>
</head>
<body>
    <canvas id="canvas"></canvas>
</body>
</html>

Upvotes: 4

Views: 589

Answers (1)

iforce2d
iforce2d

Reputation: 8262

Try changing the value of Box2D.Common.b2Settings.b2_velocityThreshold to zero.

http://www.iforce2d.net/b2dtut/gotchas#slownorebound

Upvotes: 2

Related Questions