Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have multiple bodies in my game world in andengine. All the bodies affected by gravity but in that I want my specific body does not affected by the gravity.

For that solution after research I found that I have to use body.setGravityScale(0) method for my problem solution.

But in my andengine extension I don't found that method so please provide guidance about how get access about that method.

Also for the above problem any other guidance will be acceptable. Thank You!

I apply following code for reverse gravity final Vector2 vec = new Vector2(0, -SensorManager.GRAVITY_EARTH * bulletBody.getMass());

bulletBody.applyForce(vec, bulletBody.getWorldCenter());

share|improve this question
    
I manually update the andengine box2d library and add setGravityScale() method that is the way I do my work. –  Siddharth Dec 5 '12 at 16:12

3 Answers 3

To make bodies "immune" to gravity simply apply an equal and opposite force for every step. Opposite forces will cancel each other out, leaving you with zero force (no gravity). This method means your object will still react to other forces.

Alternatively you can set the mass of the object to zero. Massless objects are not affected by gravity. However the second option may have some unintended consequences. Some physics engines make things with zero mass static. Which means they don't respond to collisions either (you'd likely not want such an interaction with a massless object anyways).

share|improve this answer
    
You are wrong about one thing - AndEngine uses the C++ version of Box2D, it is not a Java port. –  JohnEye May 24 '12 at 15:42
    
Good to know. The documentation is pretty poor for AndEngine. –  Byte56 May 24 '12 at 16:38
    
Chances are, applying an opposite force will take twice as long though. –  Tharwen May 24 '12 at 20:44
    
@Tharwen Twice as long to do what? The engine is going to sum up the forces before it does anything else. I can't imagine this would add more than a millisecond or two. Which is negligible. –  Byte56 May 24 '12 at 20:49
    
Applying two forces will probably take twice as long as applying one force. It's just something to be aware of, and it will stop being negligible if you start doing the calculation thousands of times or more (Admittedly, it's unlikely that you would, but as I said, it's just something to be aware of). –  Tharwen May 25 '12 at 8:27

Possibly kinematic bodies is what you want, notice they are not affected by collision response.

share|improve this answer

If you are using a version of Box2D that does not provide the SetGravityScale method, the best option is to disable gravity entirely and manually apply gravity itself to all but one object.

Applying the opposite of gravity is not significantly more expensive, but it is often unusably imprecise when multiple forces are acting on the same object.

It is also possible you may want to make bullets kinematic objects rather than dynamic ones. This will stop them from being affected by gravity, and allow you to move them by setting their velocity rather than applying forces, often a more useful approach for a bullet. If you do this, however, they will no longer interact with static objects either.

share|improve this answer
    
how can I apply gravity to particular object manually because there was no method for manual gravity setting –  Siddharth May 25 '12 at 13:06
3  
hint: gravity is a force –  JohnEye May 25 '12 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.