简体   繁体   English

如何在LibGdx中更改触摸形状的位置

[英]How to change the position of a shape on touch in LibGdx

basically I am making a game using LibGdx that will involve a ball and ground. 基本上,我正在使用LibGdx制作一个涉及球和地面的游戏。 The objective is to keep the ball in the air as long as possible by touching it. 目的是通过触摸使其尽可能长时间地保持在空中。

So far I have only used box2d and not put any sprites over the shapes. 到目前为止,我仅使用box2d而不在形状上放置任何精灵。 But I cant seem to get the touch working correctly. 但是我似乎无法正常工作。

Here's what I have so far in regards to this feature: 到目前为止,我对此功能的了解如下:

This is setting up a class that extends the Input processor as I only want to use the touchDown method. 这是在设置一个类来扩展Input处理器,因为我只想使用touchDown方法。

    Gdx.input.setInputProcessor(new InputController(){
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button){
            if((screenX == ball.getPosition().x) && (screenY == ball.getPosition().y)){
                movement.y = speed;                 
            }
            return true;                
        }

    });

Ball is a body which is set to the world of the ball shape. 球是设置为球形世界的物体。 Speed is a float variable. 速度是一个浮点变量。 I then have 然后我有

    ball.applyForceToCenter(movement, true);

In the render method movement is a vector 2 which is not defined until the touch where it should increase the y position by 500. 在渲染方法中,运动是矢量2,直到触摸它应该将y位置增加500的位置才定义。

Could anyone help as this didn't work :( 任何人都可以帮忙,因为这不起作用:(

Your first issue is that you're testing to see if the point clicked is exactly the position of the ball. 您的第一个问题是要测试以查看单击的点是否恰好是球的位置。 This is highly unlikely, and almost certainly not what you want. 这极不可能,而且几乎肯定不是您想要的。 Instead, you want to test to see if the point clicked hits anywhere in the ball. 相反,您想测试一下单击的点是否击中了球的任何位置。

Your second issue is that you need to transform your device coordinates to box2d coordinates. 第二个问题是您需要将设备坐标转换为box2d坐标。 The device and box2d use different coordinate systems, so comparing the two doesn't make sense. 设备和box2d使用不同的坐标系,因此比较两者是没有意义的。

To solve the first issue, you can loop through all of the fixtures in the body and use the method fixture.testPoint(x, y) to see if the point hits the fixture. 要解决第一个问题,您可以遍历体内的所有固定装置,并使用方法fixture.testPoint(x, y)来查看该点是否碰到了固定装置。 Since your body only has one fixture, you can just use this code for the test: 由于您的身体只有一个固定装置,因此您可以使用以下代码进行测试:

body.getFixtureList().get(0).testPoint(x, y);

Solving the second issue is a bit trickier. 解决第二个问题比较棘手。 Ideally, your game should be using a camera to handle rendering. 理想情况下,您的游戏应该使用摄像头来处理渲染。 If it doesn't, you should get one. 如果没有,您应该得到一个。 With the camera, you can unproject the device coordinates to get camera coordinates like this: 使用相机,您可以取消投影设备坐标以获取相机坐标,如下所示:

Vector3 mouseCoords = new Vector3(screenX, screenY, 0);
camera.unproject(mouseCoords);
//mouseCoords.x and mouseCoords.y will now be relative to the camera

Then, you have to convert to box2d coordinates: 然后,您必须转换为box2d坐标:

mouseCoords.scl(WORLD_TO_BOX);

where WORLD_TO_BOX is the scaling from world coordinates to box coordinates, which you should already have setup. 其中WORLD_TO_BOX是从世界坐标到框坐标的缩放比例,您应该已经设置了。 You can then use mouseCoords.x and mouseCoords.y in the testPoint method. 然后,可以在testPoint方法中使用mouseCoords.x和mouseCoords.y。

EDIT: Code below shows how to loop through all fixtures in a body. 编辑:以下代码显示了如何遍历身体中的所有固定装置。 This isn't necessary if you only have one fixture, however. 但是,如果只有一个灯具,则没有必要。

for(Fixture fixture : body.getFixtureList())
{
    //Do something to fixture
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM