Box2D Forums

It is currently Fri May 24, 2013 5:03 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Feb 26, 2012 7:44 pm 
Offline

Joined: Sun Feb 26, 2012 7:08 pm
Posts: 3
Hi all,

I'm new to box2d, and i'm making games for android. There is something i want to ask,
I want to create an object which will pull other object, something like gravity / magnetic field,
while there is world gravity also so they will affecting each other.

I calculate how much force applied to other object like this:

Code:
Vec2 position = source.getPosition();
Vec2 position2 = target.getPosition();
float range = spacing(position.x - position2.x, position.y - position2.y); //method to calculate range, just simple pythagoras
if(range <= FIELD_RADIUS) {
   Vec2 force = new Vec2(position.x-position2.x, position.y-position2.y);
   force.normalize();                                                                      //force direction always point to source
   force.mul(target.getMass * 10 * (FIELD_RADIUS-range) / FIELD_RADIUS);
   target.applyForce(force, target.getWorldCenter());
}


I can't find what's wrong with this code, but no matter how much force I applied, the object doesn't seems to be pulled by the source.
Even if i change the force magnitude to 10000 it won't pull the object, unless i set the world gravity to zero. strange...

So I think maybe i use the applyForce not correctly, can any of you give me suggestion / solution?

Also, i want to know the difference between getWorldCenter and getPosition.

Thanks in advance.


Top
 Profile  
 
PostPosted: Mon Feb 27, 2012 8:39 am 
Offline

Joined: Sun Feb 26, 2012 7:08 pm
Posts: 3
I have test this further, and it seems that the gravity is somehow overpower the force.

but still it's strange i think. Box2d use meter and second right? so when i set the gravity as (0, -3),
it's the gravity acceleration of 3 m/s2 to negative y axis, am I right?

so if I apply the force to y positive, with magnitude body.getMass()*10, it gives acceleration of 10m/s2 upward,
it should be greater than the gravity? but it's not... strange...

did I miss something here? please, any help would be much appreciated.


Top
 Profile  
 
PostPosted: Mon Feb 27, 2012 8:59 am 
Offline

Joined: Sun Feb 26, 2012 7:08 pm
Posts: 3
it turns out that this problem is because improper use of force.mul()
it returns the multiplied force, but the force itself is not multiplied.

so the code should be like this
Code:
if(range <= FIELD_RADIUS) {
   Vec2 force = new Vec2(position.x-position2.x, position.y-position2.y);
   force.normalize();                                                                      //force direction always point to source
   force.set(force.mul(target.getMass * 10 * (FIELD_RADIUS-range) / FIELD_RADIUS));
   target.applyForce(force, target.getWorldCenter());
}


Top
 Profile  
 
PostPosted: Tue Feb 28, 2012 12:25 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 353
melonkuantum wrote:
it turns out that this problem is because improper use of force.mul()


Yeah, mutable methods usually have the postfix "Local", so you would want to use "mulLocal()"


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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