QueryAABB not calling ReportFixture method

General discussion about Box2D tutorials
Post Reply
pwitulski
Posts: 8
Joined: Sat Aug 17, 2013 4:48 pm

QueryAABB not calling ReportFixture method

Post by pwitulski »

Hello,

So I am at a point were I want to to use my mouse to interact with the dynamic bodies in my scene. Pretty common thing to do. I looked the reference on how it was done in the sample loader for cocos2d using obj-c and the box2d testbed with the cocos2d-x example which I am using. Now everything is pretty much together. I am using logs to debug and I can confirm I am touching the the screen so I know my ccTouchBegan method works.

Thats another that is different. In the cocos2d-x testbed they use
bool Box2DView::ccTouchBegan(Touch* touch, Event* event)
and in the cocos2d, obj-c, iphone example it uses
(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
but it shouldnt really matter since I just want it to use one touch at a time. In cocos2d-x they make a method called MouseDown(const b2Vec2& p) that uses the box2d code and it is call in the ccTouchBegan method in your specified scene.

Its all good until I reach this statement:

m_world->QueryAABB(&callback, aabb);

for whatever reason, QuertAABB is not calling the ReportFixture method in the query callback. This is the problem.

Edit: I placed a CCLog call inside of ReportFixture method thats how I know it doesn't make it inside there.

It doesn't matter if I use the TouchDownQueryCallback class in the sample loader or the QueryCallback class in the box2d testbed example.

The only real difference I can think of at the moment is that all the example code in cocos2d-x and the sample loader is using Box2D 2.2.1 and I updated mine in my project to 2.3.0 and maybe that is why QueryAABB is not calling ReportFixture. Is there a difference between the two versions?

Thank you,
Paul.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: QueryAABB not calling ReportFixture method

Post by iforce2d »

As the name suggests, ReportFixture will only be called if the AABB you are querying actually has any fixtures in it. Are you certain that you are converting touch locations from screen coordinates to physics world coordinates correctly?
pwitulski
Posts: 8
Joined: Sat Aug 17, 2013 4:48 pm

Re: QueryAABB not calling ReportFixture method

Post by pwitulski »

I didn't have much time to look into it this morning. But I think I located where my problem is. I did a log output of my aabb variable and for some reason the lowerbound and upperbound x coordinates are correct but the y-axis points seem to be inverted?? I'll look at it again this evening but I don't know why cocos2d-x has the coordinates starting from the left bottom corner and box2d from the top left corner or if its just my aabb variable is not calculating correctly with the y points inverted.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: QueryAABB not calling ReportFixture method

Post by iforce2d »

In the Box2D coordinates start from the bottom left. The UITouch given to you by ccTouchesBegan typically has y-values starting from the top when using locationInView (the exact outcome depends on how the view has been set up, but the default is zero at the top).

I think you just need to step through the code using the debugger and look at every variable along the way to check that the value is really what you're assuming it is. Logging is good too, but I find it less effort just to step through :)
pwitulski
Posts: 8
Joined: Sat Aug 17, 2013 4:48 pm

Re: QueryAABB not calling ReportFixture method

Post by pwitulski »

So I did find my problem this morning.

The small problem was the aabb lowerbound.y and upperbound.y calculation was wrong because I didn't have to recalculate it by taking the difference to the window hight which I was doing.

The real problem, which is sad that it was obvious, was that I was passing the query callback the world coordinates in pixels, which Box2D doesn't like LOL. :D Used my PTM_RATIO to calculate the correct position and it works now.

Sucks that the simplest problems are sometimes a bigger pain than complex ones. :roll:

..and yes I should get into a better habit of using the debugger and stepping through the code, it really does help and its easy to overlook as a novice programmer and not utilize it.
Post Reply