Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Lucas Tulio Jan 7 '13 at 12:14
    
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. –  Jagdeep Singh Jan 9 '13 at 5:47
add comment

2 Answers

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.

share|improve this answer
add comment

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); 
}
                      }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.