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;