This project has moved and is read-only. For the latest updates, please go here.

Moving a Body doesn´t work as expected

Topics: User Forum
Dec 17, 2011 at 8:36 PM
Edited Dec 17, 2011 at 10:55 PM

Hello guys,

i got a problem starting with FPE...

I want to add Physics and Collisions into my sideview-platformer.
My Player has an Sprite (Texture) of size 32x48. My tile-engine has one body for each tile (â 32x32).

Now if i set the gravity to Y: 0.5, my player falls down slowly onto the tiles. First bug: It falls some pixels to far into the tile.
Next huge bug: If i try to move the player by adding forces, it won´t move. It only moves if i apply a force of 1000+. But then unbelievable slowly...

Now my question is: Why is this so? What do i make wrong? And how can i make the player moves just the time i press the button? Applying forces let him fly away...

Here´s my code:

    public class Player : EntityNode
        public Body Body;

        public Player()
            AddFeature(new PlayerRenderer());
            Velocity = new Vector2(20, 20);

            Position = new Vector2(100, -100);

            Body = BodyFactory.CreateRectangle(IngameScreen.World, 32, 48, 0, Position);
            FixtureFactory.AttachRectangle(32, 48, 1.25f, Vector2.Zero, Body);
            Body.BodyType = BodyType.Dynamic;

        public override void Update(GameTime gameTime)
            if (DualLinkGame.Input.Keyboard.IsPressed(Keys.Left, false))
                Body.SetTransform(new Vector2(Body.Position.X + Velocity.X, 0f), 0f);
            else if (DualLinkGame.Input.Keyboard.IsPressed(Keys.Right, false))
                Body.ApplyForce(new Vector2(Velocity.X, 0));
            if (DualLinkGame.Input.Keyboard.IsPressed(Keys.Up, false))
                Body.ApplyForce(new Vector2(0, -Velocity.Y));
            else if (DualLinkGame.Input.Keyboard.IsPressed(Keys.Down, false))
                Body.ApplyForce(new Vector2(0, Velocity.Y));


And another important question:
How can i create bodies which are exactly the same size of my texture?
If i create a fixture of 32x48 like my tile, the collision doesn´t work as expected... Sometimes the player collides in air, sometimes he falls down trough the terrain ... how can i calculate, how big are 32 pixels?! And how can I scale the world, so 200 pixels aren´t the same as a 46-story building (found this in the box2D manual)?