sprite body collision

hi,
I draw a sprite on fly and want to check if it touchs a body in the box2d world. Exists a method which checks that collision or how can I check it?? I thought about something like the GetBodyAtMouse() function. I would give a sprite to a function, the function checks if a touch exists and I get a boolean back.

regards
Hey, I see 3 solutions.

If your sprite is (roughly) a convex polygon, I think it would be easiest to create a b2Shape for it real quick, (as a Sensor) and then check for collisions with that (after a 0-time timestep, to trigger contact generation).

If that's too complicated, and if your sprite is roughly a box, (or can be appropriately represented by a box) you can do a b2World.Query, that will find all bodies within a given AABB (bounding box).

If your sprite is a complex/concave/irregular shape, and you need exact collisions, you're going to have a pretty hard time. You might resort to AS3's built in functions for testing overlap,
Code:
sprite.hitTestObject(otherSprite)
. If your other bodies are sprites too, this should work.

If you use this last method though, I would still start by using a b2World.Query, to find possible candidates for collision, Flash's HitTest functions are very slow because they compare actual graphics. So if you can narrow down the list of possible collisions as much as possible, that's going to improve your performance. (However, I realize now that Flash probably won't start comparing graphics if their bounding boxes don't overlap, so it might not even make that much difference).
thanks, for the answer!!

I think I will do it with the first possibility, the second and the third possibiltities just take the bounding box and are not exact enough!!

What I have to do when created the b2Shapes (Senors)?? How can I see a collision, and how I can see with which body it collide??

regards
You should implement a ContactListener, and see whether any contacts are generated between the shapes you are looking at. Contacts are generated by Box2D in each timestep, so you should run at least a single timestep. (During this timestep, your contactlistener will be called if any contacts are generated, then you can check if those contacts include the bodies that you are interested in).
thanks for the answer!!
The ContactListener works so far, but how can I get the bodies which collide?
Or how can I add the ContactListener just to the new generated bodies?

regards
sorry... stupid question!!

there is point.shape1/2
Just to point out, flash's built in hitTestObject method uses the bounds of each body for checks, not the actual shape. There is hitTestPoint which takes a point object, and a boolean. The boolean(if true) tells it to check the actual shape of the object. In this case though, you are testing a single pixel( which is really what happens, the vectors are rasterized to a lower resolution bitmap, then the point is checked. ) There is no way in flash to test actual shapes against shapes.
VengantMjolnir wrote:
Just to point out, flash's built in hitTestObject method uses the bounds of each body for checks, not the actual shape. There is hitTestPoint which takes a point object, and a boolean. The boolean(if true) tells it to check the actual shape of the object. In this case though, you are testing a single pixel( which is really what happens, the vectors are rasterized to a lower resolution bitmap, then the point is checked. ) There is no way in flash to test actual shapes against shapes.


Ahh that's what I thought actually, but seeing the function, I couldn't determine whether it tested actual shapes or not.

It seemed logical to me that it would test actual shapes, because that would be most useful in games.
Hi,

I created the shapes and the collision detection works well, but when the collision comes, the object moves!! is there a possibility to overlap shapes or set the mass to 0, that there come no movements?

regards
You can call Step() with 0 time, that way the objects won't move (I think).

Original thread: https://box2d.org/forum/viewtopic.php?t=3023