Moving a source of gravity

Topics: Developer Forum
Sep 30, 2013 at 2:02 AM
I've created a game with black holes, similar to that of one's in geometry wars.

Problem is I can not get the gravity source to stick to the Body I've attached it to, as it scrolls across the screen, the gravity source simply stays in the same position. This is the code I've used.

Body blackHoleBody;
                blackHoleBody = BodyFactory.CreateBody(world);
                CircleShape ballShape =
                    new CircleShape(ConvertUnits.ToSimUnits(imageItemList[i].texture.Width / 2f), 1f);
                Fixture ballFixture = blackHoleBody.CreateFixture(ballShape);

                blackHoleBody.BodyType = BodyType.Static;
                blackHoleBody.Position = ConvertUnits.ToSimUnits(imageItemList[i].Position);
                blackHoleBody.Restitution = 0.0f;

                blackHoleBody.CollisionCategories = Category.All;
                blackHoleBody.CollidesWith = Category.All;

                //Create the gravity controller
                GravityController gravity = new GravityController(10f);
                gravity.DisabledOnGroup = 3;
                gravity.EnabledOnGroup = 2;
                gravity.DisabledOnCategories = Category.Cat2;
                gravity.EnabledOnCategories = Category.Cat3;
                gravity.GravityType = GravityType.Linear;


                //Add the planet as the one that has gravity

My guess is that I need to get in the GravityController inside world, and update it's position there, but when I look I can only find the world global gravity.

If anyone could point me in the right direction it would be much appreciated.
Sep 30, 2013 at 5:53 PM
I can't tell without seeing the drawing code but my guess is you're moving the texture of the black hole but not the body.

How are you moving the body around the screen? You need to apply forces or set the linearVelocity on the body to make it move and then use the body's position to draw your texture to the screen (using ConvertUnits to move from engine co-ords to screen co-ords)

Does your drawing code look something like this:
spritebatch.Draw(BlackHoleTexture, ConvertUnits.ToDisplayUnits(blackHoleBody.Position), Color.White);
Or are you using another Vector2 to set the textures position on screen? If you are, try the code above and see what happens.

And is the black hole meant to be static or dynamic?

Have fun
Sep 30, 2013 at 6:07 PM
My black hole is a imageItem with a body attached to it, I move the black hole sprite and update the Body's position so that is equals the sprite's position.

// Moves all level sprites
                    for (int i = 0; i < imageItemList.Count; i++)
                        imageItemList[i].Position.X -= 3f;
// Updates the Black Hole Body so that it equals the sprite's position
                    for (int i = 0; i < imageItemList.Count; i++)
                        if (imageItemList[i].asset_name == ("Images\\Black Hole"))
                            ballBodyList[iNum].Position = ConvertUnits.ToSimUnits(imageItemList[i].Position);
I know that the Body is attached to that Black Hole because the player can still collide with it and be stopped as if it were solid, but the gravity well remains in the starting position of the Black Hole Sprite and does not move.

This leads me to believe the gravityController is not getting attached to the Body properly.
Sep 30, 2013 at 8:07 PM
Ahh I think I see your problem now.

Get rid of
    gravity.DisabledOnGroup = 3;
    gravity.EnabledOnGroup = 2;
And it should work.

Collision groups are different from collision categories. With EnabledOnGroup = 2 in your code the controller looks to see if the body is in group 2 (which is different from Category 2). Because it isn't in that group it ignores it.

So remove the lines or add blackHoleBody.CollisionGroup =2 somewhere and you should be fine.