1
\$\begingroup\$

So I was sitting with my programming teacher with this issue for about two hours and we couldn't figure out anything specific that could possibly be the issue. Therefore I'm taking some measures and I'm asking you guys on here.

So around three months ago, I managed to get pixel-perfect collision to work in my game with added support for sourceRectangle, scale, rotation and origin points. There are three main objects the player is able to collide with: Checkpoints, Spikes and Bullets (there are a few other things but they dont meet the same importance level).

Updater of pixel-collision:

foreach(Entity e1 in Data.entities.OfType<Player>())
        {
            foreach(Entity e2 in Data.entities.OfType<Collidable>())
            {
                Matrix entityTransform;

                entityTransform =
                        Matrix.CreateTranslation(new Vector3(-e2.origin, 0.0f)) *
                        Matrix.CreateScale(e2.scale.X, e2.scale.Y, 0.0f) *
                        Matrix.CreateRotationZ(e2.rotation) *
                        Matrix.CreateTranslation(new Vector3(e2.position, 0.0f));

                Rectangle entityRect = CalculateBoundingRectangle(new Rectangle(0, 0, e2.srcRect.Width, e2.srcRect.Height), entityTransform);

                // The per-pixel check is expensive, so check the bounding rectangles
                // first to prevent testing pixels when collisions are impossible.
                if(e1.rectangle.Intersects(entityRect))
                {
                    if(Sprite.IntersectPixels(e1.transform, e1.srcRect.Width, e1.srcRect.Height, e1.sprite.textureData,
                                                e2.transform, e2.srcRect.Width, e2.srcRect.Height, e2.sprite.textureData))
                    {
                        e1.CollisionEvent(e2, gameTime);
                    }
                }
            }
        }

        Data.entities.AddRange(Data.tempEntities);
        Data.tempEntities.Clear();
    }

So the main problem here is, bullets specifically doesn't work at all for some reason. Sometimes they work, sometimes they work very oddly and sometimes they don't work (except for the spike-shaped bullet which doesn't work at all no matter how many restarts).

Creating a new bullet:

public Bullet(BulletDefinition bulletDef, Vector2 position, Vector2 speed, Vector2 acceleration, float rotation, float texturePriority, Color color, int bulletID)
    {
        texture = TextureSet.Bullet;
        this.bulletDef = bulletDef;
        this.texturePriority = texturePriority;
        this.position = position;
        this.speed = speed;
        this.acceleration = acceleration;
        this.rotation = rotation;
        this.bulletID = bulletID;
        this.color = color;

        switch(bulletDef)
        {
            case BulletDefinition.CircularBullet:
                srcRect = new Rectangle(0, 0, 128, 128);
                origin = new Vector2(64, 64);
                scale = new Vector2(0.25f, 0.25f);
                this.rectangle = new Rectangle(1, 1, 32, 32);
                break;
            case BulletDefinition.OvalBullet:
                srcRect = new Rectangle(0, 0, 128, 128);
                origin = new Vector2(64, 64);
                scale = new Vector2(0.1f, 0.225f);
                this.rectangle = new Rectangle(0, 0, 13, 29);
                break;
            case BulletDefinition.BoomerangBullet:
                srcRect = new Rectangle(128, 0, 128, 128);
                origin = new Vector2(64, 64);
                scale = new Vector2(0.25f, 0.25f);
                this.rectangle = new Rectangle(0, 0, 32, 32);
                break;
            case BulletDefinition.SpikeBullet:
                srcRect = new Rectangle(256, 0, 128, 128);
                origin = new Vector2(64, 64);
                scale = new Vector2(0.25f, 0.25f);
                this.rectangle = new Rectangle(0, 0, 32, 32);
                break;

        sprite = new Sprite(this.rectangle, GraphicsManager.textures[(int)TextureSet.Bullet], srcRect);
    }

If this is not fixable, I'll just reprogram the entire Bullet class from start. But until then I wanted to see if a fix of any kind would be appliable for this issue, since I have absolutely no idea what I could do to fix this. Code and explainations to why it is like this would be highly appreciated.

Take note as of yet, the only collidable objects that have actually been using rotation, origin and scale consistently as of yet is the Bullet class.

EDIT: Forgot to mention that the game I'm making is similar to a bullet-hell game. Examples are I Wanna Be The Guy.

\$\endgroup\$
4
  • \$\begingroup\$ when you're doing collision detection with fast moving small objects like bullets you might want to consider using sweep testing \$\endgroup\$ – craftworkgames Feb 10 '17 at 6:03
  • \$\begingroup\$ I forgot to define what type of game it is when it comes to using bullets in this game. This game is a mix of platforming and bullet-hell so judging from the limitations of "sweep testing" it doesn't seem like it would be manageable. \$\endgroup\$ – A. Ben Feb 10 '17 at 7:55
  • \$\begingroup\$ Try doing creating the transformation matrix in the order translateToOrigin * rotate * translate * scale \$\endgroup\$ – Bálint Feb 10 '17 at 8:53
  • \$\begingroup\$ It didn't change much. However, I think I've managed to get myself a proper insight on what the actual issue of this is. I will give a detailed text explaining the ordeal as soon as I can. \$\endgroup\$ – A. Ben Feb 10 '17 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.