Hi. I was wondering if it was possible set a center of gravity or various points of gravity using the dyn4j engine. The specific situation requires a relatively low-mass body to still apply an attractive force on surrounding bodies. I would prefer to avoiding using setMass (as I understand it is deprecated and may cause issues). Does dyn4j support the creation of a gravity well without an accompanying body?

I understand that there are ways to implement these "gravity wells" using the functionality of dyn4j (such as generating some sort of "force map" function that applies the appropriate force to a body if it takes in an input position and returning a force vector), but I was hoping for a simpler solution.

I am open to any suggestions and would be grateful for input!

Thank you,

SCr

## Gravity Wells or Gravity Points without a Body

### Re: Gravity Wells or Gravity Points without a Body

One simple way would be to use one of the World.detect methods using a Circle. If the circle, representing the gravity well, overlaps anything then compute the distance to the center of the circle. Once you have the distance you can use some function (universal gravitation with a modified G constant for example) to determine the force to apply.

William

William

### Re: Gravity Wells or Gravity Points without a Body

Hi William,

Thank you for the help. I had a follow-up question about this. As far as I understand, the World.detect methods receive a fixed convex shape or AABB whose location does not change in the world. Correct me if I'm wrong, but this would not work for a moving gravity well. Is my best option to essentially use an 'invisible' body and apply forces through a collision listener?

Thank you,

SCr

Thank you for the help. I had a follow-up question about this. As far as I understand, the World.detect methods receive a fixed convex shape or AABB whose location does not change in the world. Correct me if I'm wrong, but this would not work for a moving gravity well. Is my best option to essentially use an 'invisible' body and apply forces through a collision listener?

Thank you,

SCr

### Re: Gravity Wells or Gravity Points without a Body

Sorry, one more newbie question. How would I naturally implement gravity using mass? I tried to make bodies with high mass / density, and even a body of mass type infinite, but there doesn't appear to be any gravity. Is this happening because I have set world.gravity to (0,0)? How would I 'enable' gravity in the world?

Thanks again!

SCr

Thanks again!

SCr

### Re: Gravity Wells or Gravity Points without a Body

the world gravity is just a 'global' force that is applied to everything. Think platformer games where you always fall down because gravity.

To model planets and such you will have to calculate the effect of gravity yourself and apply it to the bodies between every step of the simulation. One way to do so is to each loop of the game logic use the detect method described by William to find what bodies are 'close enough' for gravity to have an interesting effect. Then calculate the force you need to apply to each body by calculating the distance and the mass of the body then applyForce and step the world.

To model planets and such you will have to calculate the effect of gravity yourself and apply it to the bodies between every step of the simulation. One way to do so is to each loop of the game logic use the detect method described by William to find what bodies are 'close enough' for gravity to have an interesting effect. Then calculate the force you need to apply to each body by calculating the distance and the mass of the body then applyForce and step the world.

Return to “General Discussion”

### Who is online

Users browsing this forum: No registered users and 2 guests