Box2D Forums

It is currently Tue May 21, 2013 12:14 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Wed Nov 04, 2009 1:43 am 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
Hi,

I'm new to this, but wanted to get my feet wet. Decided to try to see how hard it would be to extend Box2d to see how hard it was to get some dry friction action going on. (enough so I can simulate car wheels at least)

First attempt was just adding some force or impulses every step in "user land" to counteract the force up to a friction value. This didn't work out when there was contact and bodies pushing on joints (body would move even if the force being applied was significantly less than the friction force).

Then tried it with a controller. Same issue, more or less (even though it had better abstractions than the previous approach)

Long story short, I came up with just making a special type of joint that took one body so it would "fight" with the other joints during the contraint processing.

The joint isn't really a joint, but merely a constraint with one body (I feel a joint would be a subclass of the type of contraint with one body, but there's currently nothing that needs it). It's passed a vector for x and y friction. Ideally a production version of this would allow the user to choose a different alignment of this friction (and probably a point instead of it just being applied to the center of mass)

The same technique could be used for angular joints. Also, I plan on experimenting with making a Drag type friction joint.

Here's a quick video of the demo
http://screenr.com/fBB

Code
http://github.com/mikelikespie/box2d-demo/tree/car

Make sure you're looking at the car branch.

The car demo can be found in /Box2d/Examples/TestBed/Test/TopDownCar.h
The joint I created is b2FrictionJoint. (http://github.com/mikelikespie/box2d-de ... nJoint.cpp)

Had to hack up the core processing a bit to allow for joints with only one body defined.
Also hacked up the test bed to make steering more natural and having multiple keys pressed.

Please excuse the code quality, it's not meant for production.

Would like feedback on this method, and whatnot. if people are interested (and this is a reasonable approach I wouldn't mind making it better and contribbing and whatnot).

-- update --

I pushed a new version to git with a friction joint that's significantly cleaned up, and has support for angle friction too. I also cleaned up the hack I did to the world and island and whatnot.

The attached patch should apply to trunk.

p.s.
sup with the inconsistent cr lfs?


Attachments:
friction_joint.patch [10.5 KiB]
Downloaded 133 times


Last edited by MikeRLewis on Wed Nov 11, 2009 3:16 am, edited 1 time in total.
Top
 Profile  
 
PostPosted: Thu Nov 05, 2009 6:35 pm 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
Btw, looking for suggestions on how to make this cleaner and more extensible. It would be cool to have something that works like a rudder. You could make interesting things both side view and top down (like a sideways flight simulator or something).


Top
 Profile  
 
PostPosted: Fri Nov 06, 2009 11:13 am 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
It looks good. It's a cool technique for using joints for something decidedly non-jointlike. It's definitely the right thing to do - joints really means anything other than collisions that participates in the solver.

You can generalize the direction of friction using a tensor (i.e. a matrix in 2d). Look at b2TensorDamping controller for how I generalized linear damping to have directional control. Also, assuming that the entire shape is in contact with the ground, the frction will always behave as if at the center of mass, so I wouldn't worry about generalizing that.


Top
 Profile  
 
PostPosted: Sat Nov 07, 2009 1:55 am 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
Yeah, i came across the b2TensorDamping. I actually used that code to try making a friction controller, but had problems like I described.

So, I'm not sure if I understand what the T matrix transformation represents. I know that the 11 and 22 represent the x and y friction. What does rotation actually 'mean' for the TensorDamping.

Sorry, my math skills are getting soft.

-Mike


Top
 Profile  
 
PostPosted: Sat Nov 07, 2009 4:46 am 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
No, your joint based solution is much better for friction.

And a rotational matrix doesn't really represent anything. It's highly unlikely you'd want that for damping or friction. Matrices are just a way of generalizing directional control. It's general, because though you've identified 11 and 22 as the x and y friction, that's only the case if the other two are zero. But I can use it to represent friction in other directions, too. Suppose I had something that only had friction of factor 2 in direction (1,1), then the matrix M= [1 1; 1 1] would represent it. Notice how M(1,1) = (2, 2) and M(1, -1) = (0,0), where (1,-1) is the orthogonal axis. That's why I call it a tensor, not a matrix - tensors are meant to be objects not tied to a specific axis (though they look like matrices when you do express them in a given axis).

With a tensoryou can construct independent levels of friction for any choice of orthogonal axis, not just x,y.


Top
 Profile  
 
PostPosted: Sat Nov 07, 2009 1:02 pm 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
That's interesting.

Is this the type of Tensor you're referring to? http://en.wikipedia.org/wiki/Tensor_field

It seems like Tensor could refers toa couple different concepts.

A few more q's:

If I do rotational friction, should I create a separate type of FrictionJoint, or just allow the one that does directional friction do rotational friction too? (I prefer the latter I think)

I have it set up where you give the joints a friction "force," not a coefficient or anything. The coefficient wouldn't work because you can't really account for one body adding z down force to another body right now.

I'd like to try to make this code production ready and maybe get it into Box2d source tree. For this I think I'd want to do the following:

[list=]
[*]Create a constraint class that has just one body
[*]Refactor the joint class to inherit from the constraint class (a joint would be more or less a constraint with two bodies)
[/list]

Also, I want to make a joint/constraint to simulate drag, which I suppose I could apply the same thing to. Do you know what the effect is called that a rudder or fin has where it redirects force? (I believe it does at least, I could be wrong). I'm looking to simulate that too... perhaps for missiles and boats and whatnot.

Thanks again!


Top
 Profile  
 
PostPosted: Sat Nov 07, 2009 3:46 pm 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
Tensor. In practical terms, I could sum it up this way:
if A = [x 0; 0 y] represents friction of x in the x-axis, and y-in the y axis, and a linear combination for other directions, and R is a rotation matrix rotating these two axis onto two other axis, then RAR^-1 represents friction of x along one of those axis and y the other. To actually get the friction for a given direction, use matrix multiplication.

I don't have good answers to the other questions.
I would not recommend creating a unary equivalent to joints, it complicates everything for relatively little benefit. How many types of unary joint can there be, anyway? I'd instead convert your joint to be a true binary joint, by defining friction on a body relative to another body. There already exist joints that are not strictly speaking binary (gear joint, constant volume joint (from Java)), which work find in the current system.

I'd bet the tensor damping component works ok for boats, if you create a rudder body and a hull body and give both appropriate damping levels. I don't know the physics of what it should truly be.

It's nice that you want to contribute this code, but I don't think Erin is accepting code at the moment for the core engine, and that's the only place joints can be inserted (thanks to the closed factory design). I think the best you can get is maintaining a patch. But please do. Now you've drawn my attention to it, I see this as an absolutely necessary component.


Top
 Profile  
 
PostPosted: Wed Nov 11, 2009 3:17 am 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
Just an update. I cleaned it up and posted a patch to the original post.

It's looking pretty reasonable now.


Top
 Profile  
 
PostPosted: Wed Nov 11, 2009 9:51 am 
Offline

Joined: Fri Mar 14, 2008 12:18 am
Posts: 58
Is there any chance to get Erin to integrate these patches in to mainstream Box2d?

You could release you patches under MIT license then Erin could legally "steal" your work and put it under Box2d's preferred licensing / copyright.

I ask because I ended up implementing both of these features for my top down game. I had to explicitly setting the forces/torques for the rotation and friction constraints before each call to Step. I thought about building them as joints (to get box2d to do the explicity call for me), but didn't want to tackle the single body joint problem.

-nick


Top
 Profile  
 
PostPosted: Wed Nov 11, 2009 9:57 am 
Offline

Joined: Wed Nov 04, 2009 1:05 am
Posts: 9
Consider my patches licensed AS IS or MIT license or whatnot. Eric or anybody else can take them. I'd love to see them go upstream.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group