Shihab Uddin
Shihab Uddin

Reputation: 6931

collision detection with two same sprite object andengine?

I am developing a game where a single sprite are generated continuously on the scene with a random path. I apply path modifier to them. During random movement on the scene, one sprite overlap with other. I want to stop this overlapping.

Instantly, I find two solution. (May be)

First one: this reason I thought if I detect collision between them & then give a new path may be solved.

Second one: if I pause the path modifier for 2/3 seconds when a collision occur, and move again after resumed the path modifier.(is it possible?)

I tried First approach Now. Problem: I can't identify sprite uniquely to iterate inside an Update handler. I don't use layer to attach.

I use generic pool to generate & recycle them.

Code:

public Iterator<EasyEnemy> eIt;

registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            try {
                // ===get EasyEnemy List from it===
                eIt = getEasyEnemyIterator();
                while (eIt.hasNext()) {
                    EasyEnemy ee = eIt.next();

                    // need some help here 
                    if (ee.collidesWith(ee)) {

                        // this doesn't work.

                    }
                }
            } catch (Exception e) {
            }
        }

        @Override
        public void reset() {

        }
    });

I try best to focus my problem. If some one have better solutions please help can help me.

Upvotes: 1

Views: 2255

Answers (1)

Jawad Amjad
Jawad Amjad

Reputation: 2552

the ee variable describes an object which you want to check collision. Where is the other object to which ee will collide? Basically you are checking ee collides with ee.

An object will never collide with it self :)

You need to have two different objects to check collision between them. Like this in case you have all objects in an ArrayList.

for(int i = 0;  i< yourList.size()-1;i++)
{
    for(int j = 0;  j< yourList.size()-1;j++)
   {

      if(yourList.get(i)!= yourList.get(j))
        {
          if(yourList.get(i).collideswith(yourList.get(j))
           {
            //do what you want to do on collision :)
           }
         }

   }

 }

Upvotes: 3

Related Questions