Gugis Posted November 1, 2013 Share Posted November 1, 2013 Hello.I'm working on top-down 8 ball pool game. Box2D world has 0 gravity and when I use ApplyImpulse on ball it won't slow down and never stops. My question is how to apply friction for top down game?You can test it here: http://impressive.lt/test/box2d/8ball.htmlSource code:<html> <head> <title>8ball</title> </head> <body onload="init();"> <canvas id="canvas" width="600" height="400" style="background-color:#333333;" ></canvas> </body> <script type="text/javascript" src="Box2dWeb-2.1.a.3.min.js"></script> <script type="text/javascript"> function init() { var b2Vec2 = Box2D.Common.Math.b2Vec2 , b2AABB = Box2D.Collision.b2AABB , b2BodyDef = Box2D.Dynamics.b2BodyDef , b2Body = Box2D.Dynamics.b2Body , b2FixtureDef = Box2D.Dynamics.b2FixtureDef , b2Fixture = Box2D.Dynamics.b2Fixture , b2World = Box2D.Dynamics.b2World , b2MassData = Box2D.Collision.Shapes.b2MassData , b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape , b2CircleShape = Box2D.Collision.Shapes.b2CircleShape , b2DebugDraw = Box2D.Dynamics.b2DebugDraw , b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef ; var world = new b2World( new b2Vec2(0, 0) //gravity , true //allow sleep ); function borders(){ var borderFix = new b2FixtureDef; borderFix.density = 1.0; borderFix.friction = 0.5; borderFix.restitution = 0.2; borderFix.shape = new b2PolygonShape; borderFix.shape.SetAsBox(20, 2); var borderBody = new b2BodyDef; borderBody.type = b2Body.b2_staticBody; borderBody.position.Set(10, 400 / 30 + 1.8); world.CreateBody(borderBody).CreateFixture(borderFix); borderBody.position.Set(10, -1.8); world.CreateBody(borderBody).CreateFixture(borderFix); borderFix.shape.SetAsBox(2, 14); borderBody.position.Set(-1.8, 13); world.CreateBody(borderBody).CreateFixture(borderFix); borderBody.position.Set(21.8, 13); world.CreateBody(borderBody).CreateFixture(borderFix); } function balls(){ var cue, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15; var ballFix = new b2FixtureDef; ballFix.density = 1.0; ballFix.friction = 0.5; ballFix.restitution = 0.2; var ballBody = new b2BodyDef; ballBody.type = b2Body.b2_dynamicBody; ballFix.shape = new b2CircleShape(0.4); ballBody.position.x = Math.random() * 10; ballBody.position.y = Math.random() * 10; var ball = world.CreateBody(ballBody).CreateFixture(ballFix); ball.GetBody().ApplyImpulse(new b2Vec2(1,0), new b2Vec2(0,0)); } var debugDraw = new b2DebugDraw(); debugDraw.SetSprite(document.getElementById("canvas").getContext("2d")); debugDraw.SetDrawScale(30.0); debugDraw.SetFillAlpha(0.5); debugDraw.SetLineThickness(1.0); debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); world.SetDebugDraw(debugDraw); window.setInterval(update, 1000 / 60); function update() { world.Step(1 / 60, 10, 10); world.DrawDebugData(); world.ClearForces(); }; borders(); balls(); }; </script> </html> Quote Link to comment Share on other sites More sharing options...
Psychho Posted November 1, 2013 Share Posted November 1, 2013 Set linear damping in your pool balls bodyDef. Change to this var ballBody = new b2BodyDef;ballBody.type = b2Body.b2_dynamicBody;ballBody.linearDamping = 0.3;Also if you want you can also set angularDamping to make the balls stop spinning. Gugis 1 Quote Link to comment Share on other sites More sharing options...
Gugis Posted November 1, 2013 Author Share Posted November 1, 2013 Set linear damping in your pool balls bodyDef. Change to this var ballBody = new b2BodyDef;ballBody.type = b2Body.b2_dynamicBody;ballBody.linearDamping = 0.3;Also if you want you can also set angularDamping to make the balls stop spinning.Thanks, it looks much better now /m\ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.