Drawing physic Objects

Topics: User Forum
Mar 21, 2013 at 6:50 PM
Hello,

i have ran into a problem in game project and i believe it has something to do with the way i am drawing the objects.

i have been messsing around with this tutorial : http://roy-t.nl/index.php/2013/01/02/farseer-physics-3-3-1-and-xna-platformer-character/

i have changed a few things in the drawable physic class so that i may draw a sprite from a spritesheet with no animation for now.

Could somone please tell me whats wrong with this drawable physics class that i have edited.

    public class DrawablePhysicsObject
    {
        // For all objects using this class
        public Body Body { get; private set; }
        public Texture2D texture;
        public World world;
        private int frameWidth = 0;
        private int frameHeight = 0;
        private int currentFrame;
        private Vector2 size;
        protected List<Rectangle> frames = new List<Rectangle>();
        public Vector2 Position
        {
            get { return CoordinateHelper.ToScreen(Body.Position); }
            set { Body.Position = CoordinateHelper.ToWorld(value); }
        }

        public Vector2 Size
        {
            get { return CoordinateHelper.ToScreen(size); }
            set { size = CoordinateHelper.ToWorld(value); }
        }

        ///<summary>
        /// Creates a rectangular drawable physics object
        ///</summary>
        /// <param name="world">The farseer simulation this object should be part of</param>
        /// <param name="texture">The image that will be drawn at the place of the body</param>
        /// <param name="size">The size in pixels</param>
        /// <param name="mass">The mass in kilograms</param>
        public DrawablePhysicsObject(World world, Texture2D texture, Rectangle initialFrame, Vector2 size, float mass)
        {
            //Body = BodyFactory.CreateRectangle(world, size.X * CoordinateHelper.pixelToUnit, size.Y * CoordinateHelper.pixelToUnit, 1);
            Body = BodyFactory.CreateRectangle(world, ConvertUnits.ToSimUnits(initialFrame.Width), ConvertUnits.ToSimUnits(initialFrame.Height), 1);
            Body.BodyType = BodyType.Dynamic;
            Body.IgnoreCCD = false;

            this.Size = size;
            this.texture = texture;

            frames.Add(initialFrame);
            frameWidth = initialFrame.Width;
            frameHeight = initialFrame.Height;
        }

        /// <summary>
        /// Creates a circular drawable physics object
        /// </summary>
        /// <param name="world">The farseer simulation this object should be part of</param>
        /// <param name="texture">The image that will be drawn at the place of the body</param>
        /// <param name="diameter"> The diameter in pixels</param>
        /// <param name="mass">The mass in kilograms</param>
        public DrawablePhysicsObject(World world, Texture2D texture, Rectangle initialFrame, float diameter, float mass)
        {
            size = new Vector2(diameter, diameter);
            Body = BodyFactory.CreateCircle(world, (diameter / 2.0f) * CoordinateHelper.pixelToUnit, 1);

            Body.BodyType = BodyType.Dynamic;

            this.Size = size;
            this.texture = texture;

            frames.Add(initialFrame);
            frameWidth = initialFrame.Width;
            frameHeight = initialFrame.Height;
        }

        public int Frame
        {
            get { return currentFrame; }
            set
            {
                currentFrame = (int)MathHelper.Clamp(value, 0,
                    frames.Count - 1);
            }
        }

        public Rectangle Source
        {
            get { return frames[currentFrame]; }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle destination = new Rectangle
            (
                (int)Position.X,
                (int)Position.Y,
                (int)Size.X,
                (int)Size.Y
            );

            spriteBatch.Draw(texture, destination, Source, Color.White, Body.Rotation, new Vector2(Source.Width / 2.0f, Source.Height / 2.0f), SpriteEffects.None, 0);
        }
I am using Source so i may draw a frame from a spritesheet.

My physic body fixture collisions are off by quite a bit, collision occur outside of the shape i see in debugview.

Anybody know why im having this problem?

Thanks for the help.