Box 2D surprise - Gideros Forum

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Box 2D surprise

Member
edited December 2012
Today i decided to replace sprite:hitTestPoint () with more interesting box2d feature - b2.World:queryAABB(lowerx, lowery, upperx, uppery) and here the surprise i got. For point A /look at the diagram/ i have correct return - a nil but for point B i have incorrect return - the fixture next to the point. Black lines are hint where incorrect results happen. b2 query is done over a box not the actual fixture. Any suggestions or i did something wrong?

Likes: zer0flag

Tagged:
+1 -1 (+1 / -0 ) Share on Facebook

• Maintainer
@plamen well as you said b2 queries a box and not fixture, thus if you have provided queryAABB points to include your circle dimensions (drawing a box around circle) than point B is right, it is inside the queryAABB limits. Or I misunderstood something?
• Member
edited December 2012
The box i provided was 4 by 4 pixels

function OnTouchesEnd(e)
gx,gy=stage:globalToLocal(e.x,e.y)
pickfixtures=cloud_world:queryAABB(gx-2, gy-2, gx+2, gy+2)
print(pickfixtures[1])
end
• Member
Here is B2 description of query function: Query the world for all fixtures that potentially overlap the provided AABB.
• Member
I guess this will explain the problem : http://www.iforce2d.net/b2dtut/world-querying
It seems the description is a bit misleading. Now i need some other way to check if click is inside the fixture.
• Maintainer
edited December 2012
So it's a square area around fixture, not the fixture itself. Well that's disappointing.
Can you use TNT Collision engine and check for a point inside figure?
• Maintainer
@plamen I think your body/fixture/AABB is rotated. Therefore, A is inside, but B is not (just a rough guess).
• Maintainer
hmm.. I realized my guess is not consistent with http://www.iforce2d.net/b2dtut/world-querying

Also you can enable AABB drawing with:
 `debugDraw:setFlags(b2.DebugDraw.SHAPE_BIT + b2.DebugDraw.AABB_BIT)`
• Member
@atilim ill try this one but its clear what happens. The original idea have been obviously to do rectangular selection and check for fixtures inside. For this approach actual shape of fixture and bounding box of the fixture return same results because your selection shape is always rectangular. If you can use lasso instead of AABB then you will need to check for actual fixture shape.
And i used points to show where things work /its my bad/ actually they are squares 4 by 4 pixels but it doesn't make any difference.
• Member
edited December 2012
Anyway i found working solution with couple extra lines of code.
• Maintainer
@plamen do tell, very interested
• Member
@ar2rsawseen: The goal was to find if i click on object on screen, enemy airship or cloud island etc. hitTestPoint was my method of choice but it needs Lua iteration of the objects. Box2D will do iteration for me using C-compiled code much faster and will return to me a fixture so i decided to swap Lua iteration with box2d function. The other part of the story you already know. Now when i have the fixture i have the body so i can do a simple radial check it works just fine for me. I just need simple circular area to use for tap and assign the objects as primary targets of the airship.
• Maintainer
@plamen ok I thought that there was more general solution, not tied to specific shape.

So the performance difference is noticeable?
• Member
@ar2rsawseen: I am trying to keep things simple so i am not looking for universal solution. But for large number of objects performance difference is noticeable. I don't think i can afford more realism /finding more complex shapes/ if i try to support Ipod 4th gen as my weakest platform. My game have to be very simple. Just lots of shooting and explosions...Thinking of it...Do i have to include some naked girls driving fast cars cut scenes between levels :-?
• Guru
Hey Plamen, have you looked into GregBUG's C++ plugin for Gideros (TNT Collision Engine). I've tested it and it is very fast. Especially if you need a large number of objects on screen. And even I could get it working.

http://www.giderosmobile.com/forum/discussion/1939/tnt-collision-engine-released- :-)
• Member
@Tom2012 thanks ill have a look. for now i use box2d for collisions as part of the dynamic simulations