
我正在开发像超级马里奥这样的简单平台游戏.我正在使用带有libGdx引擎的Java.我有一个物理问题,独立于帧率.在我的游戏中角色可以跳跃,跳跃高度显然取决于帧速率.
在我的桌面上游戏运行良好,它以每秒60帧的速度运行.我也尝试在平板电脑上以较低的fps运行游戏.发生的事情是,这个角色的跳跃速度远远高于我跳到桌面版本时的跳跃速度.
我已经阅读了一些关于修复时间步的文章,我理解它,但还不足以应用于这种情况.我似乎错过了一些东西.
这是代码的物理部分:
protected voID applyPhysics(Rectangle rect) { float deltaTime = Gdx.graphics.getDeltaTime(); if (deltaTime == 0) return; stateTime += deltaTime; veLocity.add(0, world.getGravity()); if (Math.abs(veLocity.x) < 1) { veLocity.x = 0; if (grounded && controlsEnabled) { state = State.Standing; } } veLocity.scl(deltaTime); //1 multiply by delta time so we kNow how far we go in this frame if(collisionX(rect)) collisionXAction(); rect.x = this.getX(); collisionY(rect); this.setposition(this.getX() + veLocity.x, this.getY() +veLocity.y); //2 veLocity.scl(1 / deltaTime); //3 unscale the veLocity by the inverse delta time and set the latest position veLocity.x *= damPing; dIEByFalling();}调用jump()函数并将一个变量jump_veLocity = 40添加到veLocity.y.
速度用于碰撞检测.
解决方法:
我认为你的问题在这里:
veLocity.add(0, world.getGravity());修改速度时还需要缩放重力.尝试:
veLocity.add(0, world.getGravity() * deltaTime);单独注意,尝试使用Box2D,它可以为你处理这些:)
总结以上是内存溢出为你收集整理的java – LibGdx物理独立于帧速率全部内容,希望文章能够帮你解决java – LibGdx物理独立于帧速率所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)