Sprite is not attached with a body in libgdx - libgdx

I want the sprite to get attached with a polygon shape body. When I run the code, both the sprite and body is getting displayed but they are not linked with each other. I looked at all the stack overflow questions but nothing helped.
I am creating this in a class and I am instantiating this from main class.
This is my code,
public SpriteButtons(PlayScreen screen) {
this.world = screen.getWorld();
batch = new SpriteBatch();
region = new TextureRegion(screen.getAtlas().findRegion("fireball"), 0, 0, 16, 16);
sprite = new Sprite(region);
this.gamecame = new OrthographicCamera();
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
bodyDef.position.set(0, 0);
body = world.createBody(bodyDef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(0.2f, 0.2f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1f;
sprite.setSize(25, 25);
Fixture fixture = body.createFixture(fixtureDef);
}
public OrthographicCamera getGamecame() {
return gamecame;
}
public void render(float dt) {
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2f,
body.getPosition().y - sprite.getHeight()/2f );
Gdx.app.log("" + body.getPosition().x, "");
batch.begin();
sprite.draw(batch);
setRegion(region);
batch.end();
}
My output is as follows,
The badlogic sprite is not getting attached with a polygon body and I am instantiating this class from main class and I will be calling this class render method from main class render method. Sorry if the question seems stupid. I don't know where I am missing this. Please help..!! Thanks in advance..!!

You must update sprite's position according to the body position in the render method - if you are not doing this how the sprite can know that it is attached to body?
Please notice that box2d body's origin is in the center of the body when libgdx'es sprite's origin is at it's left botto corner. Then you need to subtract half of width and height of body's position.
So this is what you've got to do:
//render()
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2f,
body.getPosition().y - sprite.getHeight()/2f );
...
sprite.draw(batch);
...

try doing this
batch.draw(sprite, sprite.getX(), sprite.getY()); and change body's inital postion somewher above,so that it fall and you can check under gravity by initializing world like world = new World(new Vector2(0, -98f), true);

Related

Sprite appearing in top right corner of the screen in LIBGDX

I am working on a game using LIBGDX. I am following tutorial from this . Everything works perfectly fine but my output sprite is at the top right corner of the screen. I am trying to center it around but I am not able to ! I even adjusted the camera but I failed.
This is my part of screen code,
public PlayScreen(MyGame game) {
this.game = game;
this.gamecam = new OrthographicCamera();
this.gamePort = new FitViewport(MyX.V_WIDTH / MyXGame.PPM, MyX.V_HEIGHT / MyXGame.PPM, gamecam);
this.hud = new HudClass(game.batch);
loader = new TmxMapLoader();
map = loader.load("tiledmap.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / MyJungleGame.PPM);
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0, -10), true);
b2dr = new Box2DDebugRenderer();
creator = new B2WorldCreator(this);
gamehero = new Hero(this);
world.setContactListener(new WorldContactListener());
items = new Array<Item>(); //initializing things
itemsToSpawn = new LinkedBlockingQueue<ItemDef>();
}
My render method,
public void render(float delta) {
this.update(delta);
Gdx.gl.glClearColor(1, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gamecam.combined);
game.batch.setProjectionMatrix(this.gamecam.combined);
game.batch.begin();
gamehero.draw(game.batch);
for (Goomba enemy : creator.getGoombas())
enemy.draw(game.batch);
for(Item item :items)
item.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
I have attached the screen shot of the output .
To my knowledge, problem lies in setting up the camera in playscreen. But I don't know how to resolve it. Please help I am new to LIBGDX. Thanks in advance.
https://github.com/libgdx/libgdx/wiki/Orthographic-camera
This is the best help that I can give to you. Hope that it helps. Also, I would encourage you to write and design better you code, it is somehow a little bit hard to read.
Cheers.

Units conversion Box2D with weird results

i'm using LibGDX and trying to learn box2D, but this units conversions are confusing me, what i think: i got a 256x256 pixels image and want to create a body representing this image, using the 1:32 scale, so everytime i want to pass values to the box2D scale i must divide it by 32 or mutiply by 1/32(it's the same equation), and everytime i want to get values from this world to pixel scale i must mutiply it by 32, but the problems still the same: can't get good simulations, i'll put my code here for you see how i'm doing things(i'll put the variables too so you can just ctrl+c and ctrl+v):
private SpriteBatch batch;
private ShapeRenderer srender;
private Texture img;
private World world;
private Body bad, bridge;
private OrthographicCamera cam, cam2;
private Box2DDebugRenderer render;
#Override
public void create () {
srender = new ShapeRenderer();
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
cam = new OrthographicCamera(500f, 500f);
cam.translate(250f, 250f);
cam.update();
//Used to draw the box2D world scaled to the pixels size
cam2 = new OrthographicCamera(500 * 0.03125f, 500 * 0.03125f);
cam2.translate(250f * 0.03125f, 250f * 0.03125f);
cam2.update();
render = new Box2DDebugRenderer();
world = new World(new Vector2(0f, -10f), true);
BodyDef bdef = new BodyDef();
bdef.type = BodyType.DynamicBody;
bad = world.createBody(bdef);
PolygonShape pshape = new PolygonShape();
pshape.setAsBox(img.getWidth() * 0.03125f, img.getHeight() * 0.03125f);
FixtureDef fdef = new FixtureDef();
fdef.shape = pshape;
bad.createFixture(fdef);
/*Criando a ponte*/
bdef.type = BodyType.StaticBody;
bridge = world.createBody(bdef);
pshape.setAsBox(500f * 0.03125f, 5f * 0.03125f);
bridge.createFixture(fdef);
bad.setTransform(new Vector2(250f * 0.03125f, 500f * 0.03125f) , 0);
bridge.setTransform(new Vector2(250f * 0.03125f, 0f * 0.03125f), 0);
batch.setProjectionMatrix(cam.combined);
srender.setProjectionMatrix(cam.combined);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, (bad.getPosition().x - img.getWidth()/2 * 0.03125f) * 32,
(bad.getPosition().y - img.getHeight()/2 * 0.03125f) * 32);
batch.end();
render.render(world, cam2.combined);
srender.begin(ShapeRenderer.ShapeType.Filled);
srender.circle(250f, 500f, 5f);
srender.circle(250f, 0f, 5f);
srender.end();
world.step(1/60f, 6, 2);
}
if you run this code you will see that the box created still not fitting the image size, the image stop walking on the middle of the screen while it should stop only at the bottom, can anyone help me here? thanks for attention.
Ps:i have already heard about changing the viewport, but i'm still trying to learn this conversions, after that i'll go look for the viewports.

body falls slowly in any gravity

I've created a World with earth gravity and I place an entity in the scene (contains a sprite and a Body) and it falls down slowly like a balloon.
Here's how I set the World:
world = new World(new Vector2(0, -GRAVITY_EARTH), true);
and here's the relevant Box2D code for the Body etc:
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(positionX, positionY);
// Create our body in the world
body = world.createBody(bodyDef);
// Grab the first idle sprite to use as initial
Sprite sprite = idleSprites.get(0);
// Create a box shape to represent our hit box
PolygonShape box = new PolygonShape();
box.setAsBox(sprite.getWidth() / 2f, sprite.getHeight() / 2f);
// Create a fixture definition to apply our shape
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 1f; // Give it full density
fixtureDef.friction = 0f; // Give it no friction
fixtureDef.restitution = 0f; // Make it not bouncy
// Create our fixture and attach it to the body
fixture = body.createFixture(fixtureDef);
// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
box.dispose();
and how I draw the sprite:
TextureRegion keyFrame = idleAnimation.getKeyFrame(stateTimeSeconds, true);
Vector2 position = body.getPosition();
batch.draw(keyFrame, position.x - keyFrame.getRegionWidth() / 2f, position.y - keyFrame.getRegionHeight() / 2f);
and the relevant code in the render() method:
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
final float deltaTime = Gdx.graphics.getDeltaTime();
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(sky, 0, 0);
tim.animate(spriteBatch, deltaTime);
spriteBatch.draw(floor, 0, 0);
spriteBatch.end();
// Render physics for debug
debugRenderer.render(world, camera.combined);
// Run physics
doPhysicsStep(deltaTime);
}
private void doPhysicsStep(float deltaTime) {
// fixed time step
// max frame time to avoid spiral of death (on slow devices)
float frameTime = Math.min(deltaTime, 0.25f);
accumulator += frameTime;
while (accumulator >= TIME_STEP) {
world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
accumulator -= TIME_STEP;
}
}
I've tried changing the density of the fixture, and I've tried changing the gravity value, and I've tried changing the TIME_STEP and nothing is having an effect. The body just falls down slowly like a balloon.
It looks to me like you're using pixels as your units, box2d treats every unit as a meter and so you're hitting the internal limit of 2.0 units per time step, see http://www.iforce2d.net/b2dtut/gotchas. You can get around this by setting up your camera in world units instead of pixels, you have to scale all your sprites and positions to fit into world units instead of pixels though.
Something like this may do:
float w = (float) Gdx.graphics.getWidth();
float h = (float) Gdx.graphics.getHeight();
camera = new OrthographicCamera(30, 30 * (h / w));
the way the camera is set up here allows the height of the viewport to be variable based on the screens' aspect ratio.
Then to setup the sprite change it by a set factor
sprite.setSize(sprite.getWidth / PIX2M, sprite.getHeight / PIX2M);
where PIX2M is a static field defining how many pixels are a meter in box2d
Alternatively you can set the dimensions of the sprite explicitly to a value which makes physical sense and with the aspect ratio of the original image(my personal preference) . So an image of a person which is 100 x 500 for example could be set like this.
sprite.setSize(.4f, 2f);
meaning the person is 2 meters high and .4 meters wide. Also with this method you don't need a PIX2M conversion factor and you will always know the exact size of your body. Since you set the camera to a specific number of world units, 30 in this case, the sprite will take up the same amount of room on the screen no matter the resolution of the display.

LibGDX Box2D incorrect sprite position of moving body

I'm trying to make a 2D car side scrolling game. I'm using wheel joints to move the car.
Here is the screenshot when car is not moving.
Screenshot of car when not moving
And when the car is moving. You can see sprites are not in correct position.
Screenshot when the car is moving
Here is the constructor of car object.
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(x, y);
//Chassis
PolygonShape chassisShape = new PolygonShape();
chassisShape.setAsBox(width, height);
chassisFixtureDef.shape = chassisShape;
chassis = world.createBody(bodyDef);
// Car Body Sprite
Sprite body = new Sprite(new Texture(Gdx.files.internal("data/body.png")));
body.setSize(5f, 2f);
body.setPosition(0f, 0);
body.setOrigin(body.getWidth() / 2, body.getHeight() / 2);
chassis.setUserData(body);
chassis.createFixture(chassisFixtureDef);
//Left Wheel
CircleShape wheelShape = new CircleShape();
wheelShape.setRadius(height / 1.5f);
wheelFixtureDef.shape = wheelShape;
leftWheel = world.createBody(bodyDef);
//Sprite Test
wheel = new Sprite(new Texture(Gdx.files.internal("data/wheel.png")));
wheel.setSize(1f, 1f);
wheel.setOrigin(wheel.getWidth() / 2, wheel.getHeight() / 2);
leftWheel.setUserData(wheel);
leftWheel.createFixture(wheelFixtureDef);
//Right Wheel
rightWheel = world.createBody(bodyDef);
rightWheel.setUserData(wheel);
rightWheel.createFixture(wheelFixtureDef);
//Left Axis
WheelJointDef def = new WheelJointDef();
def.bodyA = chassis;
def.bodyB = leftWheel;
def.frequencyHz = chassisFixtureDef.density;
def.localAnchorA.set(-width / 2 * 1.7f + wheelShape.getRadius(), -height / 2 * 2.5f);
def.localAxisA.set(Vector2.Y);
def.maxMotorTorque = chassisFixtureDef.density * 30;
leftAxis = (WheelJoint) world.createJoint(def);
def.bodyB = rightWheel;
def.localAnchorA.x *= -1;
rightAxis = (WheelJoint) world.createJoint(def);
And Here is the code to draw sprites relevant to bodies on screen.
for (Body body : bodies)
if (body.getUserData() != null && body.getUserData() instanceof Sprite){
Sprite sprite = (Sprite)body.getUserData();
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(batch);
}
Sorry if couldn't make my question very specific or clear. I'm new to Stackoverflow.
Edit [Solved]
Just placed in render method [Correct]
renderer.render(world, camera.combined);
after gl.clear
I was doing it after drawing everything on screen. [Wrong]
seems like you update your physic after rendering it, because your physic-debugged polygon moved already further than the sprite shows up. You could try to first update your physics and then draw your sprites.
There shouldn't be something wrong with the code / calculations itself.
If you're already doing so it's probably because the updates of your physic simulation updates more frequently than your rendering-loop.

How to change size after it has been created

I am using java, libgdx, and box2d
In main class I have created a player. I want to change shape.setAsBox to 100 in player class. So in other words I want to change shape.setAsBox after it has been created. I believe only way to do this is to delete fixture and recreate a new one with 100 size. How can I do this.
public class main{
...
public main(){
//create player
BodyDef bdef = new BodyDef();
Body body;
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
/***Body - Player ***/
bdef.type = BodyType.DynamicBody;
bdef.position.set(50 / PPM, 50 / PPM);
bdef.linearVelocity.set(1.5f, 0);
body = world.createBody(bdef);
/*** 1st fixture ***/
shape.setAsBox(50/ PPM, 50 / PPM);
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
body.createFixture(fdef).setUserData("player");
player = new Player(body);
}
....
public void update(float dt) {
playerObj.update(dt);
...
}
}
// playyer class
public class player{
public player(Body body){
super(body);
}
....
public void update(){
//get player x position
currentX = this.getBody().getPosition().x;
// how can I delete old fixture and recreate a new one?
// which will has shape.setAsBox = 100.
}
}
Destroy the Fixture and redefine it. Since your player has one Fixture, either keep track of it to remove it or call:
this.getBody().destroyFixture(this.getBody().getFixtureList().first());
Then recreate a simple shape in the already existing Body:
PolygonShape shape;
FixtureDef fdef;
// Create box shape
shape = new PolygonShape();
shape.setAsBox(100 / PPM, 100 / PPM);
// Create FixtureDef for player collision box
fdef = new FixtureDef();
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
// Create player collision box fixture
this.getBody().createFixture(fdef).setUserData("player");
shape.dispose();