3 Replies - 382 Views - Last Post: 07 March 2013 - 09:26 AM Rate Topic: -----

#1 aprilrocks92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-February 13

Collision detection bullet-asteroids(rocks)

Posted 07 March 2013 - 04:13 AM

I have been working on the following method for collision detection between a spaceship, its bullets and the asteroids in a 2D game environment. However, I am not getting the proper collision detection effect, as it seems like the collisions are only detected occasionally. Once detected, there is no problem, points increment appropriately.

How can I improve my collision detection to properly function? Thanks!

		public void CheckCollisionRockShip ()
		{
			foreach (Rocks rock in rocks) {
				if(spriteRectangle.Intersects(rock.rockRectangle)){
					rock.isVisible = false;
					Points += 1;
				}
			}
		}
		public void CheckCollisionBulletRock ()
		{
			foreach (Bullets bullet in bullets) {
				foreach(Rocks rock in rocks) {
					if(bullet.bulletRectangle.Intersects(rock.rockRectangle)){
					rock.isVisible = false;
					}
				}
			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Collision detection bullet-asteroids(rocks)

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Collision detection bullet-asteroids(rocks)

Posted 07 March 2013 - 06:54 AM

There doesn't seem to be enough information. How do you know that a collision should have been detected, but was not correctly detected? Does your Intersects() method take visibility into account or does it just do raw bounds checking?
Was This Post Helpful? 0
  • +
  • -

#3 aprilrocks92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-February 13

Re: Collision detection bullet-asteroids(rocks)

Posted 07 March 2013 - 08:10 AM

View PostSkydiver, on 07 March 2013 - 06:54 AM, said:

There doesn't seem to be enough information. How do you know that a collision should have been detected, but was not correctly detected? Does your Intersects() method take visibility into account or does it just do raw bounds checking?


When I fly the spaceship over a rock, or fire a bullet at a rock, sometimes the rock disappears, and sometimes it does not.

I have the following variables related to the spaceship:
 
shipRectangle = new Rectangle ((int)shipPosition.X, (int)shipPosition.Y, shipTexture.Width, shipTexture.Height);
shipPosition = shipVelocity + shipPosition;
shipOrigin = new Vector2 (shipRectangle.Width / 2, shipRectangle.Height / 2);



rocks
Rectangle rockRectangle = new Rectangle ((int)position.X, (int)position.Y, texture.Width, texture.Height);



bullets
Rectangle bulletRectangle = new Rectangle((int)positionBullet.X, (int)positionBullet.Y, texture.Width, texture.Height);


Was This Post Helpful? 0
  • +
  • -

#4 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Collision detection bullet-asteroids(rocks)

Posted 07 March 2013 - 09:26 AM

Hey, cool, I'm working on a game in C# as well. Are you using XNA?

Anyway, I'm a little confused as to why you have multiple collision checking methods. I'm assuming you have a base "sprite" class, and my feeling is you should just have one collision detection method there.

Here's what I do:

public virtual bool detectCollision(Rectangle otherRectangle)
        {
            return otherRectangle.Intersects(this.collisionRectangle);
        }



Simple, right? Then, in my Update method, I call any appropriate checks (for instance, a for loop through the alien lasers that checks to see if they intersect with the player ship). If it returns true, I fire off the appropriate event. This works really well in general, though I plan to do an override later for less rectangle-y shaped objects that's more accurate.

Doing it this way will also keep you from having to pass the entire rocks and bullets list to the playerShip.

If you want to browse my code, you can see it at github. I want to warn you, it's not really a "game" yet, just the foundation of one, but the collision detection works fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1