14.3 C
New York
Thursday, October 9, 2025

java – Objects transfer with completely different pace on completely different gadgets


I used to be making a 2nd top-down recreation, however once I examined it on two gadgets (Samsung M31 and Samsung S25) there was an issue with participant pace – it was sooner on S25 and slower on M31. I attempted utilizing some mounted steps and delta issues, however nothing labored.
Thanks for any assist!
This is some code:
render model 1)

@Override
    public void render(float delta) {
        world.step(1f/60f, 6, 2);
        updateGameLogic();
        renderGame();
    }

render model 2:

static remaining float STEP_TIME = 1f/300f;
float accumulator = 0;
...
@Override
    public void render(float delta) {
        accumulator += Math.min(delta, 0.25f);

        whereas (accumulator >= STEP_TIME) {
            accumulator -= STEP_TIME;
            updateGameLogic();
            world.step(STEP_TIME, 6, 2);
        }
        renderGame();
    }

updateGameLogic()

non-public void updateGameLogic() {
        if (!actMenu && !actVending)
            place.lerp(new Vector2(participant.getX(), participant.getY()), 0.1f); // 0.1f - digicam smoothing coefficient
        digicam.place.set(place, 0);
        digicam.replace();

        up = Gdx.enter.isKeyPressed(Enter.Keys.W) || Gdx.enter.isKeyPressed(Enter.Keys.UP);
        down = Gdx.enter.isKeyPressed(Enter.Keys.S) || Gdx.enter.isKeyPressed(Enter.Keys.DOWN);
        left = Gdx.enter.isKeyPressed(Enter.Keys.A) || Gdx.enter.isKeyPressed(Enter.Keys.LEFT);
        proper = Gdx.enter.isKeyPressed(Enter.Keys.D) || Gdx.enter.isKeyPressed(Enter.Keys.RIGHT);

        assault = Gdx.enter.isKeyPressed(Enter.Keys.SPACE) || Gdx.enter.isKeyPressed(Enter.Keys.ENTER);
        menu = Gdx.enter.isKeyJustPressed(Enter.Keys.ESCAPE) || Gdx.enter.isKeyJustPressed(Enter.Keys.BACK);

        menuX = place.x - SCR_WIDTH / 4;
        menuY = place.y - SCR_HEIGHT / 3;

        vendingX = place.x - SCR_WIDTH / 4;
        vendingY = place.y - SCR_HEIGHT / 4;

        // contact handler

        touchHandler();

        // occasions

        if (deathTime == 0 && !actMenu && !actVending) {
            participant.changePhase();
            changePhaseGhosts();
            changePhaseZombies();
            changePhaseWardens();
            changePhaseAnimatedObstacles();
            participant.step(actJoystick);
            participant.updateProjectiles();
            updateProjectilesWardens();
            //            participant.updateMeleeRegion();
            //            meleeRegionUpdate();
            updateGhosts();
            updateZombies();
            updateWardens();
            updateDoors();
            updateCoins();
            updateChests();
            updateLevel();
            updateVending();
            updateButtons();
        }
    }

renderGame():

non-public void renderGame() {
        // draw
        ScreenUtils.clear(0, 0, 0, 0);
        //        debugRenderer.render(world, digicam.mixed);
        batch.setProjectionMatrix(digicam.mixed);
        rayHandler.setCombinedMatrix(digicam.mixed, digicam.place.x, digicam.place.y, digicam.viewportWidth, digicam.viewportHeight);
        digicam.replace();

        batch.start();
        ...
        batch.finish();

        playerLight.attachToBody(participant.getBody());
        rayHandler.updateAndRender();
        destroyScheduledBodies();

touchHandler():

non-public void touchHandler() {
        actJoystick = false;
        actAttack = false;

        float playerSpeed = participant.getSpeed();
        float playerDiagSpeed = playerSpeed / (float) Math.sqrt(2);

        ArrayList touches = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            touches.add(new Vector3());
        }

        for (int i = 0; i < 3; i++) {
            if (Gdx.enter.isTouched(i)) {
                touches.get(i).set(Gdx.enter.getX(i), Gdx.enter.getY(i), 0);
                digicam.unproject(touches.get(i));

                if (touches.get(i).x < participant.getX()) {
                    actJoystick = true;
                    joystick.updateKnob(touches.get(i));
                    if (participant.isShopping()) {
                        participant.setShopping(false);
                    }
                } else if (touches.get(i).x > participant.getX()) {
                    if (btnAttack.hit(touches.get(i).x, touches.get(i).y)) {
                        actAttack = true;
                    }
                }
            }
        }

        if (elevatorUseTime != 0 || actMenu || actVending) {
            actJoystick = false;
            actAttack = false;
        }

        if (actJoystick) {
            Vector2 directionVector = joystick.getDirectionVector();
            participant.getBody().setLinearVelocity(
                directionVector.x * participant.getSpeed(),
                directionVector.y * participant.getSpeed()
            );

            if (Math.abs(directionVector.x) > Math.abs(directionVector.y)) {
                if (directionVector.x > 0) participant.setDirection('r');
                else participant.setDirection('l');
            } else {
                if (directionVector.y > 0) participant.setDirection('u');
                else participant.setDirection('d');
            }
        } else {
            joystick.resetKnob();
            participant.getBody().setLinearVelocity(0, 0);
        }

        if (actAttack || assault) participant.assault();
        if (menu) {
            actMenu = !actMenu;
            uiInput.setButtons(hudButtons);
            if (actMenu) uiInput.setButtons(menuButtons);
        }

        if (up) {
            participant.setDirection('u');
            participant.getBody().setLinearVelocity(
                participant.getBody().getLinearVelocity().x,
                playerSpeed
            );
        } else if (down) {
            participant.setDirection('d');
            participant.getBody().setLinearVelocity(
                participant.getBody().getLinearVelocity().x,
                -playerSpeed
            );
        } else if (proper) {
            participant.setDirection('r');
            participant.getBody().setLinearVelocity(
                playerSpeed,
                participant.getBody().getLinearVelocity().y
            );
        } else if (left) {
            participant.setDirection('l');
            participant.getBody().setLinearVelocity(
                -playerSpeed,
                participant.getBody().getLinearVelocity().y
            );
        }
        if (up && proper) {
            participant.setDirection('r');
            participant.getBody().setLinearVelocity(
                playerDiagSpeed,
                playerDiagSpeed
            );
        } else if (up && left) {
            participant.setDirection('l');
            participant.getBody().setLinearVelocity(
                -playerDiagSpeed,
                playerDiagSpeed
            );
        } else if (down && proper) {
            participant.setDirection('r');
            participant.getBody().setLinearVelocity(
                playerDiagSpeed,
                -playerDiagSpeed
            );
        } else if (down && left) {
            participant.setDirection('l');
            participant.getBody().setLinearVelocity(
                -playerDiagSpeed,
                -playerDiagSpeed
            );
        }
    }

Entity:

public class Entity {
    non-public Physique physique;
    non-public float width, top;
    non-public float pace = 50f;
    protected int well being, maxHealth;
    protected char path = 'd';
    non-public int part, nPhases;
    protected lengthy timeLastPhase, timePhaseInterval, timeBasePhaseInterval;
    non-public boolean isAlive;
    protected boolean isBattle;

    public Entity(World world, float width, float top, float x, float y, int maxHealth, int nPhases, lengthy timeBasePhaseInterval) {
        part = 0;
        this.nPhases = nPhases;
        this.timeBasePhaseInterval = timeBasePhaseInterval; // besides operating animation!
        this.timePhaseInterval = timeBasePhaseInterval;
        this.width = width;
        this.top = top;
        this.well being = this.maxHealth = maxHealth;
        this.isAlive = true;

        BodyDef bodyDef = new BodyDef();
        bodyDef.sort = BodyDef.BodyType.DynamicBody;
        bodyDef.place.set(x, y);
        bodyDef.fixedRotation = true;

        physique = world.createBody(bodyDef);

        PolygonShape form = new PolygonShape();
        form.setAsBox(this.width / 2, this.top / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.form = form;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.4f;

        physique.createFixture(fixtureDef);

        form.dispose();
    }

    protected void changePhase(){
        if (isBattle) timePhaseInterval = timeBasePhaseInterval-250;
        else timePhaseInterval = timeBasePhaseInterval;

        if(TimeUtils.millis() > timeLastPhase+timePhaseInterval) {
            if (++part == nPhases) part = 0;
            timeLastPhase = TimeUtils.millis();
        }
    }

    public void hit(int injury) {
        health-=injury;
        if (well being<=0) {
            isAlive = false;
            well being = 0;
        }
    }
```

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles