gpt4 book ai didi

java - Libgdx 的世界单位

转载 作者:行者123 更新时间:2023-11-30 06:03:30 25 4
gpt4 key购买 nike

我一直在努力学习 libgdx,但现在我对那里的世界单位有点困惑,我什至不知道我应该问什么确切的问题。

无论如何,我一直在阅读这个示例游戏代码here据我了解,当没有相机时,SpriteBatch渲染与设备分辨率相关的所有内容,例如像素,但是当我们制作相机时,设置它的“大小”、位置然后使用 batch.setProjectionMatrix(camera.combined) , batch translates pixels to units and then it knows where to render , 但在这个游戏中有 Rectangle 之间的碰撞检测对象,以及当您设置此 Rectangle 的位置时与 rect.set(x, y, 1, 1);其中 xy是世界单位而不是像素,矩形如何知道它是否应该使用那些 xy如果没有像 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();

一切都很好,ShapeRenderer设置 ShapeRenderer 的投影矩阵后,圆圈应该在的位置 shapeRenderer.setProjectionMatrix(camera.combined) : enter image description here

但是,我仍然不明白如何检查这个圆圈上的碰撞,当我在渲染方法中执行此操作并按下圆圈时,什么也没有发生,就像我没有得到那个日志,我认为那是因为圆圈不知道世界规模(没有任何东西像 setprojectionmatrix )和渲染坐标以及实际圆“认为”它不匹配的地方。我如何检查该圆上的碰撞?

if(Gdx.input.justTouched()) {

if(circle.contains(Gdx.input.getX(), Gdx.input.getY())) {

Gdx.app.log("SCREEN", "TOUCHED AND CONTAINS");
}
}

EDIT2:我现在明白了,一切正常,我只是没有使用 camera.unproject在触摸坐标上

最佳答案

假设您有一台 1200 x 800 像素的设备。

现在你说你将在位置上有一个 box2d 矩形:100、100 和大小:100,100

当您现在使用 SpriteBatch (Box2dDebugRenderer) 渲染它时,您将看到一个 1200 x 800 单位大的世界。您必须尝试忘记以像素为单位进行思考。重要的是 SpriteBatch 总是绘制 1200 x 800 像素但并不总是 1200 x 800 单位,稍后更多。

这就是你所看到的:

enter image description here

当我们现在使用相机时,我们说我们将只有 600 x 400 单位的世界。
我们创建了一个尺寸为:600 x 400 units 的相机!

camera = new OrthographicCamera(600, 400);

enter image description here

批处理仍然绘制 1200 x 800 单位和 1200 x 800 像素

现在使用 batch.setProjectionMatrix(camera.combined); 为批处理提供一个矩阵来计算单元的大小。有了这个矩阵,他可以计算出它必须绘制 1 个单位的大小。
之后,SpriteBatch 绘制了 600 x 400 个单位,但仍然是 1200 x 800 像素。
然后你会看到:

enter image description here

因此,您可能会忘记以像素为单位进行思考,相机矩阵会为您进行从像素到单位的计算,您可以专注于以单位为单位进行思考。

接下来你必须考虑单位的原因是 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 以像素为单位返回位置。
现在我们的相机有两种方法可以将像素位置转换为单位位置,反之亦然。

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

希望能帮到你

关于java - Libgdx 的世界单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51993577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com