Jump to content
  • Advertisement
Sign in to follow this  
Darren Johnson

Handling collision and the removal of objects

This topic is 2270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am in the process of building a simple shooter game. I am currently making a destructible class which will be tasked with handling the removal of objects from the game which have been destroyed by bullets fired.

I think i know how to go about this as I will need to:

  1. Get the index of the most recently fired bullet
  2. Get the positions of the objects in the room
  3. Check for collisions between the bullet and those objects.

I just don't know how to implement this. Also is there anything I am missing?

I have a list of bullets in a bulletManager class, how do I access or call the most recently fired bullet?

Here is the destructible class:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace MainCharacter
{
class Destructible : BaseCollideable
{
Texture2D texture;
Vector2 position;
int health;
bool destroyed = false;
/// <summary>
/// Creates a new destructible object.
/// </summary>
/// <param name="tex">The textures of the object.</param>
/// <param name="pos">The position of the object.</param>
/// <param name="hel">The health of the object.</param>
/// <param name="dst">The status of the object.</param>
public Destructible(Texture2D tex, Vector2 pos, int hel, bool dst)
: base(tex)
{
texture = tex;
position = pos;
health = hel;
destroyed = dst;
}
/// <summary>
/// Updates the destructible object.
/// </summary>
public override void Update()
{
if (destroyed == true)
{
// Remove bullet
// Remove texture
// Deposit a power up in its place
}
base.Update();
}
public void Destroyed()
{
// If bullet collides with object:
// destroyed = true
// else
// destroyed = false
}
public void GetBulletIndex()
{
// Get the index of the most recent bullet fired
}
public void GetObjectPositions()
{
// Get the positions of objects in the current room
}
public void CheckForCollisions()
{
// Check for collisions between the latest
// bullet fired and the object positions
}
public Texture2D Texture
{
get { return texture; }
}
public Vector2 Position
{
get { return position; }
}
public int Health
{
get { return health; }
}
public bool Destroyed
{
get { return destroyed; }
}
}
}


Can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
In general, the way to go about this is:
#1, Have a list of Active Bullets. These bullets are on the screen and moving, and can collide with objects.
#2, have a list of Objects on screen that can collide with the bullets (Enemies, Buildings, etc).

For each Object, loop through the bullets and check if they are intersecting. If so, apply bullet damage to the object. Remove the Bullet from the list, and, if the bullet destroyed the Object, remove the Object as well (or maybe put an exploding animation in it's place).

Share this post


Link to post
Share on other sites
If you have multiple bullets visible on the screen at any point of time, just getting a handle to the latest bullet fired will not cut it for you.

Follow the process, aforementioned by BeerNutts..



In general, the way to go about this is:
#1, Have a list of Active Bullets. These bullets are on the screen and moving, and can collide with objects.
#2, have a list of Objects on screen that can collide with the bullets (Enemies, Buildings, etc).

For each Object, loop through the bullets and check if they are intersecting. If so, apply bullet damage to the object. Remove the Bullet from the list, and, if the bullet destroyed the Object, remove the Object as well (or maybe put an exploding animation in it's place).
Edited by Marvel Magnum

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!