Rectangle Intersection - LIBGDX -- Collision

Hi Guys. Im having problems to check the collision between my missile and my enemy plane.
In java2D i have the intersect method, but it aint working correctly. Does libgdx have an intersection method?

heres the exactly part of the code that i need to fix :

  for (int i = 0; i < missiles.size(); i++) {
            Missile m1 = missiles.get(i);

            
            //If is out of Screen Bound
            if ((m1.posY) > screenHeight) {
                missiles.remove(m1);
                System.out.println("missile removed!" + m1.id);
            }

            missileSprite.getBoundingRectangle().setX(m1.posX);
            missileSprite.getBoundingRectangle().setY(m1.posY);
            
            //If missile hit enemyplane
            for (int k = 0; k < enemyPlanes.size(); k++) 
            {
                EnemyPlane ep1 = enemyPlanes.get(k);
                
                enemyPlaneSprite.getBoundingRectangle().setX(ep1.getPosX());
                 enemyPlaneSprite.getBoundingRectangle().setY(ep1.getPosY());
               
                 /**FIX ME :D */
                if(Intersector.(enemyPlaneSprite.getBoundingRectangle()))
                {
                    gameupdate.getShooterGame().getMissile().getMissiles().remove(m1);
                    gameupdate.getShooterGame().getEnemyPlanes().getEnemyPlanes().remove(ep1);
                    System.out.println("Yay,Enemy Plane was hit!");
                    gameupdate.getShooterGame().addNewEnemyPlane();
                }
               
            }

        }

Heres the whole method :

 public void checkCollision() {
        ArrayList<Missile> missiles = gameupdate.getShooterGame().getMissile().getMissiles();
        Plane plane = gameupdate.getShooterGame().getPlane();
        ArrayList<EnemyPlane> enemyPlanes = gameupdate.getShooterGame().getEnemyPlanes().getEnemyPlanes();

        //
        int screenHeight = Gdx.graphics.getHeight();
        int width = Gdx.graphics.getWidth();
        //

        for (int i = 0; i < missiles.size(); i++) {
            Missile m1 = missiles.get(i);

            
            //If is out of Screen Bound
            if ((m1.posY) > screenHeight) {
                missiles.remove(m1);
                System.out.println("missile removed!" + m1.id);
            }

            missileSprite.getBoundingRectangle().setX(m1.posX);
            missileSprite.getBoundingRectangle().setY(m1.posY);
            
            //If missile hit enemyplane
            for (int k = 0; k < enemyPlanes.size(); k++) 
            {
                EnemyPlane ep1 = enemyPlanes.get(k);
                
                enemyPlaneSprite.getBoundingRectangle().setX(ep1.getPosX());
                 enemyPlaneSprite.getBoundingRectangle().setY(ep1.getPosY());
               
                 /**FIX ME :D */
                if(Intersector.(enemyPlaneSprite.getBoundingRectangle()))
                {
                    gameupdate.getShooterGame().getMissile().getMissiles().remove(m1);
                    gameupdate.getShooterGame().getEnemyPlanes().getEnemyPlanes().remove(ep1);
                    System.out.println("Yay,Enemy Plane was hit!");
                    gameupdate.getShooterGame().addNewEnemyPlane();
                }
               
            }

        }


    }

Does anyone have a light for this :stuck_out_tongue:
And yes i did research but
http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/math/Intersector.html#intersectRectangles(com.badlogic.gdx.math.Rectangle, com.badlogic.gdx.math.Rectangle)

static boolean intersectRectangles(Rectangle a, Rectangle b)
Returns wheter the two rectangles intersect

doesnt exists?

hello,
i don’t know if libGdx has a build method to do the collision detection, but when i moved to lwjgl and needed the intersects method from java2D i had to created one by myself (check this topic)
and here is the code i’ve wrote (the variable are self expressed, w=width and h=height in case you wondered :stuck_out_tongue: )


public boolean aabb(double x1, double y1, double w1, double h1, double x2,
			double y2, double w2, double h2) {
		
		double cntrX1 = x1 + w1 / 2;
		double cntrY1 = y1 + h1 / 2;

		double cntrX2 = x2 + w2 / 2;
		double cntrY2 = y2 + h2 / 2;

		double distanceX = Math.abs(cntrX1 - cntrX2);
		double distanceY = Math.abs(cntrY1 - cntrY2);

		double sumWidth = (w1 / 2 + w2 / 2);
		double sumHeight = (h1 / 2 + h2 / 2);

		if (distanceX < sumWidth && distanceY < sumHeight) {
			
			return true;

		}
		return false;

	}

i hope it helped

To use the libgdx rectangle intersect method, first import

import com.badlogic.gdx.math.Intersector;

and then to use the method type

Intersector.overlaps(r1, r2);

where r1 is your first rectangle and r2 is your second rectangle. Its a boolean method so use it appropriately.

yes but overlaps means that intersects or it means that one rectangle is totally inside another?

I would have to assume intersects. I don’t see how completely inside each other would ever be useful. Try it out it’s not hard to use

I did and it didnt work sometimes.

When did those sometimes cases occur

maybe you should try it with single object, just do a simple test between your player and another rectangle, if it works then the problem is in the way you call it

Guys , it worked. i had to do several modifications but the overlaps method works the same than intersect.

Thanks :stuck_out_tongue:

Thanks all, seriously :smiley: now im finnally being able to make this little games which was impossible before.

I know this is basic, but if you want advanced and easy 2d collision, look into box2d - comes with libgdx!

Do I get an appreciation? ;D

I will study that as soon as i learn how to mess with Sprites sequence and make a funny effect when the missile hits the enemy plane.

Btw, i found this to study :
https://code.google.com/p/libgdx/wiki/SpriteAnimation

https://code.google.com/p/libgdx-users/wiki/Sprites

Is the the correct way or theres a simple way with .GIF, idk.

Cmon, this is like the first result on google search.

i clicked appreciate but idk wth happened because i cant appreciate all the answers , just one…
What this do anyway :stuck_out_tongue:

just tell me how and i will Appreciate you LOL ( not in a sexual way , ok? :stuck_out_tongue: ) NO jokes;

That way is honestly not that complicated, but there is another (better) way - packing your images into a texture atlas using texturepacker2, and then loading the desired frames into an array - code here.

May be a bit difficult to read through, but basically - you find the desired regions in the atlas (because you don’t need animation images of a monster in the player class, for example), load them into an array, and then cycle through the array and put each frame into an animation. Still fairly? somewhat complicated, but makes things easier in the long term when your game gets bigger.

Alright, i will try to find a tutorial about that. I need some theory before applying the code :stuck_out_tongue:

Think about what happens when you remove list items the way you do while iterating with a for loop.

Thanks for all the answers :stuck_out_tongue:

I thought about that, i think i should remove the item from the list using the “int i” and “int k” instead of using the object as reference.

Thats what you meant :stuck_out_tongue: ?


Also, im having a little hard time with texture atlas, anyone have a good tutorial or a step-by-step ?

Someone in the libgdx irc channel taught me a little about texture atlases. He sent me this code (I am sorry I can’t remember his nickname - He let me use the code):
http://www.java-gaming.org/?action=pastebin&id=576

He packs the images into TextureAtlas using PixmapPacker and keeps their names and references in a HashMap. So that when you need to call them, you just go ahead and call Atlas.get(“nameOfTheImage”);

But if you did not store the names in a HashMap like my helper did, you could get them by calling: AtlasRegion region = atlas.findRegion(“tank”);

I hope you find it useful.

That would not fix the bug…