I'm trying to create a game where you can move the ball around and there will be friction in it but if I add friction then the ball stops moving horizontally when it hits the ground after it jumps high(probably because the normal force is high when the ball falls in a high speed so the friction is stronger). when I remove the friction(set it to 0) it is fine but I want the friction.
this is the code for creating the bodies for the ball and the floor:
BodyDef ballDef = new BodyDef();
ballDef.type = BodyType.DYNAMIC;
CircleShape golfShape = new CircleShape();
golfShape.setRadius(blockSize / 6.0f / jBox2DScale);
ballBody = world.createBody(ballDef);
ballBody.setFixedRotation(true);
FixtureDef ballFixture = new FixtureDef();
ballFixture.density = 1f;
//ballFixture.restitution = 0.9f;
ballFixture.friction = 0.1f;
ballFixture.shape = golfShape;
ballBody.createFixture(ballFixture);
ballBody.setTransform(new Vec2(width / 2.0f / jBox2DScale, height / 2.0f / jBox2DScale), 0.0f);
BodyDef blockDef = new BodyDef();
blockDef.type = BodyType.STATIC;
Body body = world.createBody(blockDef);
FixtureDef blockFixture = new FixtureDef();
blockFixture.density = 100;
blockFixture.restitution = 0.75f;
blockFixture.friction = 0.15f;
EdgeShape sideShape = new EdgeShape();
sideShape.set(new Vec2(0.0f, 0.0f), new Vec2(width, 0.0f));
sideShape.m_vertex0.set(-width, 0.0f);
sideShape.m_vertex3.set(width * 2, 0.0f);
sideShape.m_hasVertex0 = true;
sideShape.m_hasVertex3 = true;
sideShape.setRadius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(width, 0.0f), new Vec2(width, height));
sideShape.m_vertex0.set(width, -height);
sideShape.m_vertex3.set(width, height * 2);
sideShape.m_hasVertex0 = true;
sideShape.m_hasVertex3 = true;
sideShape.setRadius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(width, height), new Vec2(0, height));
sideShape.m_vertex0.set(width * 2, height);
sideShape.m_vertex3.set(-width, height);
sideShape.m_hasVertex0 = true;
sideShape.m_hasVertex3 = true;
sideShape.setRadius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(0.0f, height), new Vec2(0.0f, 0.0f));
sideShape.m_vertex0.set(0.0f, height * 2);
sideShape.m_vertex3.set(0.0f, -height);
sideShape.m_hasVertex0 = true;
sideShape.m_hasVertex3 = true;
sideShape.setRadius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
body.setTransform(new Vec2(x, y), 0.0f);
Image of what happens:
Does anyone have an idea on how to solve it?
(Edit) I removed the fixed rotation on the ball and added angular dampening which I think is the best easy way to fix it.
you can fix this behaviour by
ballBody.setFixedRotation(false);
or by removing the statement altogether.