Search code examples
c#windowsgame-physicsmonogamefarseer

MonoGame with Farseer physics engine. Can't locate at bottom


I'm trying to learn how to implement physics in games, so I chose Farseer Physics. I'm trying to implement the falling round object till the obstacle(ground in my case). Here's my code:

using FarseerPhysics;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace TestFarseer
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        World world;
        Texture2D buttonTexture, groundTexture;
        Body buttonBody, groundBody;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            world = new World(new Vector2(0, 1f));
            ConvertUnits.SetDisplayUnitToSimUnitRatio(102f);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            buttonTexture = Content.Load<Texture2D>("pause");
            groundTexture = Content.Load<Texture2D>("Panel");

            groundBody = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(groundTexture.Width), ConvertUnits.ToSimUnits(groundTexture.Height), 1f);
            groundBody.BodyType = BodyType.Static;
            groundBody.Position = new Vector2(ConvertUnits.ToSimUnits(groundTexture.Width/2f), ConvertUnits.ToSimUnits(GraphicsDevice.Viewport.Height-groundTexture.Height));
            CircleShape buttonShape = new CircleShape(0.5f, 0.8f);

            buttonBody = new Body(world);
            buttonBody.Mass = 10f;
            buttonBody.CreateFixture(buttonShape);
            buttonBody.BodyType = BodyType.Dynamic;
            buttonBody.Position = new Vector2(ConvertUnits.ToSimUnits(51f), ConvertUnits.ToSimUnits(51f));
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            world.Step((float)gameTime.ElapsedGameTime.TotalSeconds);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(buttonTexture, ConvertUnits.ToDisplayUnits(buttonBody.Position), null, Color.White, buttonBody.Rotation, new Vector2(buttonTexture.Width/2, buttonTexture.Height/2), 1f, SpriteEffects.None, 0);
            spriteBatch.Draw(groundTexture, ConvertUnits.ToDisplayUnits(groundBody.Position), null, Color.White, groundBody.Rotation, new Vector2(groundTexture.Width/2, groundTexture.Height/2), 1f, SpriteEffects.None, 0);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

And the collision occur, but I can't set ground to be at bottom. Here's the screenshot:Bottom problem


Solution

  • In Farseer the origin of a body is at the center, so :

     groundBody.Position = new Vector2(ConvertUnits.ToSimUnits(groundTexture.Width/2f), ConvertUnits.ToSimUnits(GraphicsDevice.Viewport.Height-(groundTexture.Height / 2)));`
    

    Groover,