3
\$\begingroup\$

I have various bodies rotated at some angle with the help of Box2d in LibGDX. What I want is to destroy the body when I click on it, but the problem is that I am not able to get the area definition of the body so that I can check whether my touch point lies inside the body. I tried using actor and its hit() method but it's working only if I don't rotate it. As far as I know, once I rotate the body, its bounds are not rotated. How can I check the Touch event in a Body?

\$\endgroup\$
2
  • \$\begingroup\$ Hi Jagdeep. I don't have any experiences with Box2D (I'm working with Slick2D right now) so I'm just gonna make a suggestion. In Slick2D, we have an object called the Shape, which defines the dimensions of a Sprite on the screen. For a rectangle, for example, we have four points, indicating the four vertices. In order to detect if the user clicked on one of the sprites, we check if the x an y points of the mouse click (or touch) are inside the points defined by the Shape. Maybe Box2D has a similar way to find out the objects' dimensions and position. \$\endgroup\$ Jan 7, 2013 at 12:14
  • \$\begingroup\$ Hi Lucas, I cant use rectangles for the touch events because I am rotating the bodies to a certain angle and rotating of rectangles along the bodies is not possible as I know. \$\endgroup\$ Jan 9, 2013 at 5:47

3 Answers 3

2
\$\begingroup\$

Create Box2d fixture for your touch (and attach it to body), set it to be a sensor, place it where your touch event occurred. From now on, you can check every body that collides with your sensor. And because you are using sensor, simulation will not respond to such collision(s), but will report them instead. Here's Box2d manual, look at chapter 6.3 about sensors.

\$\endgroup\$
0
\$\begingroup\$

You could assign a sprite to your box2d body.

Form example I created a circle dynamic body with a radius of 1f, than I assigned to it a sprite (a red ball) that fits exactly the box2d body. Now it simply checks if the touch lies into sprite width and height.

Example Code:

   private void checkInput () {
                   Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
                   camera.unproject(touchPos);
                             if (touchPos.x > sprite.getX() && touchPos.x < sprite.getX() + sprite.getWidth() && touchPos.y > sprite.getY() && touchPos < sprite.getY() + sprite.getHeight()) {
                body.setActive(false);
              world.destroyBody(body); 
}
                      }
\$\endgroup\$
0
\$\begingroup\$

User touches the body only if he touches some of Fixture's contained into this body. This mean, you can check every Fixture of Body using testPoit() method:

public class Player {
    private Body _body;

    public boolean isPointOnPlayer(float x, float y){
        for(Fixture fixture : _body.getFixtureList())
            if(fixture.testPoint(x, y)) return true;
        return false;
    }
}

Next, you need to create InputAdapter like this:

public class PlayerControl extends InputAdapter {
    private final Camera _camera;
    private final Player _player;
    private final Vector3 _touchPosition;

    public PlayerControl(Camera camera, Player player) {
        _camera = camera;
        _player = player;
        // create buffer vector to make garbage collector happy
        _touchPosition = new Vector3();
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // don't forget to unproject screen coordinates to game world
        _camera.unproject(_touchPosition.set(screenX, screenY, 0F));
        if (_player.isPointOnPlayer(_touchPosition.x, _touchPosition.y)) {
            // touch on the player body. Do some stuff like player jumping
            _player.jump();
            return true;
        } else
            return super.touchDown(screenX, screenY, pointer, button);
    }
}

And the last one - setup this processor to listen user input:

public class MyGame extends ApplicationAdapter {

    @Override
    public void create () {
    // prepare player and game camera
    Gdx.input.setInputProcessor(new PlayerControl(cam, player));
}

Read more about touch handling here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .