Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Handling collision and the removal of objects

Options
  • 29-05-2012 11:33am
    #1
    Closed Accounts Posts: 364 ✭✭


    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?


Comments

  • Registered Users Posts: 1,235 ✭✭✭Odaise Gaelach


    I get the feeling that this is a bit bigger than just that one class you've shown. But I'm assuming that you're working on a 2D shooter, like a top-down one.

    I tried to make one of those ages ago. Riemers XNA Tutorials are really useful. In that one he makes a 2D shooter game and it's got three sections in it on collision detection that show how it works. They should answer all your questions. :)


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Maths problem, not programming problem. Maths is hard and stuff for this.

    Easy way to start = just use circles. Easy to detect intersection. Problem then is continuous vs discrete testing, ie what happens if objects move so fast they go all the way through each other in one frame. Then do it for polygons >.< Then do it in 3d :(


  • Closed Accounts Posts: 364 ✭✭Xylophonic


    I have began looking at that riemers website. I have also looked into that circle method too.

    Thanks for the help.


  • Registered Users Posts: 627 ✭✭✭Dboy85


    You could implement some fancy jazz like an octree and split the screen space into it. That way your first check would be, are 2 objects leaf nodes of the same. Then, like suggested before, use the distance formula and check if the two radii combined are greater than the distance between for a spherical bounding box on each of the objects. Then you're into near phase stuff testing Separating Axis theorem for convex polygons and deeper for convex. It is a more math problem than a programming but these techniques are well documented in many languages so not hard to copy paste if you want to stay ignorant to the math. The same stuff applies to 3d in most cases and a good source of information can be found on NeHe's openGl tutorial website here.


Advertisement