dotnet-ad / Humper

Collision detection for Axis-Aligned-Bounding-Boxes (aka AABB) in C#.
MIT License
104 stars 19 forks source link

Tunneling issue #7

Open Charathan opened 6 years ago

Charathan commented 6 years ago

Hi, I was working on a game in Love2D and decide to port it to Monogame. Finding Humper helped me a lot since I was using Bump in Love2D but I did run into a issue I didn't have in bump:

test

I start off with a world with a width of 480 and height of 280 and cell size of 64 and 56 spheres (a 16 by 16 AABB) falling, the green number counts the spheres in the world, and make them fall, most of them bounce back using the default collision response but some of them pass through the floor. Am I doing something wrong or is this a bug?

The sphere have the following collision response:

            float nx = 0, ny = 0;
            float dt = (float)gametime.ElapsedGameTime.TotalSeconds;
            _vy = Math.Max(_vy + (_gravity * dt), _maxHeight);

            nx = Position.X + (_vx * dt);
            ny = Position.Y + (_vy * dt);

            var move = _box.Move(nx, ny, (collision) =>
            {
                if (collision.Other.HasTag(EntityType.Wall))
                {
                    return CollisionResponses.Bounce;
                }
                return CollisionResponses.Cross;
            });

            foreach (IHit hit in move.Hits)
            {
                if (hit.Box.HasTag(EntityType.Wall))
                {
                    if ((hit.Normal.X < 0 && _vx > 0) || (hit.Normal.X > 0 && _vx < 0))
                    {
                        _vx = _vx * -1;
                    }
                    if ((hit.Normal.Y < 0 && _vy > 0) || (hit.Normal.Y > 0 && _vy < 0))
                    {
                        _vy = _vy * -1;
                    }
                }
            }

            Position = new Vector2(_box.X, _box.Y);

            if (_box.Y > 480) // if they tunnel through the floor...
            {
                _entities.Remove(this);
            }

The walls don't have a response since they are static.

Donaut commented 3 years ago

I think the problem is with the last part. image

480 is a too accurate value. Maybe try with some bigger value? Like 600