×

Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Checking for multiple sprite collision

    Hi,

    I have an enemy class in my game and I create multiple enemy instances from my main class.

    I am now trying to add a method in the enemy class to check for a collision with another enemy instance. Because there could be a variable number of enemies at one time I need my method to be able to loop through all the other enemy instances and check for a collision with each one.

    In my main class I have appended each enemy to a LayerManager and I have tried to setup the enemy class constructor with a layermanager reference as follows:

    public class Enemy {

    LayerManager manager;

    public Enemy(LayerManager parent) // LayerManager is passed in by main class
    {
    this.manager = parent; // Set this enemy instance layer manager to be the layer manager passed in by main class
    //Other constructor code
    }

    I then tried to create a method to loop through the other items in the layer manager and check for a tile collision as follows:

    public boolean checkspritecollision(Sprite sprite)
    {
    for(int i=0;i<manager.getSize();i++)
    {
    if ((x == manager.getLayerAt(i).getX()) & (y == manager.getLayerAt(i).getX()))
    {
    spritecollision = true;
    }
    }
    return spritecollision;
    }

    // Other enemy methods
    } // end enemy class

    In my main class I create each enemy instance and pass in the main layer manager as follows:

    for(int i=0;i<enemy.length;i++) // loop through each position and create an enemy instance
    {
    enemy[i] = new Enemy(manager); // manager is the main layer manager that each enemy instance is appended to
    }

    However when I call the checkspritecollision() method from within the enemy method the enemy sprites are no longer moved.

    Is there a problem with my logic here? Am I correct to pass in the main layer manager as a reference to each enemy instance?

    Thanks in advance for any help.

    Phil

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Checking for multiple sprite collision

    Hello phil_mw60,

    I am now trying to add a method in the enemy class to check for a collision with another enemy instance. Because there could be a variable number of enemies at one time I need my method to be able to loop through all the other enemy instances and check for a collision with each one
    Take all the enemies sprites (sprite array) and take and the main hero of the game's sprtie.Then you can detect the collision between the enemies itself,something like this algo,
    for(int i=0;i<enemiesSprite.length;i++)
    {
    if(enemiesSprite[i].collidesWith(enemiesSprite[i+1]))
    {
    // do something here//
    }
    }

    and if you wanna collision detection for the enemies and the hero then you can do this,

    For(i=0;i<enemiesSprite.length;i++)

    {

    if(enemiesSprite[i].collidesWith(heroSprite, boolean pixelLevel))
    {
    // do something here//

    }

    }
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    16

    Re: Checking for multiple sprite collision

    use collidesWith() api of sprire.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Checking for multiple sprite collision

    Hi raj_J2ME ,

    Thanks for the useful advice.

    Take all the enemies sprites (sprite array) and take and the main hero of the game's sprtie.Then you can detect the collision between the enemies itself,something like this algo,
    for(int i=0;i<enemiesSprite.length;i++)
    {
    if(enemiesSprite[i].collidesWith(enemiesSprite[i+1]))
    {
    // do something here//
    }
    }
    Wouldn't this just check for a collision between each sprite and the next sprite in the array? e.g Does Sprite 1 collide with Sprite 2, does Sprite 2 collide with Sprite 3 etc..

    What would happen if Sprite 1 collided with Sprite 3 in this example? Would the above code detect this?

    Thanks,

    Phil

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Checking for multiple sprite collision

    Quote Originally Posted by phil_mw60 View Post
    Wouldn't this just check for a collision between each sprite and the next sprite in the array? e.g Does Sprite 1 collide with Sprite 2, does Sprite 2 collide with Sprite 3 etc..
    Phil
    Hey you are correct indeed,
    As I said this was just a algorithm for the collision detection.Rather you can manage that in a way the method check the collision for each and every one.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Checking for multiple sprite collision

    Hi raj_J2ME ,

    I'm afraid I don't understand fully...

    If I put that bit of code in a method in my enemy class, would I need to do the actual check from my main class?

    The problem comes because at the moment there are methods in my enemy class for moving each enemy on the X and Y axis. I already do a check during this movement to see if the new co-ords would collide with a Tiled Layer and if they do, I set X = Original_X and Y = Original_Y ie undo the move.

    therefore in my main class I just issue the following bit of code:
    enemy[i].movex;
    enemy[i].movey;

    I would like to be able to check for a collision between enemy instances from within my enemy class. I think part of the problem is allowing my enemy class to be able to access the enemy instances that are created in my main class.

    Thanks for any clarification on this.

    Phil

  7. #7
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Checking for multiple sprite collision

    In your main class you must be having all the enemies created so far and the hero too...

    You can continuously check the collision in the main class's thread....thats simple,

    public boolean checkEnemesCollision()
    {

    for(int i=0;i<numberEnemies.length;i++)
    {

    if(heroSprite.collidesWith(enemiesSprite[i]),true)
    return true
    }
    }
    Just improve this method and call this in an recursive loop, in order to have the collision detection around the game.

    is this the answer for your post?
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Checking for multiple sprite collision

    Hi Raj,

    I've sent you an email with further details, hope you don't mind.

    I think you might need to look at the code itself to see what I'm trying to do.

    No worries if you are busy, I think I need to read up on the basics of class interaction and then I can hopefully come up with a solution myself!

    Thanks,

    Phil

  9. #9
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: Checking for multiple sprite collision

    Will check this night
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    19

    Re: Checking for multiple sprite collision

    Hi Raj,

    I think I've managed to figure out the necessary logic! I've put the logic in my main class which is able to loop through each of the enemy instances as follows:

    Code:
    for(int i=0;i<enemy.length;i++) // Loop through each of the enemies and update location
            {
    
                enemy[i].original_x = enemy[i].x;    // Set the current co-ords in case we need to move sprite back
                enemy[i].original_y = enemy[i].y;
    
                enemy[i].moveEnemyX();               // Call the enemy.moveEnemyX() method
    
                for(int j=0;j<enemy.length;j++)  // loop through each of the enemy instances and check for collision
                {
                  if ((enemy[i].sprite.collidesWith(enemy[j].sprite, false)) & (i != j)) // Ignore collision with self
                  {
                  enemy[i].x = enemy[i].original_x;  
                  }
                }
    
             }
    I then have a enemy.movey method and do use the same logic to check for a collision between enemies after moving the Y-Co-ords and this seems to work ok.

    Thanks for your help. Please let me know if you can think of a better way to do it but I think this should allow for efficient collision detection between multiple sprites.

    Phil

  11. #11
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: Checking for multiple sprite collision

    Hey,

    That,s great to see..good logic.
    However you can keep in this collision detection method just the collision detection and can keep the movement of the enemies in the separate method.quoted code be inside the different method.
    enemy[i].original_x = enemy[i].x; // Set the current co-ords in case we need to move sprite back
    enemy[i].original_y = enemy[i].y;

    enemy[i].moveEnemyX();
    Though this is a nice implementation.
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. MBM image is not found
    By tamhanna in forum Symbian
    Replies: 14
    Last Post: 2009-04-20, 12:44
  2. Error in packaging process :(
    By a-eqla3 in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2008-07-24, 20:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×