Jump to content

Game collision problem

- - - - -

  • Please log in to reply
5 replies to this topic

#1
sonar87

sonar87

    Newbie

  • Members
  • PipPip
  • 15 posts
In the game I'm working on I'm trying to detect when a bullet hits the player. To do this I decided to get the position of each of the corners of the bullet image. Then check each of the points to see if they where between each corner of the player image. My player, however, can still walk through bullets. I've probably just been at this too long and will feel stupid for asking, but does anyone see anything wrong with this method?

        public static int BulletCollisionCheck(Templates.Projectiles.Bullet bulletx)

        {

            bulletpoints[0] = (int)bulletx.Position.X;

            bulletpoints[1] = (int)bulletx.Position.Y;

            bulletpoints[2] = (int)bulletx.Position.X + bulletx.Image.Width;

            bulletpoints[3] = (int)bulletx.Position.Y + bulletx.Image.Height;


            foreach (int point in bulletpoints)

            {

                if (point > (int)Player1.Position.X

                    && point < (int)Player1.Position.X + (int)Game1.CharacterWidth

                    && point > (int)Player1.Position.Y

                    && point < (int)Player1.Position.Y + (int)Game1.CharacterHeight)

                { 

                    errorscreen.Show("player hit", false);

                    break;

                }

            }


            return 0;

        }


#2
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts

sonar87 said:

My player, however, can still walk through bullets.
Does the errorscreen.Show get called if the player gets hit by a bullet?

A question from me here, Are you using XNA or normal C#?
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The problem is that "point" is actually only half the information of a point. Either X or Y.
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:

if (point[B][SIZE=4].x[/SIZE][/B] > (int)Player1.Position.X

  && point.[SIZE=4][B]x[/B][/SIZE] < (int)Player1.Position.X + (int)Game1.CharacterWidth

  && point.[B][SIZE=4]y[/SIZE][/B] > (int)Player1.Position.Y

* && point.[B][SIZE=4]y[/SIZE][/B] < (int)Player1.Position.Y + (int)Game1.CharacterHeight)


#4
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts

wim DC said:

The problem is that "point" is actually only half the information of a point. Either X or Y.
You gonna want a class that holds BOTH x and y, and use that object to do the if-test like so:

if (point[B][SIZE=4].x[/SIZE][/B] > (int)Player1.Position.X

  && point.[SIZE=4][B]x[/B][/SIZE] < (int)Player1.Position.X + (int)Game1.CharacterWidth

  && point.[B][SIZE=4]y[/SIZE][/B] > (int)Player1.Position.Y

* && point.[B][SIZE=4]y[/SIZE][/B] < (int)Player1.Position.Y + (int)Game1.CharacterHeight)

His bulletpoints array is holding the X and Y of the bullet, Isn't the foreach loop letting him skip the point.X and point.Y ?
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
His foreach is fine, but when the "point" in the foreach has a X-value, then this if should be done:

if (point > (int)Player1.Position.X   &&   point < (int)Player1.Position.X + (int)Game1.CharacterWidth)

However, if "point" contains a Y-value, it should check:

if( point.y > (int)Player1.Position.Y   &&   point.y < (int)Player1.Position.Y + (int)Game1.CharacterHeight)


With the current code it is not possible with a foreach, or else you'll make it ugly(/ier) by doing:


          boolean x = true;


          foreach (int point in bulletpoints)

            {

                if (x && point > (int)Player1.Position.X && point < (int)Player1.Position.X + (int)Game1.CharacterWidth)

                {  

                    x = !x;    

                    errorscreen.Show("player hit", false);               

                    break;

                }

                if (!x && point > (int)Player1.Position.Y   &&   point < (int)Player1.Position.Y + (int)Game1.CharacterHeight

                { 

                    x = !x;    

                    errorscreen.Show("player hit", false);

                    break;

                }

            }



#6
sonar87

sonar87

    Newbie

  • Members
  • PipPip
  • 15 posts
alright guys, I figured it out. Was thinking too much about how to compare the points to bother giving a thought to what a point even is I guess. Thanks.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users