Collisions are off

Oct 25, 2010 at 2:15 PM

For some reason my collisions are off and I have no idea why, this is the baseEntity that I have been trying to write :

public class BaseEntity
    {
        public bool enabled;
        private const float pixelsPerMeter = 100f;

        protected SpriteBatch spriteBatch;
        public Texture2D sprite;

        protected World world;
        public Body body;
        public Fixture fixture;
        public PolygonShape shape;
        public Vector2 center;

        public Vector2 Position
        {
            get
            {
                return body.Position * pixelsPerMeter;
            }
            set
            {
                if (value.X == 0 || value.Y == 0)
                {
                    Vector2 set = Vector2.Zero;
                    if (value.X != 0)
                        set.X = value.X / pixelsPerMeter;
                    if (value.Y != 0)
                        set.Y = value.Y / pixelsPerMeter;

                    body.Position = set;
                }
                else
                {
                    body.Position = value / pixelsPerMeter;
                }
            }
      
        }

        public BaseEntity(SpriteBatch _spriteBatch, Texture2D _sprite, World _world)
        {
            world = _world;
            sprite = _sprite;
            body = world.CreateBody();
            body.BodyType = BodyType.Dynamic;
            shape = new PolygonShape(PolygonTools.CreateRectangle(sprite.Width / pixelsPerMeter, sprite.Height / pixelsPerMeter));
            fixture = body.CreateFixture(shape);
            center = new Vector2(sprite.Width / 2f, sprite.Height / 2f);

            enabled = true;
            spriteBatch = _spriteBatch;
            sprite = _sprite;
        }
        public BaseEntity(Vector2 _position, Vector2 _center, Texture2D _sprite, float _rotation, SpriteBatch _spriteBatch, World _world)
        {

            world = _world;
            body = world.CreateBody();
            body.BodyType = BodyType.Dynamic;
            shape = new PolygonShape(PolygonTools.CreateRectangle(sprite.Width / pixelsPerMeter, sprite.Height / pixelsPerMeter));
            fixture = body.CreateFixture(shape);
            fixture.Body.Position = _position / pixelsPerMeter;
            fixture.Body.Rotation = _rotation;
            center = new Vector2(sprite.Width / 2f, sprite.Height / 2f);

            enabled = true;
            sprite = _sprite;
            spriteBatch = _spriteBatch;
        }

        public virtual void Draw(GameTime time)
        {
            if (enabled)
                spriteBatch.Draw(
                    sprite,
                    Position,
                    null,
                    Color.White,
                    body.Rotation,
                    center,
                    1,
                    SpriteEffects.None,
                    0
                 );
        }

        public virtual void Think(GameTime time)
        {

        }

This is what happens : 

http://www.cubeupload.com/files/ab3829img25102010150439.png

they are colliding between the blue and green thingie

Oct 26, 2010 at 11:53 PM

Anyone ? Still cracking my head arround this

Oct 28, 2010 at 8:31 PM

  It's not very clear what you want, but i image that the problem is that the too objects don't seem to touch when the collisions are resolved.

Try to adjust the 'pixelsPerMeter' constant, so that you get objects with sides in the 10 - 0.1 meter range.
One other alternative is to adjust the 'LinearSlop' in Settings.cs of farseer engine.

 

Oct 29, 2010 at 3:34 PM

Tried the pixels per meter, same results it is already colliding before its supposed to happen, maybe there is something wrong with my drawing code ? also what do I need to adjust in linearslop ?

Nov 4, 2010 at 1:32 AM
Well, Just from quickly going through your code, it doesn't look like anything should be troubling you. Outside of possibly creating the objects on stage strangely, the only thing I'd advise you check out is the texture files themselves. Make sure that the images don't have strange invisible borders or anything like that. -Oranjoose