我一直在尝试学习libgdx,但是现在我对那里的世界单位有些困惑,我什至不知道我应该问什么确切的问题。
无论如何,我一直在这里阅读示例游戏代码
,据我了解,没有摄像头时,SpriteBatch会渲染与设备分辨率相关的所有内容,因此会产生像素,但是当我们制作摄像头时,先设置其“大小”,位置,然后使用batch.setProjectionMatrix(camera.combined),批量转换像素单元,然后它知道在何处渲染,但在这场比赛中有之间的碰撞检测Rectangle的物体,当你的这个是设置位置Rectangle与rect.set(x, y, 1, 1);其中x和y是世界的单位,而不是像素,请问矩形知道它应该使用那些x和y作为单位,而不是像素,如果没有什么setProjectionMatrix可以让我们知道现在我们正在以单位而不是像素为单位,那么这就是1, 1);最后,它也是单位吗?如果是这样,那么Rectangle如何知道这些单位应该多大(例如游戏中的比例为1/16)renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);. 
我什至不知道这个问题是否真的有道理,但这就是我在这里,我真的在这里迷路了
编辑:好的,我知道单元现在是如何工作的,但是碰撞仍然使我有些困惑,在此示例中,我创建了这个
// onCreate
    mapSprite = new Sprite(new Texture(Gdx.files.internal("space.png")));
            planetTexture = new Texture(Gdx.files.internal("planet.png"));
            shapeRenderer = new ShapeRenderer();
           //Planet(X,Y,Radius)
        planet = new Planet(20, 30, 7);
        planet2 = new Planet(70, 50, 8);
        circle = new Circle(planet.getPosition().x + planet.radius, planet.getPosition().y + planet.radius, planet.radius);
        circle2 = new Circle(planet2.getPosition().x + planet2.radius, planet2.getPosition().y + planet2.radius, planet2.radius);
        mapSprite.setPosition(0,0);
        mapSprite.setSize(133, 100);
        stateTime = 0;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 133, 100);
        camera.position.set(133 /2, 100 /2, 0);
        camera.update();
        batch = new SpriteBatch();
...
// Render Method
batch.setProjectionMatrix(camera.combined);
        batch.begin();
        mapSprite.draw(batch);
        batch.draw(planetTexture, planet.getPosition().x, planet.getPosition().y, planet.radius * 2, planet.radius * 2);
        batch.draw(planetTexture, planet2.getPosition().x, planet2.getPosition().y, planet2.radius * 2, planet2.radius * 2);
        batch.end();
        shapeRenderer.setProjectionMatrix(camera.combined);
        shapeRenderer.setColor(Color.RED);
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.circle(circle.x, circle.y, circle.radius + 1);
        shapeRenderer.end();
Run Code Online (Sandbox Code Playgroud)
一切都呈现良好,ShapeRenderer设置了ShapeRenderer的投影矩阵后,圆应该是它应该位于的位置shapeRenderer.setProjectionMatrix(camera.combined):
但是,我仍然不知道如何检查该圆上的碰撞,当我在render方法中执行此操作并按圆时,什么也没有发生,就像我没有得到该日志,我认为那是因为圆没有知道世界比例(什么都没有setprojectionmatrix)并渲染坐标,而实际圆“认为”它的位置不匹配。如何检查该圆上的碰撞?
if(Gdx.input.justTouched()) {
    if(circle.contains(Gdx.input.getX(), Gdx.input.getY())) {
                    Gdx.app.log("SCREEN", "TOUCHED AND CONTAINS");
    }
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:我现在明白了,一切正常,我只是没有camera.unproject在触摸坐标上使用
假设您有一台1200 x 800像素的设备。
现在您说您将在位置上有一个box2d矩形:100、100和大小:100,100
现在,当您使用SpriteBatch(Box2dDebugRenderer)进行渲染时,您将看到一个1200 x 800单位大的世界。您必须尝试忘记以像素为单位思考。重要的是SpriteBatch始终绘制1200 x 800像素,但并不总是绘制1200 x 800像素,以后还会更多。
所以这是您看到的:
现在,当我们使用相机时,我们会说只有600 x 400单位的世界。
我们创建了一个摄像机,尺寸:600×400 台!
camera = new OrthographicCamera(600, 400);
Run Code Online (Sandbox Code Playgroud)
批次仍可绘制1200 x 800单位和1200 x 800像素
现在,batch.setProjectionMatrix(camera.combined);您可以为批处理添加一个矩阵以计算单位大小。有了这个矩阵,他可以计算多少可以画1个单位。
之后,SpriteBatch将绘制600 x 400单位,但仍为1200 x 800像素。
然后您会看到:
因此,您可以忘记以像素为单位思考,相机矩阵可以为您进行从像素到单位的计算,而您可以专注于以单位进行思考。
为什么必须以单位为单位的第二件事是box2D以米为单位进行计算。因此,矩形的尺寸为100 x 100米,底部上方100米。因此,由于重力g = 9,81m / s,矩形的下落速度并没有您预期的那么快。
SpriteBatch始终绘制1200 x 800像素,否则您只能在屏幕的一半看到游戏。但是使用摄像机矩阵时,批次知道他必须绘制1个像素的像素数,因此您在1200 x 800像素的屏幕上看到600 x 400单位的世界。
注意:1个单位并不总是等于1米。例如,在太空飞船游戏中,两艘船之间有5公里。因此,请不要创建10000 x 10000单位的世界,因为Box2d不会对此进行计算。
然后,您可以说1个单位= 100米,因此您的世界为100 x 100个单位。现在,您必须记住,当船舶的速度应达到200 m / s时,必须设置body.setLinearVelocity(2,0),因为1个单位= 100米。
因此,请始终以单位为单位进行思考!
编辑:
有时我们不会绕过像素,例如Gdx.input 
Gdx.input返回以像素为单位的位置。
现在,我们的相机有两种方法可以将单位像素的pos转换为单位的pos,反之亦然。
float x = Gdx.input.getX();
float y = Gdx.input.getY();
camera.unproject(new Vector3(x, y, 0));//this converts a Vector3 position of pixels to a Vector3 position of units
camera.project(new Vector3(10,10,0));//this converts a Vector3 position of units to a Vector3 position of pixels
Run Code Online (Sandbox Code Playgroud)
希望可以帮到您