Physics Friction

I don’t understand why this won’t work.
Player.friction=.9
It’s not applying friction.

Did you make physics.body and give it gravity?

Yes

@EpicPotato I tried friction on a test program and it didn’t seem to work. I tried linearDamping instead and that worked to slow an object down. gravityScale should be 0

What’s linearDamping?

I figured it out.

A ball rolling on a flat surface slows down because of friction. linearDamping seems to do the same thing. In my test program with 2 balls colliding, if I put linearDamping on them, the amount of time they keep moving before they stop depends on the value of linearDamping. I thought friction would do the same thing, but it doesn’t seem to.

Consider that friction is what makes a ball roll on the ground; without it it would slide. Other than that, friction’s effect on a rolling ball’s velocity are minimal. So yeah, for a ball, friction won’t make it slow down (or at least, it will slow down very slowly).

@toadkick Is that the purpose of friction, so a ball rolls instead of sliding. That would explain why friction doesn’t seem to work on slowing down 2 objects that collide, but linearDamping does.

Well, I don’t know if that’s the purpose, but it’s certainly the effect

The purpose of linear damping however is exactly what you guys are using it for, allowing more control over your bodies’ linear velocity.

EDIT: it occurs to me that what you probably want to do is set the angularDamping instead of the linearDamping. The angular damping will slow the rotation, without directly affecting the velocity, and is probably closer to what you are looking for.

@toadkick I created a small program that had an incline and a box. If I set friction to 0 and dropped the box on the incline, the box just slid down the incline. If I increased friction to 1, the box still slid down the incline, but slower. If I increased it to 3, the box tumbled down the incline without sliding at all. Thanks for the explanation of friction, that will come in handy.

As far as I know along as the block has mass and there is friction too, but the coefficient off of friction is lesser the tangent of angle of incline, the block will slide down the plane until a point tumbling starts ofcourse. This confuses me do we set the coefficient of friction while setting body.friction or the value of kinetic friction?

@Saurabh: from the Box2D reference (http://www.box2d.org/manual.html):

"Friction is used to make objects slide along each other realistically. Box2D supports static and dynamic friction, but uses the same parameter for both. Friction is simulated accurately in Box2D and the friction strength is proportional to the normal force (this is called Coulomb friction). The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong. When the friction force is computed between two shapes, Box2D must combine the friction parameters of the two parent fixtures. This is done with the geometric mean:

``````
float32 friction;

friction = sqrtf(shape1->friction * shape2->friction);
``````

So if one fixture has zero friction then the contact will have zero friction."

@toadkick What’s going wrong in here:

``````
--# Main
function setup()
block = physics.body(POLYGON, vec2(0, 0), vec2(50, 0), vec2(50, 50), vec2(0, 50))
F1 = physics.body(EDGE, vec2(0, 3*WIDTH/4), vec2(3*WIDTH/4, 0))

block.friction = 1
block.angle = 45
F1.friction = 1

block.x, block.y = 25, 3*WIDTH/4

print("A = angle of incline")
print("N = normal force")
print("f = friction")
print("M = mass")
print("U = coeff of friction")
print("a = acceleration")
print("")
print("N = MgcosA")
print("U = (1*1)^1/2 = 1")
print("f = UN")
print(":- f = MgcosA = MgsinA ... A = pi/4")
print("MgsinA - f = Ma")
print(":- a = 0")
print("is this right?")
end

function draw()
background()

pushMatrix()
fill(0, 171, 255, 255)
translate(block.x, block.y)
rotate(block.angle)
rect(0, 0, 50, 50)
popMatrix()

strokeWidth(3)
line(3*WIDTH/4, 0, 0, 3*WIDTH/4)
end

function collide(contact)
if contact.state == BEGAN then
print("")
print("velocity when contact began")
print(block.linearVelocity:len())
elseif contact.state == ENDED then
print("velocity when contact ended")
print(block.linearVelocity:len())
end
end
``````

@Saurabh: I’m not sure what you’re asking. If your asking what’s wrong with your math, then I’m not sure I can help you there. There’s got to be some other information missing from the equation, namely, what does Box2D do exactly with the mixed friction. You can probably look at the source and work that out, or ask someone like @Andrew_Stacey

As for what happens when I run the program…at first glance, the behavior did not seem unexpected. So, I tried increasing the block’s friction by 1. Each time the friction was noticeably greater, and by 5, the block began to tumble end over end. So far, so good. Then I figured, given the formula above for how Box2D does it’s “friction mixing”, setting the ground to 5 and the block to 1 should have the same effect. Except…it didn’t. The box slid down the incline just like it did when both were set to 1. So, I set the box to 10, and the ground to 0, and the box tumbled, incorrectly, because there should have been no friction at all.

So, two things:

1. Given Box2D friction mixing formula, despite the fact that the Box2D manual suggests friction values between 0 and 1, you need to use higher values than that if you really want a lot of friction.
2. There is a bug, either with Box2D, or Codea. It seems like maybe edge bodies (which are static) always have a friction value of 1, ignoring any other value you set it to. At the moment I’m not sure if this is true of all static bodies, or just edges…more experimentation is needed.

The bug is a bit of a bummer. If I were making a physics game where I needed to tune friction values between an object and it’s environment, I’d prefer to set the object’s friction to a constant value, and “paint” my enviroment with more/less friction as desired (for example, ice would have low friction, while grass or rubber would have higher friction values). Unfortunately at the moment it looks like things like that aren’t possible

From the mechanics I’ve learned yet, i believe that the net force on the body should have been zero when both the friction are set to one and angle of inclination is pi/4, so the initial speed should have been equal to the final speed, since acceleration is zero. Any one who can explain? And friction should be a dependent on the two surfaces, so it should make a difference what we set the friction for the two surfaces individually. But as toadkick said it completely depends on the value of friction coeff of the body itself.

I’ve added this bug to the issue tracker: https://bitbucket.org/TwoLivesLeft/core/issue/248/friction-property-ignored-on-some-physics

The best way of knowing what is going on is to look at the source code. But I guess we don’t have that option.

@dave1707: Box2D is open source, so if you can read c++ then you can see how it handles friction. I’m fairly sure the bug were seeing is with Codea, not Box2D.

well I cant read any computer languages beyond that of codea. I have had negligible experience in them. only 1 year that too in 7 grade. so that diesnt seem to be an option