Box2D Forums

It is currently Sun May 19, 2013 10:52 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed May 11, 2011 11:31 am 
Offline

Joined: Thu May 05, 2011 9:25 am
Posts: 6
I'm trying to make a vehicle as you can see. But The prismatic and revolute joints won't stay together well.

http://www.youtube.com/watch?v=8VkY0upr5-E


Code:
Chassis = new Sprite(450, 100, menuTextures.Chassis1);            
      
      final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 1.0f);
      objectFixtureDef.restitution = 0.0f;
      final FixtureDef pivotDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 1.0f);
      pivotDef.restitution = 0.0f;
      final FixtureDef wheelDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 5.0f);
      wheelDef.restitution = 0.0f;
      final ITriangulationAlgoritm triangulationAlgoritm = new EarClippingTriangulator();
      
      final float width = Chassis.getWidthScaled() / PIXEL_TO_METER_RATIO_DEFAULT;
      final float height = Chassis.getHeightScaled() / PIXEL_TO_METER_RATIO_DEFAULT;
      
      List<Vector2> mVertices = new ArrayList<Vector2>();
      
      mVertices.add(new Vector2(-0.39844f*width, -0.14648f*height));
      mVertices.add(new Vector2(-0.37500f*width, -0.14648f*height));
      mVertices.add(new Vector2(+0.02344f*width, -0.16406f*height));
      mVertices.add(new Vector2(+0.06152f*width, -0.17773f*height));
      mVertices.add(new Vector2(+0.06348f*width, -0.10938f*height));
      mVertices.add(new Vector2(+0.09375f*width, -0.12500f*height));
      mVertices.add(new Vector2(+0.18945f*width, -0.12305f*height));
      mVertices.add(new Vector2(+0.32812f*width, +0.07422f*height));
      mVertices.add(new Vector2(+0.21484f*width, +0.04883f*height));
      mVertices.add(new Vector2(+0.37695f*width, +0.11914f*height));
      mVertices.add(new Vector2(+0.39844f*width, +0.29688f*height));
      mVertices.add(new Vector2(+0.37793f*width, +0.29492f*height));
      mVertices.add(new Vector2(+0.39746f*width, +0.37109f*height));
      mVertices.add(new Vector2(+0.34375f*width, +0.37305f*height));
      mVertices.add(new Vector2(+0.21875f*width, +0.37500f*height));
      mVertices.add(new Vector2(+0.21875f*width, +0.41016f*height));
      mVertices.add(new Vector2(+0.02344f*width, +0.22461f*height));
      mVertices.add(new Vector2(-0.36133f*width, +0.45312f*height));
      mVertices.add(new Vector2(-0.36816f*width, +0.43750f*height));
      mVertices.add(new Vector2(-0.35547f*width, +0.40430f*height));
      mVertices.add(new Vector2(-0.35645f*width, +0.22461f*height));
      
      final List<Vector2> triangles = triangulationAlgoritm.computeTriangles(mVertices);
      
      final Body chassisBody = PhysicsFactory.createTrianglulatedBody(mPhysicsWorld, Chassis, triangles, BodyType.DynamicBody, objectFixtureDef);      
        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(Chassis, chassisBody, true, true));

        final Shape leftJointShape = new Rectangle( Chassis.getX()+ Chassis.getWidth() - 210, Chassis.getY()+ Chassis.getHeight() - 35, 10, 25);
        final Body leftAnchorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, leftJointShape, BodyType.DynamicBody, pivotDef);
        leftAnchorBody.setTransform( new Vector2( leftAnchorBody.getWorldCenter().x, leftAnchorBody.getWorldCenter().y ), 60f); // change rotation
       
        final Shape rightJointShape = new Rectangle(Chassis.getX()+ Chassis.getWidth() - 70, Chassis.getY()+ Chassis.getHeight() - 35, 10, 25);
        final Body rightAnchorBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, rightJointShape, BodyType.DynamicBody, pivotDef);
        rightAnchorBody.setTransform( new Vector2( rightAnchorBody.getWorldCenter().x, rightAnchorBody.getWorldCenter().y ), 125f); // change rotation
       
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftJointShape, leftAnchorBody, true, true));
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightJointShape, rightAnchorBody, true, true));
       
        final Sprite WheelLeft = new Sprite(Chassis.getX()+ Chassis.getWidth() - 215, Chassis.getY()+ Chassis.getHeight() - 25, menuTextures.Wheel1);
        final Body leftWheelBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, WheelLeft, BodyType.DynamicBody, wheelDef); 
        leftWheelBody.setSleepingAllowed(false);
      final Sprite WheelRight = new Sprite(Chassis.getX()+ Chassis.getWidth() - 70, Chassis.getY()+ Chassis.getHeight() - 25, menuTextures.Wheel1);
      final Body rightWheelBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, WheelRight, BodyType.DynamicBody, wheelDef);
      rightWheelBody.setSleepingAllowed(false);
      
      this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(WheelLeft, leftWheelBody, true, true));
        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(WheelRight, rightWheelBody, true, true));
       
        scene.getFirstChild().attachChild(Chassis);
        scene.getFirstChild().attachChild(leftJointShape);
        scene.getFirstChild().attachChild(rightJointShape);
        scene.getFirstChild().attachChild(WheelLeft);
        scene.getFirstChild().attachChild(WheelRight);
       
        final PrismaticJointDef leftPrismaticJointDef = new PrismaticJointDef();
        //leftPrismaticJointDef.initialize(chassisBody, leftAnchorBody, new Vector2(Chassis.getWidth() - 210, Chassis.getHeight() - 25), new Vector2(0, 1.0f)); // the 3rd parameter says where the fuck
        //leftPrismaticJointDef.initialize(chassisBody, leftAnchorBody, leftAnchorBody.getWorldCenter(), new Vector2((float)Math.cos(Math.PI/3), (float)Math.sin(Math.PI/3))); // the 3rd parameter says where the fuck
        leftPrismaticJointDef.bodyA = chassisBody;
        leftPrismaticJointDef.bodyB = leftAnchorBody;
        leftPrismaticJointDef.localAnchorA.set(-2.2f, 1.6f);
        leftPrismaticJointDef.localAnchorB.set(0, 0);
        leftPrismaticJointDef.localAxis1.set(new Vector2((float)Math.cos(Math.PI/3), (float)Math.sin(Math.PI/3)));
        leftPrismaticJointDef.lowerTranslation = -0.0f;
        leftPrismaticJointDef.upperTranslation = 0.0f;
        leftPrismaticJointDef.enableLimit = true;
        leftPrismaticJointDef.enableMotor = true;
        leftPrismaticJointDef.maxMotorForce = 0;
        leftPrismaticJointDef.motorSpeed = 0;
        this.mPhysicsWorld.createJoint(leftPrismaticJointDef);
       
        final PrismaticJointDef rightPrismaticJointDef = new PrismaticJointDef();
        //rightPrismaticJointDef.initialize(chassisBody, leftAnchorBody, new Vector2(Chassis.getWidth() - 70, Chassis.getHeight() - 25), new Vector2(0, 1.0f)); // the 3rd parameter says where the fuck
        //rightPrismaticJointDef.initialize(chassisBody, rightAnchorBody, rightAnchorBody.getWorldCenter(), new Vector2((float)-Math.cos(Math.PI/3), (float)Math.sin(Math.PI/3))); // the 3rd parameter says where the fuck
        rightPrismaticJointDef.bodyA = chassisBody;
        rightPrismaticJointDef.bodyB = rightAnchorBody;
        rightPrismaticJointDef.localAnchorA.set(2.2f, 1.6f);
        rightPrismaticJointDef.localAnchorB.set(0, 0);
        rightPrismaticJointDef.localAxis1.set(new Vector2((float)-Math.cos(Math.PI/3), (float)Math.sin(Math.PI/3)));
        rightPrismaticJointDef.lowerTranslation = -0.0f;
        rightPrismaticJointDef.upperTranslation = 0.0f;
        rightPrismaticJointDef.enableLimit = true;
        rightPrismaticJointDef.enableMotor = true;
        rightPrismaticJointDef.maxMotorForce = 0;
        rightPrismaticJointDef.motorSpeed = 0;
        this.mPhysicsWorld.createJoint(rightPrismaticJointDef);
       
        final RevoluteJointDef leftRevoluteJointDef = new RevoluteJointDef();
        //leftRevoluteJointDef.initialize(leftAnchorBody, leftWheelBody, leftWheelBody.getWorldCenter());
        leftRevoluteJointDef.bodyA = leftAnchorBody;
        leftRevoluteJointDef.bodyB = leftWheelBody;
        leftRevoluteJointDef.localAnchorA.set(0,0);
        leftRevoluteJointDef.localAnchorB.set(0,-0.5f);
        leftRevoluteJointDef.enableLimit = true;
        leftRevoluteJointDef.enableMotor = true;
        leftRevoluteJointDef.maxMotorTorque = 200;
        leftRevoluteJointDef.motorSpeed = 10;       
        this.mPhysicsWorld.createJoint(leftRevoluteJointDef);
       
        final RevoluteJointDef rightRevoluteJointDef = new RevoluteJointDef();
        //rightRevoluteJointDef.initialize(rightAnchorBody, rightWheelBody, rightWheelBody.getWorldCenter());
        rightRevoluteJointDef.bodyA = rightAnchorBody;
        rightRevoluteJointDef.bodyB = rightWheelBody;
        rightRevoluteJointDef.localAnchorA.set(0,0);
        rightRevoluteJointDef.localAnchorB.set(0,-0.5f);
        rightRevoluteJointDef.enableLimit = true;
        rightRevoluteJointDef.enableMotor = true;
        rightRevoluteJointDef.maxMotorTorque = 200;
        rightRevoluteJointDef.motorSpeed = 10;
        this.mPhysicsWorld.createJoint(rightRevoluteJointDef);


Top
 Profile  
 
PostPosted: Fri May 13, 2011 2:48 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
Just at a guess (can't tell from your code) I suspect your chassis is way too heavy for the other bodies - joints tend to behave badly when the mass of the bodies they connect is too different.
You might also want to check out the 'Line joint' (see the manual at 8.11 Line Joint) which is made for this sort of thing.


Top
 Profile  
 
PostPosted: Mon May 16, 2011 12:07 pm 
Offline

Joined: Thu May 05, 2011 9:25 am
Posts: 6
I have fixed a bit this, by trying using only revoltue joints.

But I've got another problem now, sometimes the whole trucks would enter the ground.

Any suggestions?


Top
 Profile  
 
PostPosted: Sun May 22, 2011 2:16 pm 
Offline

Joined: Thu May 05, 2011 9:25 am
Posts: 6
Could This be because I'm testing on the emulator?


Top
 Profile  
 
PostPosted: Thu Jul 28, 2011 10:10 am 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
Emulator? Android?

I hate to ask, but are you using pixel coordinates?


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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