По какой-то причине в моем мире физическое направление инвертировано. Я имею в виду -9.8 гравитация действует как 9.8 гравитации, поэтому объекты движутся вверх. Горизонтальная скорость также инвертирована. Что-то должно быть не так с системой координат x, y.
world = World (Vector2 (0f, -9.8f), false) толкает объекты вверх
есть ли проблема с PPM?
package com.mygdx.game
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.physics.box2d.*
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.math.Vector3
import com.badlogic.gdx.physics.box2d.World
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer
class MyGdxGame : ApplicationAdapter() {
val PPM = 32f
private lateinit var camera : OrthographicCamera
private lateinit var world :World
private lateinit var player:Body
private lateinit var platform:Body
private lateinit var b2dr:Box2DDebugRenderer
override fun create() {
var w = Gdx.graphics.width
var h = Gdx.graphics.height
camera = OrthographicCamera()
camera.setToOrtho(false, w / 2f, h / 2f)
world = World(Vector2(0f,-9.8f),false)
b2dr = Box2DDebugRenderer()
player = createBox(2f,10f,32,32,false)
platform = createBox(0f,0f,64,32,true)
}
override fun render() {
update(Gdx.graphics.deltaTime)
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
b2dr.render(world,camera.combined.scl(PPM))
}
override fun resize(width: Int, height: Int) {
camera.setToOrtho(false, width / 2f, height / 2f)
}
override fun dispose() {
world.dispose()
b2dr.dispose()
}
fun update(delta:Float){
world.step(1/60f,6,2)
inputUpdate(delta)
cameraUpdate(delta)
}
fun inputUpdate(delta:Float){
var horizontalForce = 0f;
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
horizontalForce -=1
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
horizontalForce +=1
}
if(Gdx.input.isKeyJustPressed(Input.Keys.UP)){
player.applyForceToCenter(0f,300f,false)
}
player.setLinearVelocity(horizontalForce*5,player.linearVelocity.y)
}
fun cameraUpdate(delta:Float){
var position:Vector3 = camera.position
position.x = player.position.x * PPM // if you get box2d units multiply with ppm
position.y = player.position.y * PPM
camera.position.set(position)
camera.update()
}
fun createBox(x:Float,y:Float,width:Int,height:Int,isStatic:Boolean):Body{
var pBody:Body
var def:BodyDef
def = BodyDef()
if(isStatic){
def.type = BodyDef.BodyType.StaticBody
}
else{
def.type = BodyDef.BodyType.DynamicBody
}
def.position.set(x/PPM,y/PPM)
def.fixedRotation = true
pBody = world.createBody(def)
var shape:PolygonShape = PolygonShape()
shape.setAsBox(width/2/PPM,height/2/PPM) // takes from center this is 32x32 box
pBody.createFixture(shape,1.0f)
shape.dispose()//clean it
return pBody
}
}