Hey all!
I'm writing a simulator for a top-down world for a robot to interact with obstacles, and i'm stuck on a key feature. Box2D has fantastic support for everything i need EXCEPT static friction for the obstacles.
here's the problem:
My world is top-down, so gravity is off. I can use linear and angular damping to simulate kinetic friction (like in the car demos), but in my world I also need (anisotropic) static friction - the robot has to push on wheeled carts and tables, which resist forces depending on their orientation.
I've attempted to solve this problem by writing an "applyForceConstraint(point, force)" function which does the following:
1) project the force onto the perpendicular axes of the body
2) threshold each axial force vector based on body mass and appropriate friction coefficient
3) add an opposite impulse at the provided point if the threshold isn't met
I then call this function in my collision listener, and during the main runtime loop for each joint which can exert force on the body.
Unfortunately, this solution doesn't work very well.
For collisions, the object moves and then bounces back (which pushes the incident object away). This effect looks ridiculous when I drag objects into the constrained body with the mouse - the constrained body ends up jackhammering against the dragged body. Worse, if the object i'm dragging is massive enough, it somehow overwhelms my constraint and the object moves anyway (not a threshold problem).
Furthermore, when I try to drag the body with the mousejoint directly i also get a weird effect, in which the object pulls away from it's starting location when i drag, and then bounces back when i let go. It also slowly rotates in place to a fixed orientation, which I've noticed depends on where I click.
* This might be a separate issue, but from plotting the relevant vectors, i noticed that the force vector from the mouse joint's "GetReactionForce" doesn't always align with the vector between its anchor points. I spent a log time trying to track this down online and the Box2D source, but couldn't figure it out. Images attached (pink is GetReactionForce, and Blue is vector between anchor points)
This might be related too: I needed to multiply in an arbitrary constant to get the forces to cancel, which seems to depend on the density (mass). I thought that ApplyLinearImpulse accounted for mass though, so this is confusing as well.
So basically I'm stumped. 😐
My approach seems *qualitatively* right, but weird stuff happens with big objects, constantly applied forces, and mouse joints.
If anyone has any suggestions i'd be very grateful!
My implementation happens to be in python (latest from pybox2d SVN), and i've attached the relevant code snippits below.
Calling constraint for collisions:
def PostSolve(self, contact, impulse):
for fixture in [contact.fixtureA, contact.fixtureB]:
if fixture.body.userData and fixture.body.userData.has_key('sprite'):
sprite = fixture.body.userData['sprite']
if type(sprite) == ConstrainedSprite:
# extract collision force vector in world coords
normal = contact.worldManifold.normal # from fixtureA to fixtureB
if fixture == contact.fixtureA: normal = -1*normal # reverse for opposite fixture
for i in range(contact.manifold.pointCount):
point = b2Vec2(contact.worldManifold.points[i])
nMag = impulse.normalImpulses[i]
tMag = impulse.tangentImpulses[i]
R = b2Mat22(math.radians(-90))
sprite.applyForceConstraints(point, force = nMag*normal)
sprite.applyForceConstraints(point, force = tMag*(R*normal))
Calling constraint for mousejoint:
if self.mouseJoint:
body = self.mouseJoint.bodyB # bodyB is always the target object
if body.userData and body.userData.has_key('sprite') and type(body.userData['sprite']) == ConstrainedSprite:
pos = self.mouseJoint.anchorB # anchorB is always on the target body, in world coords
force = self.mouseJoint.GetReactionForce(1/self.TIME_STEP) # force exerted by the mousejoint on the body
body.userData['sprite'].applyForceConstraints(pos, force*self.TIME_STEP)
And here's the code for doing the canceling:
def applyForceConstraint(self, point, force = None, torque = None):
"""Applies the anisotropic force constraint according to the current constraint
parameters. Equivalent to static friction, and should be called whenever a force
is acting on the body (even if the body is in motion).
@param point A b2Vec2 point in world coordinates where the force is acting. Assumed
to be from a contact experienced by the body, or applied by a joint.
@param force A b2Vec2 describing a force in world coordinates
"""
if force:
gain = 1.33
# project force vector onto xNormal of constraint
worldNormal = self.body.GetWorldVector(self.xNormal)
xForce = Box2D.b2Dot(worldNormal, force) * worldNormal
if abs(xForce.length) < self.xMu * self.body.mass:
# if subthreshold, apply opposite force to cancel it out
self.body.ApplyLinearImpulse(-gain*xForce, point)
# project force vector to yNormal of constraint
worldNormal = self.body.GetWorldVector(self.yNormal)
yForce = Box2D.b2Dot(worldNormal, force) * worldNormal
if abs(yForce.length) < self.yMu * self.body.mass:
# if subthreshold, apply opposite force to cancel it out
self.body.ApplyLinearImpulse(-gain*yForce, point)
# debugging: just try to reverse the original force on the point
# self.body.ApplyLinearImpulse(-gain*force, point)
I'm writing a simulator for a top-down world for a robot to interact with obstacles, and i'm stuck on a key feature. Box2D has fantastic support for everything i need EXCEPT static friction for the obstacles.
here's the problem:
My world is top-down, so gravity is off. I can use linear and angular damping to simulate kinetic friction (like in the car demos), but in my world I also need (anisotropic) static friction - the robot has to push on wheeled carts and tables, which resist forces depending on their orientation.
I've attempted to solve this problem by writing an "applyForceConstraint(point, force)" function which does the following:
1) project the force onto the perpendicular axes of the body
2) threshold each axial force vector based on body mass and appropriate friction coefficient
3) add an opposite impulse at the provided point if the threshold isn't met
I then call this function in my collision listener, and during the main runtime loop for each joint which can exert force on the body.
Unfortunately, this solution doesn't work very well.
For collisions, the object moves and then bounces back (which pushes the incident object away). This effect looks ridiculous when I drag objects into the constrained body with the mouse - the constrained body ends up jackhammering against the dragged body. Worse, if the object i'm dragging is massive enough, it somehow overwhelms my constraint and the object moves anyway (not a threshold problem).
Furthermore, when I try to drag the body with the mousejoint directly i also get a weird effect, in which the object pulls away from it's starting location when i drag, and then bounces back when i let go. It also slowly rotates in place to a fixed orientation, which I've noticed depends on where I click.
* This might be a separate issue, but from plotting the relevant vectors, i noticed that the force vector from the mouse joint's "GetReactionForce" doesn't always align with the vector between its anchor points. I spent a log time trying to track this down online and the Box2D source, but couldn't figure it out. Images attached (pink is GetReactionForce, and Blue is vector between anchor points)
This might be related too: I needed to multiply in an arbitrary constant to get the forces to cancel, which seems to depend on the density (mass). I thought that ApplyLinearImpulse accounted for mass though, so this is confusing as well.
So basically I'm stumped. 😐
My approach seems *qualitatively* right, but weird stuff happens with big objects, constantly applied forces, and mouse joints.
If anyone has any suggestions i'd be very grateful!
My implementation happens to be in python (latest from pybox2d SVN), and i've attached the relevant code snippits below.
Calling constraint for collisions:
Code:
def PostSolve(self, contact, impulse):
for fixture in [contact.fixtureA, contact.fixtureB]:
if fixture.body.userData and fixture.body.userData.has_key('sprite'):
sprite = fixture.body.userData['sprite']
if type(sprite) == ConstrainedSprite:
# extract collision force vector in world coords
normal = contact.worldManifold.normal # from fixtureA to fixtureB
if fixture == contact.fixtureA: normal = -1*normal # reverse for opposite fixture
for i in range(contact.manifold.pointCount):
point = b2Vec2(contact.worldManifold.points[i])
nMag = impulse.normalImpulses[i]
tMag = impulse.tangentImpulses[i]
R = b2Mat22(math.radians(-90))
sprite.applyForceConstraints(point, force = nMag*normal)
sprite.applyForceConstraints(point, force = tMag*(R*normal))
Calling constraint for mousejoint:
Code:
if self.mouseJoint:
body = self.mouseJoint.bodyB # bodyB is always the target object
if body.userData and body.userData.has_key('sprite') and type(body.userData['sprite']) == ConstrainedSprite:
pos = self.mouseJoint.anchorB # anchorB is always on the target body, in world coords
force = self.mouseJoint.GetReactionForce(1/self.TIME_STEP) # force exerted by the mousejoint on the body
body.userData['sprite'].applyForceConstraints(pos, force*self.TIME_STEP)
And here's the code for doing the canceling:
Code:
def applyForceConstraint(self, point, force = None, torque = None):
"""Applies the anisotropic force constraint according to the current constraint
parameters. Equivalent to static friction, and should be called whenever a force
is acting on the body (even if the body is in motion).
@param point A b2Vec2 point in world coordinates where the force is acting. Assumed
to be from a contact experienced by the body, or applied by a joint.
@param force A b2Vec2 describing a force in world coordinates
"""
if force:
gain = 1.33
# project force vector onto xNormal of constraint
worldNormal = self.body.GetWorldVector(self.xNormal)
xForce = Box2D.b2Dot(worldNormal, force) * worldNormal
if abs(xForce.length) < self.xMu * self.body.mass:
# if subthreshold, apply opposite force to cancel it out
self.body.ApplyLinearImpulse(-gain*xForce, point)
# project force vector to yNormal of constraint
worldNormal = self.body.GetWorldVector(self.yNormal)
yForce = Box2D.b2Dot(worldNormal, force) * worldNormal
if abs(yForce.length) < self.yMu * self.body.mass:
# if subthreshold, apply opposite force to cancel it out
self.body.ApplyLinearImpulse(-gain*yForce, point)
# debugging: just try to reverse the original force on the point
# self.body.ApplyLinearImpulse(-gain*force, point)