Box2D Forums

It is currently Sat May 25, 2013 8:47 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Jul 02, 2012 7:56 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
In my app (JR Chemistry Set: http://itunes.com/apps/jrchemistryset ) I have a Box2d game component where users can touch/move a bouncing ball (chemical element) and place them in a preset mold where a weld joint holds them in place.

I've had some user feedback complaining that the bouncing balls are "too hard to grab". For the most part I think they are simply un-used to touch/grab (i.e. inexperienced users) but still I guess I should try to accommodate them if it doesn't inhibit game play.

I am wondering how to increase the touch area of the bouncing balls so they are easier to "grab". Looking through my code it seems I might be able to achieve this in TouchBegan (included below).

Your thoughts? Do I just increase the size of "d" below?


- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint location = [self convertTouchToNodeSpace:touch];
int in_game = [[NSUserDefaults standardUserDefaults] integerForKey:@"CCLayer"];
if (in_game == 1) {
m_mouseWorld.Set(ptm(location.x), ptm(location.y));
if (m_mouseJoint != NULL)
{
CCLOG(@"hello2");
}

b2AABB aabb;
b2Vec2 d = b2Vec2(0.001f, 0.001f);
aabb.lowerBound = m_mouseWorld - d;
aabb.upperBound = m_mouseWorld + d;

// Query the world for overlapping shapes.
QueryCallback callback(m_mouseWorld);
world->QueryAABB(&callback, aabb);

if (callback.m_fixture)
{
b2BodyDef bodyDef;
b2Body* groundBody = world->CreateBody(&bodyDef);
b2Body* bodyz = callback.m_fixture->GetBody();
bodyz->SetAwake(true);
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = bodyz;
md.target = m_mouseWorld;
md.maxForce = 1000.0f * bodyz->GetMass();
m_mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
}
return TRUE;
}
//to prevent touches being passed to this handler when not in the game
return FALSE;
}


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 10:23 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Of course, everybody knows that increasing the size of your "d" is guaranteed to have more people grabbing your balls... the spam mail said so :D

Seriously now - yeah you would need to make the d bigger so that the QueryAABB covered a larger area. You will probably need to alter that QueryCallback class so that instead of just having a single 'm_fixture', it builds a list of all the balls that were in the area, so you can then check which is the closest to the touch.


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 10:42 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
LOL.

Thx. I'm hoping I don't have to alter QueryCallback just yet.

I've tried increasing "d" value tenfold, one hundred fold, even 1,000 fold (i.e. from .001 to 1.0) and am not seeing any noticeable difference. Even at 100.0 there is no difference in "grab-ability"...

I must be missing something...

Also, perhaps you might explain the relative size of "d"...and how it works.


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 10:57 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
The d is just being used to set the corners of the aabb.
It depends on the scale of things in your world as to how many of them will be covered by a 100x100 square, but supposing there are a whole bunch of balls in the query aabb, whatever happens in the QueryCallback is very important. If it's set up to return just any fixture that was in the area, it will probably just return as soon as one is found, which might not be the closest one to the touch.

The QueryAABB is designed to be efficient for a large number of objects, but if the total number of bodies to grab is not too many, say less than 50 or so, I think the performance would still be ok doing it the boring way and just looping through them all and find the closest one.


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 11:06 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
Yes, less than 50...I'll worry about QueryCallback when I deal with larger numbers.

But why hasn't changing my "d" value shown any affect?

I've tried a bunch of versions of the following:

b2AABB aabb;
b2Vec2 d = b2Vec2(0.1f, 1000.1f);
aabb.lowerBound = m_mouseWorld - d;
aabb.upperBound = m_mouseWorld + d;

b2AABB aabb;
b2Vec2 d = b2Vec2(1000.1f, 1000.1f);
aabb.lowerBound = m_mouseWorld - d;
aabb.upperBound = m_mouseWorld + d;

Etc.


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 10:05 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Making d larger would mean you could touch further away from a ball and still be able to move it around, but if you're always touching on the ball anyway it wouldn't make any difference.


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 7:07 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
That's the point...for old people who are not familiar with touch/drag, or fat/clumsy fingers, or, like my neighbours kid with cerebral palsy, having a bigger area to connect their finger with is a benefit.

My ball sprites are about 100 x 100 pixels. How would I increase aabb so that it's 125x125? or 150x150?

I'm not achieving it with the code I posted, so am I missing something?


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 7:20 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
It sounds like you might be better off just looping through all the ball bodies and checking how far they are from the touch. Find the closest one and if it's within a certain distance, it becomes the dragged ball. Then you could just adjust that threshold distance.


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 7:30 am 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
I see...yes.

Found a similar solution here: http://www.cocos2d-iphone.org/forum/topic/3658

Will try it.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group