Body Position offset [solved]

Topics: User Forum
Nov 8, 2010 at 8:47 AM

hello i'm using Farseer for testing a simple use, one ball on on floor with rebound, and after with left and arrow key apply force left or right, but i've a problem my floor doesn't fill the screen width, and normally my ball must fall when she roll over the floor, but i've an offset between my real floor position and that iseen, my floor was normally centered on the screen, and it was offset on the left.

i'm looking on this code lot of times , and i doesn't understand where is the error !! Can you give me a tips ? Thx for your help :o)

 

You can dl my project at http://www.gletellier.com/PhysiqueTest.zip or just seen the code here :

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerPhysics.Factories; 
using FarseerPhysics.Dynamics;
using FarseerPhysics.Common;

namespace PhysiqueTest
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont sFont;
        string sDebug = "";        
        World physicsWorld = new World(new Vector2(0.0f, 9.8f));
       Texture2D platformTexture, boxTexture,bodyTexture;
       
  Fixture platformFixture, boxFixture;

        Vector2 platformPosition = Vector2.Zero;
        Vector2 boxPosition = Vector2.Zero;
        Vector2 platAff = Vector2.Zero;

        bool bShoot = false;

          const float pixelsPerMeter = 100.0f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.IsFullScreen = false;
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferredBackBufferHeight = 768;
            graphics.ApplyChanges();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
          
            // TODO: use this.Content to load your game content here

            boxTexture = Content.Load<Texture2D>("box");
            sFont = Content.Load<SpriteFont>("debug");
            boxPosition.X = (1024 / 2) - (boxTexture.Width / 2);
            boxPosition.Y = 100;

            platformTexture = Content.Load<Texture2D>("platform");
            bodyTexture = Content.Load<Texture2D>("platform2");
            platformPosition.X =  (1024 / 2) - (platformTexture.Width / 2);
            platformPosition.Y = 600;
            
            Vector2 platformPos = new Vector2(platformPosition.X / pixelsPerMeter, platformPosition.Y / pixelsPerMeter);

            platformFixture = FixtureFactory.CreateRectangle(physicsWorld, platformTexture.Width / pixelsPerMeter, platformTexture.Height / pixelsPerMeter, 1);
            platformFixture.Body.Position = platformPos;
            platformFixture.Body.BodyType = BodyType.Static;
            platformFixture.Restitution = 0.3f;

            Vector2 newVect = new Vector2(platformPos.X ,platformPos.Y);
            platformFixture.Body.Position = newVect;

            boxFixture = FixtureFactory.CreateCircle(physicsWorld, boxTexture.Width / pixelsPerMeter, 1);
            boxFixture.Body.Position = new Vector2(boxPosition.X / pixelsPerMeter, boxPosition.Y / pixelsPerMeter);
            boxFixture.Body.BodyType = BodyType.Dynamic;
            boxFixture.Restitution = 0.8f;
            boxFixture.Friction = 0.3f;
        }

        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
           
            // Gather basic input.
            KeyboardState keystate = Keyboard.GetState();          
            if (keystate.IsKeyDown(Keys.Right))            {
               
                boxFixture.Body.ApplyForce(new Vector2(1.0f,0));                
            }
            if (keystate.IsKeyDown(Keys.Left))
            {
                boxFixture.Body.ApplyForce(new Vector2(-1.0f, 0));
            }
            
            if (!bShoot)
            {
                bShoot = false;
            }

            boxPosition.X = boxFixture.Body.Position.X * pixelsPerMeter;
            boxPosition.Y = boxFixture.Body.Position.Y * pixelsPerMeter;

            platAff.X = platformFixture.Body.Position.X * pixelsPerMeter;
            platAff.Y = platformFixture.Body.Position.Y * pixelsPerMeter;

            sDebug = boxPosition.X.ToString("000") + " - " + boxPosition.Y.ToString("000") + "     "  ;
            sDebug += platformFixture.Body.Position.X * 100 + "  - " + platformFixture.Body.Position.Y * 100;

            physicsWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            spriteBatch.Begin();
            spriteBatch.Draw(boxTexture, boxPosition, Color.White);
            spriteBatch.Draw(platformTexture, platformPosition, Color.White);
            spriteBatch.Draw(bodyTexture, platAff, Color.White);
            spriteBatch.DrawString(sFont,sDebug,new Vector2(15,15),Color.Red);
            spriteBatch.End();               

            base.Draw(gameTime);
        }
    }
}

 

 

 

 

Nov 8, 2010 at 5:12 PM

sorry i've not understand that the body position was the gravitationnal center ! i fix it and it's ok !!!