lag problem: not fluid movement on Box2D game

Discuss issues specific the Java port of Box2D
hmsdel91
Posts: 2
Joined: Tue Aug 04, 2015 4:26 pm

lag problem: not fluid movement on Box2D game

Postby hmsdel91 » Tue Aug 04, 2015 4:35 pm

Hi,I'm a newbie game developer. I've been trying to
develop a game using LibGDX and Box2D physics engine,but
when I export my android application, It's not working
as It should be. This problem is happening in some
devices not matter if it's an old device or a new one,for example the
app is working good in Sony Xperia Z1, Motorola XT615, LG L1,
LG L3, LG L5,etc.
However, in Sony Xperia U,Motorola Moto E,Samsung Galaxy's,etc is not
working like I expect. You can see a not fluid behavior when the object is
moving across the screen,that's the problem I have. It's like a stutter effect, not fluid.
I read on forums that It could be a FPS (frames per second)problem when I set the time step in
the world. So , I am using a fixed time step method and with the remainder time that I get in
"accumulator" I interpolate it.

I read these articles to learn the fixed time step and interpolation method:

http://gafferongames.com/game-physics/fix-your-timestep/
http://saltares.com/blog/games/fixing-your-timestep-in-libgdx-and-box2d/

but I think that there is a fail in my code that I can't see.
Please I need you to see the code and tell me what I'm doing wrong. I'm desperate, I tried everything but
I can't find the solution to my problem!.

Here is my code:

package com.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.TimeUtils;

public class MyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
Sprite sprite;
Body body;
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
World world;
final float PixelsToMeters=100f;
float step = 1.0f / 60.0f ,alpha;
Vector2 currentPosition,lastPosition,position;
double currentTime,accumulator,lastAngle,currentAngle;
final float BODY_WIDTH=95f,BODY_HEIGHT=95f;

@Override
public void create () {
world= new World(new Vector2(0f,-9.8f),true);
batch = new SpriteBatch();
img = new Texture("circulo.png");
sprite=new Sprite(img);

//BODY DEFINITION
BodyDef bodydef=new BodyDef();
bodydef.type= BodyType.DynamicBody;
bodydef.position.set(-3.5f,-2.4f);
body=world.createBody(bodydef);

CircleShape shape=new CircleShape();
shape.setRadius((sprite.getWidth()/2)/PixelsToMeters);

FixtureDef fixturedef=new FixtureDef();
fixturedef.shape=shape;
fixturedef.density=0.1f;
fixturedef.restitution=1f;
fixturedef.friction=0.5f;
body.createFixture(fixturedef);
shape.dispose();

//SET SPRITE POSITION
sprite.setPosition(body.getPosition().x *PixelsToMeters - BODY_WIDTH/2,
body.getPosition().y * PixelsToMeters - BODY_HEIGHT/2);

//THROW CIRCLE
body.setLinearVelocity(3.5f,9.5f);

lastPosition=new Vector2();
position=new Vector2();
camera=new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

}

@Override
public void render () {
camera.update();

double newTime = TimeUtils.millis() / 1000.0;
double frameTime = Math.min(newTime - currentTime, 0.25);
float deltaTime = (float)frameTime;

currentTime = newTime;
accumulator+=deltaTime;

while (accumulator >= step) {

//SAVE LAST BODY POSITION AND ANGLE
lastPosition.x=body.getPosition().x;
lastPosition.y=body.getPosition().y;
lastAngle=body.getAngle();

world.step(step, 8, 3);
accumulator -= step;
}

//SAVE CURRENT BODY POSITION AND ANGLE
currentPosition= new Vector2(body.getPosition().x,body.getPosition().y);
currentAngle=body.getAngle();

alpha=(float) (accumulator/step);

position.x = lastPosition.x + (currentPosition.x - lastPosition.x) * alpha;
position.y = lastPosition.y + (currentPosition.y - lastPosition.y) * alpha;

sprite.setPosition(position.x * PixelsToMeters - BODY_WIDTH/2, position.y * PixelsToMeters
-BODY_HEIGHT/2);
sprite.setRotation((float)Math.toDegrees(lastAngle+(currentAngle-lastAngle)*alpha));

Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY(),
sprite.getOriginX(), sprite.getOriginY(),
sprite.getWidth(), sprite.getHeight(), sprite.getScaleX(),
sprite.getScaleY(), sprite.getRotation());
batch.end();
}

@Override
public void dispose() {
world.dispose();
batch.dispose();
img.dispose();
super.dispose();
}
}

Return to “Java”



Who is online

Users browsing this forum: Bing [Bot] and 2 guests