[SOLVED] Rectangle Penetration

Topics: Developer Forum, Project Management Forum, User Forum
Sep 23, 2013 at 7:56 PM
If I have two rectangles that their drawing specify 0f (zero) as rotation, penetrate themselves.

Example:
Modyfing te code of the first sample
Image


Here's the modified code
using System.Text;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using FarseerPhysics.Samples.Demos.Prefabs;
using FarseerPhysics.Samples.DrawingSystem;
using FarseerPhysics.Samples.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FarseerPhysics.Samples.Demos
{
    internal class SimpleDemo1 : PhysicsGameScreen, IDemoScreen
    {
        private Border _border;
        private Body _rectangle;
        private Sprite _rectangleSprite;
        private Body _rectangle2;
        private Sprite _rectangleSprite2;

        #region IDemoScreen Members

        public string GetTitle()
        {
            return "Body with a single fixture";
        }

        public string GetDetails()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("This demo shows a single body with one attached fixture and shape.");
            sb.AppendLine("A fixture binds a shape to a body and adds material");
            sb.AppendLine("properties such as density, friction, and restitution.");
            sb.AppendLine(string.Empty);
            sb.AppendLine("GamePad:");
            sb.AppendLine("  - Rotate object: left and right triggers");
            sb.AppendLine("  - Move object: right thumbstick");
            sb.AppendLine("  - Move cursor: left thumbstick");
            sb.AppendLine("  - Grab object (beneath cursor): A button");
            sb.AppendLine("  - Drag grabbed object: left thumbstick");
            sb.AppendLine("  - Exit to menu: Back button");
            sb.AppendLine(string.Empty);
            sb.AppendLine("Keyboard:");
            sb.AppendLine("  - Rotate Object: left and right arrows");
            sb.AppendLine("  - Move Object: A,S,D,W");
            sb.AppendLine("  - Exit to menu: Escape");
            sb.AppendLine(string.Empty);
            sb.AppendLine("Mouse / Touchscreen");
            sb.AppendLine("  - Grab object (beneath cursor): Left click");
            sb.AppendLine("  - Drag grabbed object: move mouse / finger");
            return sb.ToString();
        }

        #endregion

        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0f, 9.82f);

            _border = new Border(World, ScreenManager, Camera);

            _rectangle = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
            _rectangle.BodyType = BodyType.Dynamic;

            //SetUserAgent(_rectangle, 100f, 100f);

            // create sprite based on body
            _rectangleSprite = new Sprite(ScreenManager.Assets.TextureFromShape(_rectangle.FixtureList[0].Shape, MaterialType.Squares, Color.Orange, 1f));


            _rectangle2 = BodyFactory.CreateRectangle(World, 5f, 5f, 1f);
            _rectangle2.BodyType = BodyType.Dynamic;

            //SetUserAgent(_rectangle, 100f, 100f);

            // create sprite based on body
            _rectangleSprite2 = new Sprite(ScreenManager.Assets.TextureFromShape(_rectangle.FixtureList[0].Shape, MaterialType.Squares, Color.Orange, 1f));
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin(0, null, null, null, null, null, Camera.View);
            ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture, ConvertUnits.ToDisplayUnits(_rectangle.Position), null, Color.White, 0f, _rectangleSprite.Origin, 1f, SpriteEffects.None, 0f);
            ScreenManager.SpriteBatch.Draw(_rectangleSprite2.Texture, ConvertUnits.ToDisplayUnits(_rectangle2.Position), null, Color.White, 0f, _rectangleSprite2.Origin, 1f, SpriteEffects.None, 0f);
            ScreenManager.SpriteBatch.End();
            _border.Draw();
            base.Draw(gameTime);
        }
    }
}
Sep 23, 2013 at 10:04 PM
Edited Sep 23, 2013 at 10:06 PM
You're drawing the sprite with a rotation of 0 but the bodies are still rotating.

change:
   ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture, ConvertUnits.ToDisplayUnits(_rectangle.Position), null, Color.White, 0f, _rectangleSprite.Origin, 1f, SpriteEffects.None, 0f);
            ScreenManager.SpriteBatch.Draw(_rectangleSprite2.Texture, ConvertUnits.ToDisplayUnits(_rectangle2.Position), null, Color.White, 0f, _rectangleSprite2.Origin, 1f, SpriteEffects.None, 0f);
to
            ScreenManager.SpriteBatch.Draw(_rectangleSprite.Texture, ConvertUnits.ToDisplayUnits(_rectangle.Position), null, Color.White, _rectangle.Rotation, _rectangleSprite.Origin, 1f, SpriteEffects.None, 0f);
            ScreenManager.SpriteBatch.Draw(_rectangleSprite2.Texture, ConvertUnits.ToDisplayUnits(_rectangle2.Position), null, Color.White, _rectangle2.Rotation, _rectangleSprite2.Origin, 1f, SpriteEffects.None, 0f);
And you'll see they're not actually penetrating.

Have fun
Sep 24, 2013 at 1:04 PM
Edited Sep 24, 2013 at 1:08 PM
Thanks for the reply. If I don't want them rotate, is there anything I can do, like "disabling" rotation (obviously without any penetration)?

EDIT: [SOLVED] setting body.FixedRotation = true;