2
\$\begingroup\$

I have two rectangular bodies. I've applied Weldjoint successfully on these bodies. I want to move that joined body by applying linear impulse. After weld joint, these two bodies becomes single body right? How do I apply force/impulse on the joined body?

I am using Box2D with LibGDX.

I've tried this:

polygon1.applyLinearImpulse(new Vector2(-5, 0), polygon1.getWorldCenter(), true);

I thought that if I move polygon1 then polygon2 will also move due to my weld joint but it is not working properly.

Why don't they move together after being welded?

below is my code.

package com.test.box2d;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.WeldJointDef;

public class WeldJointTest extends Game{

    World world;
    Box2DDebugRenderer debugRenderer;
    OrthographicCamera camera;
    Body polygon1, polygon2;

    @Override
    public void create() {

        world = new World(new Vector2(0, -10), true);
        debugRenderer = new Box2DDebugRenderer();
        camera = new OrthographicCamera(Gdx.graphics.getWidth() / 10, Gdx.graphics.getHeight() / 10);

        //body def
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.set(-20, -8f);

        //shape
        PolygonShape polygonShape = new PolygonShape();
        polygonShape.setAsBox(2, 2);

        //fixture def
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.density = 1;
        fixtureDef.restitution = .2f;
        fixtureDef.friction = .3f;
        fixtureDef.shape = polygonShape;

        polygon1 = world.createBody(bodyDef);
        polygon1.createFixture(fixtureDef);

        // body def
        bodyDef.position.set(-16, -8f);

        polygon2 = world.createBody(bodyDef);
        polygon2.createFixture(fixtureDef);

        //floor body def
        bodyDef.type = BodyType.StaticBody;
        bodyDef.position.set(-40, -50);

        // floor shape
        ChainShape chainShape = new ChainShape();
        chainShape.createChain(new Vector2[]{new Vector2(-100, 40), new Vector2(100, 40)});

        //fixture def
        fixtureDef.shape = chainShape;

        world.createBody(bodyDef).createFixture(fixtureDef);


        WeldJointDef weldJointDef = new WeldJointDef();
        weldJointDef.bodyA = polygon1;
        weldJointDef.bodyB = polygon2;
        weldJointDef.localAnchorA.set(new Vector2(4, 0));
//      weldJointDef.localAnchorB.set(polygon2.getWorldCenter());

        world.createJoint(weldJointDef);
        /*DistanceJointDef distanceJointDef = new DistanceJointDef();
        distanceJointDef.bodyA = polygon1;
        distanceJointDef.bodyB = polygon2;
        distanceJointDef.length = 25;

        world.createJoint(distanceJointDef);*/

        chainShape.dispose();
        polygonShape.dispose();
    }

    @Override
    public void render() {

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        debugRenderer.render(world, camera.combined);
        world.step(1/60f, 8, 3);

        Gdx.input.setInputProcessor(new InputProcessor() {

            @Override
            public boolean keyDown(int keycode) {

                switch (keycode) {
                case Keys.LEFT:
                    polygon1.applyLinearImpulse(new Vector2(-5, 0), polygon1.getWorldCenter(), true);
                    break;

                default:
                    break;
                }

                return true;
            }

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean scrolled(int amount) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean mouseMoved(int screenX, int screenY) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean keyUp(int keycode) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean keyTyped(char character) {
                // TODO Auto-generated method stub
                return false;
            }

        });

    }

}

from main method.

LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "Body Movement Test";
        cfg.useGL20 = false;
        cfg.width = 800;
        cfg.height = 480;
\$\endgroup\$
3
  • \$\begingroup\$ What does "it is not working properly" mean? Describe exactly what happens. Please edit your question to include that information. \$\endgroup\$
    – House
    Oct 22, 2013 at 21:47
  • \$\begingroup\$ Isn't a weld joint the same as any other joint - the bodies are still 'separate'? \$\endgroup\$
    – ThorinII
    Oct 22, 2013 at 23:39
  • \$\begingroup\$ I've edited my question and add my code.Please look at this. After pressing LEFT key... it is not moving. What I am thinking is 2 bodies have been joint each other. If i apply force on first body, second body should also move . AM I thinking correctly or not??? \$\endgroup\$
    – ved
    Oct 23, 2013 at 16:19

1 Answer 1

2
\$\begingroup\$

I think I've managed to reproduce your problem - basically your friction is too high/your impulse not enough.

Change your impulse from (-5, 0) to something like (-50, 0), and/or lower your friction coefficient. Seems to work...

Also, on a side note, I wouldn't recommend instantiating and setting your InputProcessor every frame - do it once in your create() method (unless you have reason).

\$\endgroup\$
2
  • \$\begingroup\$ thanx.. and i've done that. but, now my next problem is how to calculate the center of mass of combined body. \$\endgroup\$
    – ved
    Oct 24, 2013 at 5:10
  • \$\begingroup\$ You can calculate the combined centre of mass very easily - Google will help you (for eg see this page) \$\endgroup\$
    – ThorinII
    Oct 24, 2013 at 5:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .