5 Replies - 2150 Views - Last Post: 10 October 2012 - 12:03 PM

#1 mxa92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Problem with collision detection with an Array

Posted 07 October 2012 - 04:15 AM

I have an Array of enemies that move along the top of the screen, hit the edges, move down, and change direction (Like Space Invaders).

However, the collision detection is only working with the first and third enemy in the array. If I 'shoot' and destroy these 2 enemies, the remaining 2 enemies don't collide with the wall and continue left/right forever....

public struct EnemyData
    {
        public Vector2 Position;
        public bool IsAlive;
        public float Angle;
        public float Power;
        public Color Color;
        public Vector2 velocity;
    }

bool isDecending;
bool isRight;
int screenWidth;
int screenHeight;

EnemyData[] enemies;
int numberOfEnemies = 4;

private void SetUpEnemies()
        {
            Color[] enemyColors = new Color[10];
            enemyColors[0] = Color.Red;
            enemyColors[1] = Color.Green;
            enemyColors[2] = Color.Blue;
            enemyColors[3] = Color.Purple;
            enemyColors[4] = Color.Orange;
            enemyColors[5] = Color.Indigo;
            enemyColors[6] = Color.Yellow;
            enemyColors[7] = Color.SaddleBrown;
            enemyColors[8] = Color.Tomato;
            enemyColors[9] = Color.Turquoise;

            enemies = new EnemyData[numberOfEnemies];
            for (int i = 0; i < numberOfEnemies; i++)
            {
                enemies[i].IsAlive = true;
                enemies[i].Color = enemyColors[i];
                enemies[i].Angle = MathHelper.ToRadians(0);
                enemies[i].Power = 250;
                enemies[i].velocity = new Vector2(8, 3);
            }

            enemies[0].Position = new Vector2(50, (screenHeight / 5));
            enemies[1].Position = new Vector2(100, (screenHeight / 5));
            enemies[2].Position = new Vector2(150, (screenHeight / 5));
            enemies[3].Position = new Vector2(200, (screenHeight / 5));

        }

protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);     
enemy1Tex = Content.Load<Texture2D>("Textures\\enemy1");   

SetUpEnemies();
}


private void UpdateEnemies()
        {

            if (!isDecending)
            {
                for (int i = 0; i < numberOfEnemies; i++)
                {
                    if (isRight) enemies[i].Position.X += 4;
                    else enemies[i].Position.X -= 4;
                }


                {
                    for (int i = 0; i < numberOfEnemies; i++)
                    {
                        bool hitLeft = enemies[i].Position.X == 10;
                        bool hitRight = enemies[i].Position.X == screenWidth - 42;
                        if ((hitLeft) || (hitRight))
                        {
                            isRight = !isRight;
                            isDecending = true;
                        }
                    }
                }
            }

            else
            {
                isDecending = false;

                for (int i = 0; i < numberOfEnemies; i++)
                {
                    enemies[i].Position.Y += enemy1Tex.Height / 2;
                }
            }

        }

private void DrawEnemies()
        {
            foreach (EnemyData enemy in enemies)
            {
                if (enemy.IsAlive)
                {

                    int xPos = (int)enemy.Position.X;
                    int yPos = (int)enemy.Position.Y;

                    Vector2 enemyOrigin = new Vector2(enemy1Tex.Width / 2, enemy1Tex.Height / 2);


                    spriteBatch.Draw(enemy1Tex, new Vector2(xPos + 20, yPos - 10), null, enemy.Color, enemy.Angle, enemyOrigin, enemyScaling, SpriteEffects.None, 1);
                }
            }
        }



Thank you! I just can't figure out what i'm doing wrong! I'm checking through every enemy every time they might be colliding via a if loop?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with collision detection with an Array

#2 mxa92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Problem with collision detection with an Array

Posted 07 October 2012 - 07:20 AM

EDIT: I'm assuming it's something wrong with this part of the code:

bool hitLeft = enemies[i].Position.X == 10;
                            bool hitRight = enemies[i].Position.X == screenWidth - 42;



as when I change the numbers (ie. change 42 to 35) none of the invaders never collide with the edge and carry on forever :S
Was This Post Helpful? 0
  • +
  • -

#3 mxa92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Problem with collision detection with an Array

Posted 07 October 2012 - 02:27 PM

EDIT2: I've been adding more enemies in and have found out that when an invader is at the same X-coordinate but a different Y-coordinate they don't collide with the edge of the screen, but if I shoot and destroy the invader below, the invader on top then collides as it should

:/
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 586
  • View blog
  • Posts: 1,306
  • Joined: 24-April 12

Re: Problem with collision detection with an Array

Posted 08 October 2012 - 06:23 AM

Why are you using ==? Why don't you use =< 10 and >= screenWidth - 42? It sounds like it's not hitting that number perfectly because of the movement rate and the condition is only met when it hits it perfectly.
Was This Post Helpful? 0
  • +
  • -

#5 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with collision detection with an Array

Posted 09 October 2012 - 02:34 PM

I thought with space invaders each row of enemies would be moving in the opposite direction from the row above/below and would keep alternating as they descended, you might need a specific version of isRight in the EnemyData structure so each individual enemy can be coded to move in the same direction as with all the others on the same row.

> > > > > > > > > >
< < < < < < < < < <
> > > > > > > > > >
< < < < < < < < < <
> > > > > > > > > >

^

This post has been edited by ShadowsEdge19: 09 October 2012 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 mxa92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-November 11

Re: Problem with collision detection with an Array

Posted 10 October 2012 - 12:03 PM

View PostBBeck, on 08 October 2012 - 06:23 AM, said:

Why are you using ==? Why don't you use =< 10 and >= screenWidth - 42? It sounds like it's not hitting that number perfectly because of the movement rate and the condition is only met when it hits it perfectly.


I've changed it but the same problem is happening :/



View PostShadowsEdge19, on 09 October 2012 - 02:34 PM, said:

I thought with space invaders each row of enemies would be moving in the opposite direction from the row above/below and would keep alternating as they descended, you might need a specific version of isRight in the EnemyData structure so each individual enemy can be coded to move in the same direction as with all the others on the same row.

> > > > > > > > > >
< < < < < < < < < <
> > > > > > > > > >
< < < < < < < < < <
> > > > > > > > > >

^


Hmmm... all rows definitely move in the same direction in Space Invaders :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1