本文介绍了无法检测libgdx中是否触摸了矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上面带有精灵的矩形,我必须检测触摸位置是否在矩形内.

I have a rectangle with a sprite on it and I have to detect if the touch position lies within the rectangle.

这是我的代码,

    if (Gdx.input.isTouched())
    {
        int x1 = Gdx.input.getX();
        int y1 = Gdx.input.getY();
        Vector3 inputs = new Vector3(x1, y1, 0);
        gamecam.unproject(inputs);
        Gdx.app.log("x" + inputs.x, "y" + inputs.y);
        Gdx.app.log("rect" + rectangle.x, "rect" + rectangle.y);
        if(rectangle.contains(inputs.x,inputs.y))
        {
            Gdx.app.log("x" + inputs.x, "y" + inputs.y);
            Gdx.app.log("rect" + rectangle, "rect" + rectangle.y);
        }
    }

矩形定义

    BodyDef bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    b2body = screen.getWorld().createBody(bdef);

    rectangle = new Rectangle();
    rectangle.setHeight(55);
    rectangle.setWidth(55);
    PolygonShape head = new PolygonShape();
    rectangle.setX(300);
    rectangle.setY(10);
    bdef.position.set((rectangle.getX() - rectangle.getWidth() / 2) / MyJungleGame.PPM, (rectangle.getY() - rectangle.getHeight() / 2) / MyJungleGame.PPM);
    head.setAsBox(rectangle.getWidth() / 2 / MyJungleGame.PPM, rectangle.getHeight() / 2 / MyJungleGame.PPM);
    FixtureDef fdef = new FixtureDef();
    fdef.shape = head;
    setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);

这是我的输出

在屏幕底部的小矩形是我创建的矩形.但是,当我单击它时没有任何反应.我检查了坐标,这是日志,

The small rectangle at the bottom of the screen is the rectangle I created. But, nothing happens when I click it. I checked the coordinates and here is the log,

  x-0.925: y-0.5625
  rect300.0: rect10.0
  x-0.925: y-0.5625
  rect300.0: rect10.0
  x-0.925: y-0.5625

我尝试使用以下方法检查触摸,

I tried checking the touch using the below method,

        if (inputs.x > sprite.getX() && inputs.x < sprite.getX() + sprite.getWidth())
        {
            if (inputs.y > sprite.getY() && inputs.y < sprite.getY() + sprite.getHeight())
            {
                Gdx.app.log("sprite touched", "");
            }
        }

这也不起作用.知道我在哪里弄错了吗?请帮忙 .预先感谢

This too doesn't work. Any idea where I made the mistake ? Please help . Thanks in advance

推荐答案

由于您使用的是Box2D,因此对于新用户而言,通过 common 方法检测冲突的方法更加复杂.但是,查看您的代码...我建议您将这个坐标与您的世界的PPM一起考虑:

Since you are using Box2D, to detect collisions via the common way is more complicated to new users.However, looking on your code...I would advice taking this coordinates in consideration with PPM of your world :

int x1 = Gdx.input.getX();
int y1 = Gdx.input.getY();
Vector3 inputs = new Vector3(x1, y1, 0);

此外,如果要使用box2d构建碰撞系统,则应使用此方法: http://www.aurelienribon.com/blog/2011/07/box2d-tutorial-collision-filtering/

Also, If you are going to build a collision system with box2d, you should use this : http://www.aurelienribon.com/blog/2011/07/box2d-tutorial-collision-filtering/

这篇关于无法检测libgdx中是否触摸了矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:49