From: fredboy Date: Sun, 14 Apr 2024 19:50:39 +0000 (+0700) Subject: Delta time in physics and animation X-Git-Tag: alpha0.4.1~7 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=834fd02f670fddc6857f2b5afabc3ae044143b32;p=cavedroid.git Delta time in physics and animation --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 3f9b773..09a7e70 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -67,7 +67,7 @@ public class GameInput { if (checkSwim()) { mPlayer.swim = true; } else if (mPlayer.canJump()) { - mPlayer.getVelocity().add(0, -7); + mPlayer.getVelocity().add(0, -180); } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) { mPlayer.setFlyMode(true); mPlayer.getVelocity().y = 0; diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 6572cf5..cb5b64c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -16,11 +16,11 @@ import java.util.Iterator; @GameScope -class GamePhysics { +public class GamePhysics { - static final int PL_SPEED = 2; + public static final int PL_SPEED = 120; - private final Vector2 gravity = new Vector2(0, .9f); + private final Vector2 gravity = new Vector2(0, .09f); private final GameWorld mGameWorld; private final MainConfig mMainConfig; @@ -91,7 +91,7 @@ class GamePhysics { (int) (rect.y + rect.height / 8 * 7) / 16); } - private void dropPhy(Drop drop) { + private void dropPhy(Drop drop, float delta) { int dropToPlayer = drop.closeToPlayer(mGameWorld, mMobsController.getPlayer()); if (dropToPlayer > 0) { drop.moveToPlayer(mGameWorld, mMobsController.getPlayer(), dropToPlayer); @@ -107,13 +107,13 @@ class GamePhysics { drop.getVelocity().y += gravity.y / 4; } } - drop.move(); + drop.move(delta); if (checkColl(drop)) { drop.getVelocity().set(0, -1); do { - drop.move(); + drop.move(delta); } while (checkColl(drop)); drop.getVelocity().setZero(); } @@ -175,7 +175,9 @@ class GamePhysics { mob.getVelocity().y = 0; } else { - mob.setCanJump(false); + mob.y += 1; + mob.setCanJump(checkColl(mob)); + mob.y -= 1; } if (mob.getY() > mGameWorld.getHeightPx()) { @@ -183,10 +185,7 @@ class GamePhysics { } } - private void playerPhy(Player player) { - player.y += player.getVelocity().y; - mobYColl(player); - + private void playerPhy(Player player, float delta) { if (player.isDead()) { return; } @@ -196,62 +195,65 @@ class GamePhysics { player.swim = true; } if (!player.swim) { - if (!player.isFlyMode() && player.getVelocity().y < 4.5f) { - player.getVelocity().add(gravity.x / 4, gravity.y / 4); + if (!player.isFlyMode() && player.getVelocity().y < 270f) { + player.getVelocity().x += gravity.y / 4; } - if (!player.isFlyMode() && player.getVelocity().y > 4.5f) { - player.getVelocity().add(0, -1f); + if (!player.isFlyMode() && player.getVelocity().y > 270f) { + player.getVelocity().add(0, -60f); } } else { - player.getVelocity().add(0, -.5f); - if (player.getVelocity().y < -3) { - player.getVelocity().y = -3; + player.getVelocity().add(0, -30f); + if (player.getVelocity().y < -180) { + player.getVelocity().y = -180; } } } else { - if (!player.isFlyMode() && player.getVelocity().y < 18) { + if (!player.isFlyMode() && player.getVelocity().y < 1080) { player.getVelocity().add(gravity); } } + player.y += player.getVelocity().y * delta; + mobYColl(player); + player.x += player.getVelocity().x * (player.isFlyMode() ? 1.5f : 1) * - (GameItems.isFluid(getBlock(player)) && !player.isFlyMode() ? .8f : 1); + (GameItems.isFluid(getBlock(player)) && !player.isFlyMode() ? .8f : 1) * delta; mobXColl(player); if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) { - player.getVelocity().add(0, -8); + player.getVelocity().add(0, -480); player.setCanJump(false); } } - private void mobPhy(Mob mob) { + private void mobPhy(Mob mob, float delta) { if (mob.getType() == Mob.Type.MOB && GameItems.isFluid(getBlock(mob))) { - if (mob.getVelocity().y > 9) { - mob.getVelocity().add(0, -.9f); + if (mob.getVelocity().y > 540) { + mob.getVelocity().add(0, -5.4f); } - mob.getVelocity().add(0, -.5f); + mob.getVelocity().add(0, -30f); - if (mob.getVelocity().y < -3) { - mob.getVelocity().y = -3; + if (mob.getVelocity().y < -180) { + mob.getVelocity().y = -180; } - } else if (!mob.isFlyMode() && mob.getVelocity().y < 18) { + } else if (!mob.isFlyMode() && mob.getVelocity().y < 1080) { mob.getVelocity().add(gravity); } - mob.y += mob.getVelocity().y; + mob.y += mob.getVelocity().y * delta; mobYColl(mob); if (mob.isDead()) { return; } - mob.x += mob.getVelocity().x; + mob.x += mob.getVelocity().x * delta; mobXColl(mob); if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) { - mob.getVelocity().add(0, -8); + mob.getVelocity().add(0, -480); mob.setCanJump(false); } } @@ -261,7 +263,7 @@ class GamePhysics { for (Iterator it = mDropController.getIterator(); it.hasNext(); ) { Drop drop = it.next(); - dropPhy(drop); + dropPhy(drop, delta); if (Intersector.overlaps(drop, player)) { drop.pickUpDrop(player); } @@ -272,14 +274,14 @@ class GamePhysics { for (Iterator it = mMobsController.getIterator(); it.hasNext(); ) { Mob mob = it.next(); - mob.ai(mGameWorld); - mobPhy(mob); + mob.ai(mGameWorld, delta); + mobPhy(mob, delta); if (mob.isDead()) { it.remove(); } } - playerPhy(player); + playerPhy(player, delta); if (player.isDead()) { player.respawn(mGameWorld); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 19f2681..c35b3b4 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -117,7 +117,7 @@ public class GameRenderer extends Renderer { } } - private void drawMob(Mob mob) { + private void drawMob(Mob mob, float delta) { float mobDrawX = mob.getX() - getCamX(); float mobDrawY = mob.getY() - getCamY(); @@ -129,7 +129,7 @@ public class GameRenderer extends Renderer { return; } - mob.draw(spriter, mobDrawX, mobDrawY); + mob.draw(spriter, mobDrawX, mobDrawY, delta); } private void drawDrop(Drop drop) { @@ -210,12 +210,12 @@ public class GameRenderer extends Renderer { } } - private void drawGamePlay() { + private void drawGamePlay(float delta) { Player player = mMobsController.getPlayer(); drawWorld(true); - player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY()); - mMobsController.forEach(this::drawMob); + player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY(), delta); + mMobsController.forEach( (mob) -> { drawMob(mob, delta); }); mDropController.forEach(this::drawDrop); drawWorld(false); drawGUI(); @@ -237,7 +237,7 @@ public class GameRenderer extends Renderer { spriter.begin(); - drawGamePlay(); + drawGamePlay(delta); switch (mMainConfig.getGameUiWindow()) { case CREATIVE_INVENTORY: diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index 359a74b..f4c62f9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -23,7 +23,7 @@ public class FallingGravel extends Mob { } @Override - public void ai(GameWorld gameWorld) { + public void ai(GameWorld gameWorld, float delta) { if (mVelocity.isZero()) { gameWorld.setForeMap(getMapX(), getMiddleMapY(), 11); kill(); @@ -35,7 +35,7 @@ public class FallingGravel extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { + public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { spriteBatch.draw(GameItems.getBlockTex(11), x, y); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index a8d9ce2..d124243 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -24,7 +24,7 @@ public class FallingSand extends Mob { } @Override - public void ai(GameWorld gameWorld) { + public void ai(GameWorld gameWorld, float delta) { if (mVelocity.isZero()) { gameWorld.setForeMap(getMapX(), getMiddleMapY(), 10); kill(); @@ -36,7 +36,7 @@ public class FallingSand extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { + public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { spriteBatch.draw(GameItems.getBlockTex(10), x, y); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 3bc3b5b..9f14a69 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -13,6 +13,8 @@ import java.io.Serializable; */ public abstract class Mob extends Rectangle implements Serializable { + protected static int ANIMATION_SPEED = 360; + public enum Type { MOB, SAND, @@ -26,8 +28,8 @@ public abstract class Mob extends Rectangle implements Serializable { protected Vector2 mVelocity; protected Type mType; - protected int mAnimDelta = 6; - protected int mAnim; + protected int mAnimDelta = ANIMATION_SPEED; + protected float mAnim; private Direction mDirection; private boolean mDead; @@ -120,7 +122,7 @@ public abstract class Mob extends Rectangle implements Serializable { return mDead; } - public final int getAnim() { + public final float getAnim() { return mAnim; } @@ -169,9 +171,9 @@ public abstract class Mob extends Rectangle implements Serializable { } } - public abstract void draw(SpriteBatch spriteBatch, float x, float y); + public abstract void draw(SpriteBatch spriteBatch, float x, float y, float delta); - public abstract void ai(GameWorld gameWorld); + public abstract void ai(GameWorld gameWorld, float delta); public abstract void changeDir(); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java index b2760c2..5742a7c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java @@ -22,7 +22,7 @@ public class Pig extends Mob { } @Override - public void ai(GameWorld gameWorld) { + public void ai(GameWorld gameWorld, float delta) { if (MathUtils.randomBoolean(.0025f)) { if (mVelocity.x != 0f) { mVelocity.x = 0; @@ -32,7 +32,7 @@ public class Pig extends Mob { } if (mVelocity.x != 0f) { - mAnim += mAnimDelta; + mAnim += mAnimDelta * delta; } else { mAnim = 0; } @@ -43,7 +43,7 @@ public class Pig extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { + public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { pigSprite[0][1].setRotation(getAnim()); pigSprite[1][1].setRotation(-getAnim()); //back legs diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index f373dc3..5cdb5d8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -2,6 +2,7 @@ package ru.deadsoftware.cavedroid.game.mobs; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector2; +import ru.deadsoftware.cavedroid.game.GamePhysics; import ru.deadsoftware.cavedroid.game.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; @@ -48,7 +49,7 @@ public class Player extends Mob { } @Override - public void ai(GameWorld gameWorld) { + public void ai(GameWorld gameWorld, float delta) { } @Override @@ -56,12 +57,14 @@ public class Player extends Mob { } @Override - public void draw(SpriteBatch spriteBatch, float x, float y) { - if (mVelocity.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) { - Assets.playerSprite[0][2].rotate(mAnimDelta); - Assets.playerSprite[1][2].rotate(-mAnimDelta); - Assets.playerSprite[0][3].rotate(-mAnimDelta); - Assets.playerSprite[1][3].rotate(mAnimDelta); + public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { + final float correctedAnimationDelta = mAnimDelta * delta; + + if (mVelocity.x != 0 || Math.abs(Assets.playerSprite[0][2].getRotation()) > Math.abs(correctedAnimationDelta)) { + Assets.playerSprite[0][2].rotate(correctedAnimationDelta); + Assets.playerSprite[1][2].rotate(-correctedAnimationDelta); + Assets.playerSprite[0][3].rotate(-correctedAnimationDelta); + Assets.playerSprite[1][3].rotate(correctedAnimationDelta); } else { Assets.playerSprite[0][2].setRotation(0); Assets.playerSprite[1][2].setRotation(0); diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java index 07a2f6c..e9f2e61 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java @@ -106,9 +106,9 @@ public class Drop extends Rectangle implements Serializable { // } } - public void move() { - x += velocity.x; - y += velocity.y; + public void move(float delta) { + x += velocity.x * delta; + y += velocity.y * delta; checkWorldBounds(); y = MathUtils.round(y); } diff --git a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java index 62b554b..cc0bb93 100644 --- a/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java +++ b/desktop/src/ru/deadsoftware/cavedroid/desktop/DesktopLauncher.java @@ -9,9 +9,10 @@ class DesktopLauncher { public static void main (String[] arg) { Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration(); config.setWindowIcon(Files.FileType.Internal, "icons/icon256.png", "icons/icon128.png"); - config.setForegroundFPS(144); +// config.setForegroundFPS(144); config.setTitle("CaveDroid"); config.setWindowedMode(960, 540); + config.useVsync(false); boolean touch = false; for (String anArg : arg) {