从收集检测触摸的物体(移动)的libgdx的最佳方式物体、方式、libgdx

2023-09-06 16:18:37 作者:隐姓埋名

这是在游戏开发我的第一次尝试。我刚开始尝试libgdx和了解游戏编程的各个方面。我看了看示例项目,我可以理解libgdx游戏的整体架构。但要弄个游戏动态的基础知识,我开始想怎么画简单的形状,怎么动他们,如何处理这样的碰撞低层次的东西玩。

所以,我打算写一个死的简单的android游戏(它甚至不是一个游戏肯定)。这是观念

  1。创建随机的形状,并使其飞(移动)
2.当用户触摸造型,它会爆炸或隐藏或玩简单的动画
3.必须证明命中放大器;吴数
 

起初,我想过要尝试libgdx阶段和放大器;演员概念,但排除了做没有现场的API。而我开始尝试基本的游戏的不同方面,更好地理解其背后libgdx的概念。所以我做了这个简单的应用程序,我能够使物体掉落随机的。

 公共类A1GameScreen实现屏幕{

    OrthographicCamera摄像头;
    ShapeRenderer debugRenderer =新ShapeRenderer();
    阵列<矩形>盒;
    长lastFlew;
    INT fliesCaught;

    A1GameScreen(){

         摄像头=新OrthographicCamera();
         camera.setToOrtho(假的,800,480);
         盒=新的Array<矩形>();
         throwboxes();
    }
@覆盖
公共无效渲染(浮动三角洲){

    Gdx.gl.glClearColor(0,0,0.2F,1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


          camera.update();


    debugRenderer.setProjectionMatrix(camera.combined);
    debugRenderer.begin(ShapeType.Line);

    对于(矩形飞:框){
        debugRenderer.rect(fly.x,fly.y,fly.width,fly.height);
    }
    debugRenderer.end();

    //处理用户输入

    如果(Gdx.input.isTouched()){
        打(Gdx.input.getX(),Gdx.input.getY());
    }

    如果(TimeUtils.nanoTime() -  lastFlew> 10亿)
        throwboxes();


    迭代器<矩形> ITER = boxes.iterator();
    而(iter.hasNext()){
        矩形蝇= iter.next();
        fly.x  -  = 2;
        如果(fly.x + 32℃,)
            iter.remove();

    }

}

//方法创建苍蝇随意间隔
私人无效throwBoxes(){

    矩形飞=新的Rectangle();
    fly.y = MathUtils.random(0,480  -  32);
    fly.x = 800;
    fly.width = 32;
    fly.height = 32;
    boxes.add(飞);

    lastFlew = TimeUtils.nanoTime();

}

私人布尔命中(浮X,浮动Y){
    布尔发现= FALSE;

    对于(矩形飞:框){
        实测值= fly.contains(X,Y);
        如果(发现){
            发现= TRUE;
            fly.width = 100;
            打破;
        }


    }

    返回发现;

}
 
物体识别屏交互系统,触摸屏物体识别屏到底是怎么实现的

}

但我没能找出被触摸的项目从高空坠物。这是我在做什么,找出箱子是否在触及范围

循环遍历数组中的所有框 检查触摸坐标落在箱子坐标中 在我通过触摸坐标为包含矩形(盒)的方法来弄清楚,

像这样

 的(矩形飞:框){
        实测值= fly.contains(X,Y);
        如果(发现){
            发现= TRUE;
            fly.width = 100;
            打破;
        }


    }
 

但它不工作。我想我的问题。它

箱是2px的移动X轴每一帧,以使飞行效果 但我假设,某些帧以来的触摸事件过去了。这就是为什么我没有得到预期的结果

我的问题是

如何解决这个问题呢? 最新最好的方式来检测在libgdx碰撞?

更新

我看到很多触摸坐标和箱坐标之间的不匹配。没有在触摸范围的框。怎么样了这一点。下面的示例跟踪

 触摸坐标:的x 651.0 Y型362.0

飞COORDS:X-384.0 Y型277.0
飞COORDS:X-504.0 Y型34.0
飞COORDS:X-624.0 Y型103.0
飞COORDS:X-744.0 Y型238.0



触摸坐标:的x 441.0 Y型193.0

飞COORDS:X-52.0 Y型34.0
飞COORDS:X-172.0 Y型103.0
飞COORDS:X-292.0 Y型238.0
飞COORDS:X-414.0 Y型261.0
飞COORDS:X-534.0 Y型109.0
飞COORDS:X-656.0 Y型283.0
飞COORDS:X-776.0 Y型323.0


触摸坐标:的x 568.0 Y型162.0

飞COORDS:X-42.0 Y型267.0
飞COORDS:X-162.0 Y型166.0
飞COORDS:X-282.0 Y型266.0
飞COORDS:X-404.0 Y型52.0
飞COORDS:X-526.0 Y型296.0
飞COORDS:X-646.0 Y型64.0
飞COORDS:X-766.0 Y型16.0
 

解决方案

 公共静态布尔pointInRectangle(矩形R,浮法X,浮动Y){
    返回r.x&其中; = X&安培;&安培; r.x + r.width> = X&安培;&安培; r.y< = Y&功放;&安培; r.y + r.height> = Y;
}
 

在您的更新 -

 如果(pointInRectangle(flyRectangle,Gdx.input.getX(),Gdx.input.getY())){
  //做你想要做的矩形。也许登记他们的影响
}
 

您也可以看看部门间类。

现在的碰撞,如果你的游戏是快节奏的,有很多的敌人走动,玩家可以与碰撞,你迟早会使用Box2D的类型库,因为如果移动速度较高时,你可能无法得到的任何碰撞回调。事情可能要经过对方。您可以尝试predicting碰撞发生利用速度和deltaTime之前,但它仍然不会是足够的,你最终会重新发明轮子。

马里奥的SuperJumper是一个伟大的演示开始libGDX。尝试一下。

编辑:

有一个实例成员 -

  Vector3类型的接触点;
 

在创建 -

 接触点=新Vector3类型();
 

在更新 -

  camera.unproject(touchPoint.set(Gdx.input.getX(),Gdx.input.getY(),0));

如果(Gdx.input.justTouched()){
    如果(pointInRectangle(矩形,touchPoint.x,touchPoint.y)){
    }
}
 

请注意坐标系的libGDX。为了进行测试,创建屏幕上的一个四边形。在点击,打印/调试两个矩形和接触点的坐标。

This is my first attempt in game development. I just started experimenting libgdx and understanding the different aspects of game programming. I looked at the sample projects, i can understand the overall architecture of the libgdx game. But to get hold of the basics of game dynamics, i started playing with low level stuff like how to draw simple shapes, how to move them, how to handle collision like that.

So i planned to write a dead simple android game(Its not even a game for sure). This is the idea

1. Create random shapes and make it fly (move)
2. When user touches the shape, it ll explode or hide or play simple animation
3. Has to show Hit & Miss count

Initially i thought of going to try libgdx stage & actor concept, but ruled out that to do without the scene API. And I started this to experiment with different aspects of basic gaming and better understand the concepts behind libgdx. So i made this simple app, i am able to make objects fall random.

public class A1GameScreen implements Screen {

    OrthographicCamera camera;
    ShapeRenderer debugRenderer = new ShapeRenderer();
    Array<Rectangle> boxes;   
    long lastFlew;
    int fliesCaught;

    A1GameScreen(){

         camera = new OrthographicCamera();
         camera.setToOrtho(false, 800, 480);       
         boxes=new Array<Rectangle>();
         throwboxes();
    }
@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


          camera.update();


    debugRenderer.setProjectionMatrix(camera.combined);
    debugRenderer.begin(ShapeType.Line);

    for (Rectangle fly : boxes) {
        debugRenderer.rect(fly.x, fly.y, fly.width, fly.height);
    }
    debugRenderer.end();

    //Handle the user input

    if (Gdx.input.isTouched()){          
        hit(Gdx.input.getX(),Gdx.input.getY());
    }

    if (TimeUtils.nanoTime() - lastFlew > 1000000000)
        throwboxes();


    Iterator<Rectangle> iter = boxes.iterator();
    while (iter.hasNext()) {
        Rectangle fly = iter.next();        
        fly.x -= 2;
        if (fly.x + 32 < 0)
            iter.remove();

    }

}

//Method to create flies at random interval
private void throwBoxes(){

    Rectangle fly = new Rectangle();
    fly.y = MathUtils.random(0, 480 - 32);
    fly.x = 800;
    fly.width = 32;
    fly.height = 32;
    boxes.add(fly);

    lastFlew = TimeUtils.nanoTime();

}

private boolean hit (float x, float y) {
    boolean found=false;

    for (Rectangle fly : boxes) {
        found = fly.contains(x,y);
        if (found){
            found = true;
            fly.width=100;
            break;
        }


    }

    return found;

}

}

But i couldn't able to find out the touched item from the falling objects. This is what i am doing to find out whether the box is in touched range

Loop through all the boxes in the array Check if the touch co-ordinates falls within the box co-ordinates I passed touch co-ordinates to contains method of the Rectangle (Box) to figure out that

something like this

  for (Rectangle fly : boxes) {
        found = fly.contains(x,y);
        if (found){
            found = true;
            fly.width=100;
            break;
        }


    }

But its not working. I think i figured the problem. Its

Box is moving 2px in x axis each frame, to make flying effect But i am assuming, some frames has passed since the touch event. Thats why i am not getting the expected result

My questions are

How to solve this issue? Whats the best way to detect collision in libgdx?

Update

I see lot of mismatch between the touch co-ordinates and box co-ordinates. None of the boxes in the touch range. Hows that possible. Below the sample trace

Touch coords: x-651.0 y-362.0

Fly coords: x-384.0 y-277.0
Fly coords: x-504.0 y-34.0
Fly coords: x-624.0 y-103.0
Fly coords: x-744.0 y-238.0



Touch coords: x-441.0 y-193.0

Fly coords: x-52.0 y-34.0
Fly coords: x-172.0 y-103.0
Fly coords: x-292.0 y-238.0
Fly coords: x-414.0 y-261.0
Fly coords: x-534.0 y-109.0
Fly coords: x-656.0 y-283.0
Fly coords: x-776.0 y-323.0


Touch coords: x-568.0 y-162.0

Fly coords: x-42.0 y-267.0
Fly coords: x-162.0 y-166.0
Fly coords: x-282.0 y-266.0
Fly coords: x-404.0 y-52.0
Fly coords: x-526.0 y-296.0
Fly coords: x-646.0 y-64.0
Fly coords: x-766.0 y-16.0

解决方案

public static boolean pointInRectangle (Rectangle r, float x, float y) {
    return r.x <= x && r.x + r.width >= x && r.y <= y && r.y + r.height >= y;
}

In your update-

if(pointInRectangle(flyRectangle, Gdx.input.getX(), Gdx.input.getY())){
  // Do whatever you want to do with the rectangle. maybe register them for effect
}

You can also look into Intersector class.

Now for collision, if your game is fast-paced, with lots of enemies moving around that the player can collide with, sooner or later you will use a box2d type library because if the movement speed is high, you might not get any collision callback. Things might go through each other. You can try predicting the collision before it happens using the velocity and deltaTime, but it's still not going to be enough and you will end up reinventing the wheel.

Mario's SuperJumper is a great demo to start libGDX. Try it.

EDIT:

Have an instance member-

Vector3 touchPoint;

On create-

touchPoint = new Vector3();

On update-

camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

if (Gdx.input.justTouched()) {
    if (pointInRectangle(rectangle, touchPoint.x, touchPoint.y)) {
    }
}

Please take note of the coordinate system in libGDX. For testing, create one rectangle on screen. On click, print/debug the coordinates of both the rectangle and touchPoint.

 
精彩推荐
图片推荐