Box2D Joints vehicle example
mberube

Thanks alot for the Joint addition in the WADE UI 3.7. You are doing a Fantastic job!!!

 

I'm struggling into creating a simple 2 wheels vehicles in the 2D engine.

Ref (Truck car game): http://www.emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars/

Would you please make a demo of some of the joints (wheel, revolute, prismatic) in a vehicle system?

  

All 7 Comments
mberube

Based on this post: http://www.emanueleferonato.com/2011/08/22/step-by-step-creation-of-a-box2d-cartruck-with-motors-and-shocks/

I now know that fixture filter groupIndex must be set to all the same value in order for axle, wheels and car not to colide with themselves.

hoodFixture.filter.groupIndex=-1;
axleFixture.filter.groupIndex=-1;
wheelFixture.filter.groupIndex=-1;

But im still not able to make PrismaticJoint work as expected.

 

 

Shri

mberube,

If you go to my website www.ashatej.com, there are some physics demos. If you specifically look at the robo demo or the stickman run and jump demo, both implement jumping with a prismatic joint. You can download the source code and take a look, specifically in the gameWorld.js and the robo.js files.

Both of these demos use the a version of wade before the physics joints were integrated, but the current wade implementation should be nearly identical to what is in the demo. Yuor should just have to make sure the call to "createPrismaticJoint" has the correct parameters. Since you looked at Senor Feronato's demos, you should be able to understand the javascript in my demos.

Additionally, the first game on my page "miner 49er" uses revolute joints for the cart, so if you can figure out how the prismatic joints work, but can't get the revolute joints to work, let me know and I can put up the source for the cart object from that game.

cheers,

Shri

mberube

Thanks a lot. I will try to create the PrismaticJoint programmaticly instead of with the UI, i'm pretty sure it is just a parameter issue!

EDIT: Yes it was a parameter issue (position of joint anchors). See screenshoot of Revolute Joint on a car.

foxcode

Funnily enough I couldn't make the wheel joint work either, I should really look into that...

However For a very simple car I found you can simply use 2 distance joints. I did this in the editor

I created a procedural square and made it rectangular for my car body, and for the wheels I simply used the procedural_circle sprite. If you make all 3 dynamic objects, and add a direction joint for each wheel, all you need to do is use the anchor property to essentially move where the axle is. I've made a very quick demo of this in the editor, see the attachment below. If the wheels need motors I think you might need a Prismatic Joint instead.
 

Shri

Foxcode,

You are going about it a little oddly, based on the box2d documentation. What you want to do is create three physics objects, we'll call them a box and two wheels (a procedural square and two circles). You then create two joints. One between the front wheel and the box and one between the back wheel and the box. When you create the joints,  the anchors for the joints are the centers of the wheels (0,0) and the offset on the box where you would want the wheels to go (-50,0) and (50,0). If you want to motorize your cart, all you have to do is set the 'motorSpeed' property on a joint (and make sure that the motor is enabled).

I have attached the code of how I did the cart and wheels in my miner 49er game, it's only one js and one json file. You should be able to translate them into the newer version of wade easily and repost for all.

cheers,

Shri

foxcode

Thankyou Shri

I was getting very confused. I was trying to use "wheel" joints initially not "revolute" joints. Your code was very helpful, though I think some of the joints code may have changed since you made this? There seems to be a difference in how you define the wheel location. When you build a simple cart in the editor, you do not have to specify localAnchors. When add joint gets called, it gets the position of both objects, finds the difference and uses that as the offset value. So whatever the initial offset of the scene objects is, will be the value of localAnchor. Any localAnchor you specify will be added to this value.

I can only guess that when you made your miner game ( which I played again :) ), this automatic offset calculation didn't exist.

For the original poster (any anyone else), see the attachment to this post. It is a cart made in the editor using revolute joints. The wheels have motors so it instantly starts rolling right when it hits the ground.



 

mberube

Yes it works great! Thanks you all for your excellent work and responses!

Post a reply
Add Attachment
Submit Reply
Login to Reply