Exemple #1
0
        private void detectarColisao()
        {
            foreach (GameObject o in objetos)
            {
                o.colidiu = false;
            }

            for (int i = 0; i < objetos.Count; i++)
            {
                GameObject obj1 = objetos[i];
                for (int j = i + 1; j < objetos.Count; j++)
                {
                    GameObject obj2    = objetos[j];
                    Colisao    colisao = verificarColisao(obj1, obj2);
                    if (colisao.colidiu)
                    {
                        obj1.colidiu = true;
                        obj2.colidiu = true;
                        Colisao colisao2 = new Colisao();
                        colisao2.colidiu = colisao.colidiu;
                        colisao2.local   = colisao.local;
                        preencherSentidoColisao(colisao, obj1);
                        preencherSentidoColisao(colisao2, obj2);

                        obj1.colisaoDetectada(obj2, colisao);
                        obj2.colisaoDetectada(obj1, colisao2);
                    }
                }
                if (!obj1.colidiu)
                {
                    obj1.colisaoNaoDetectada();
                }
            }
        }
Exemple #2
0
        private Colisao verificarColisao(GameObject a, GameObject b)
        {
            Color[] bitsA = new Color[a.texture.Width * a.texture.Height];
            a.texture.GetData(bitsA);
            Color[] bitsB = new Color[b.texture.Width * b.texture.Height];
            b.texture.GetData(bitsB);

            int x1 = Math.Max(a.rectangle.X, b.rectangle.X);
            int x2 = Math.Min(a.rectangle.X + a.rectangle.Width, b.rectangle.X + b.rectangle.Width);

            int y1 = Math.Max(a.rectangle.Y, b.rectangle.Y);
            int y2 = Math.Min(a.rectangle.Y + a.rectangle.Height, b.rectangle.Y + b.rectangle.Height);

            for (int y = y1; y < y2; ++y)
            {
                for (int x = x1; x < x2; ++x)
                {
                    Color ca = bitsA[(x - a.rectangle.X) + (y - a.rectangle.Y) * a.rectangle.Width];
                    Color cb = bitsB[(x - b.rectangle.X) + (y - b.rectangle.Y) * b.rectangle.Width];

                    if (ca.A != 0 && cb.A != 0)
                    {
                        Colisao colisao = new Colisao();
                        colisao.colidiu = true;
                        colisao.local   = new Vector2(x, y);
                        return(colisao);
                    }
                }
            }

            return(new Colisao());
        }
Exemple #3
0
        private void preencherSentidoColisao(Colisao colisao, GameObject obj)
        {
            Vector2 centro = getCentroGameObject(obj);

            if (colisao.local.X > centro.X)
            {
                colisao.colisaoDireita = true;
            }
            if (colisao.local.X < centro.X)
            {
                colisao.colisaoEsquerda = true;
            }
            if (colisao.local.Y > centro.Y)
            {
                colisao.colisaoBaixo = true;
            }
            if (colisao.local.Y < centro.Y)
            {
                colisao.colisaoCima = true;
            }
        }
 public virtual void colisaoDetectada(GameObject obj, Colisao colisao)
 {
 }