Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rectangle Intersection - LIBGDX -- Collision  (Read 4685 times)
0 Members and 1 Guest are viewing this topic.
Online Andre Lopes
« Posted 2013-05-12 03:57:30 »

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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
  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 :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
 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 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?
Offline alaslipknot
« Reply #1 - Posted 2013-05-12 04:06:28 »

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  Tongue )
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-05-12 04:10:40 »

To use the libgdx rectangle intersect method, first import
1  
import com.badlogic.gdx.math.Intersector;


and then to use the method type

1  
Intersector.overlaps(r1, r2);

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


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Andre Lopes
« Reply #3 - Posted 2013-05-12 04:23:45 »

To use the libgdx rectangle intersect method, first import
1  
import com.badlogic.gdx.math.Intersector;


and then to use the method type

1  
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?
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2013-05-12 04:27:57 »

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

Online Andre Lopes
« Reply #5 - Posted 2013-05-12 04:29:17 »

I did and it didnt work sometimes.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2013-05-12 04:31:44 »

When did those sometimes cases occur

Offline alaslipknot
« Reply #7 - Posted 2013-05-12 04:37:17 »

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 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Online Andre Lopes
« Reply #8 - Posted 2013-05-12 04:55:30 »

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

Thanks Tongue
--
Thanks all, seriously Cheesy now im finnally being able to make this little games which was impossible before.
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2013-05-12 05:04:03 »

I know this is basic, but if you want advanced and easy 2d collision, look into box2d - comes with libgdx!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #10 - Posted 2013-05-12 05:07:43 »

Do I get an appreciation?  Grin

Online Andre Lopes
« Reply #11 - Posted 2013-05-12 05:08:40 »

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.
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #12 - Posted 2013-05-12 05:08:43 »

Do I get an appreciation?  Grin
Cmon, this is like the first result on google search.
Online Andre Lopes
« Reply #13 - Posted 2013-05-12 05:10:05 »

Do I get an appreciation?  Grin

i clicked appreciate but idk wth happened because i cant appreciate all the answers , just one....
What this do anyway Tongue

just tell me how and i will Appreciate you LOL ( not in a sexual way , ok?  Tongue ) NO jokes;
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #14 - Posted 2013-05-12 05:13:57 »

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.


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.
Online Andre Lopes
« Reply #15 - Posted 2013-05-12 05:20:03 »

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


Offline 65K
« Reply #16 - Posted 2013-05-12 08:17:00 »

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

Online Andre Lopes
« Reply #17 - Posted 2013-05-12 15:06:57 »

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

Thanks for all the answers 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 Tongue ?

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

Senior Member


Medals: 5
Exp: 3 years



« Reply #18 - Posted 2013-05-12 15:39:20 »

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.
Offline 65K
« Reply #19 - Posted 2013-05-12 15:41:45 »

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 Tongue ?

That would not fix the bug...

Online Andre Lopes
« Reply #20 - Posted 2013-05-12 17:11:24 »

hm, what would be the bug ? Synch issues?
Online Andre Lopes
« Reply #21 - Posted 2013-05-12 17:26:17 »

Heya.
Well, i tried loading using the method you described and .getTexture();
But it gave some bugs and the image didnt show up. Thought no errors.
Also, after i loaded the missile texture , my method collision kept telling it was happening collisions all the time;

Would you give me another hand Cheesy please Tongue

This is how the class is :

Offline kutucuk

Senior Member


Medals: 5
Exp: 3 years



« Reply #22 - Posted 2013-05-12 20:13:40 »

It returns an AtlasRegion. I guess you can cast it to TextureRegion...

You should call Atlas.generateAtlas() in the show() or create() method first.
Assuming you want to call it from a class which extends Game;
1  
2  
3  
4  
   public void create() {
      Atlas.generateAtlas();
      setScreen(new MyWorld());
   }


Then, because you created and loaded the atlas, you can get any image like this:
1  
AtlasRegion planeImage = Atlas.get("plane");


Notice that the guy who helped me packed the atlas in the runtime. We also can prepare the atlas first and get AtlasRegions from it later in the game. Probably preparing it beforehand is more efficient.
But this way, if you decide to have another image in the atlas, or want to change one of the images, it is easier.

If it is necessary to use a TextureRegion, you can just cast it:
1  
TextureRegion text = (TextureRegion) planeImage;

But I am not sure if this is efficient or right thing to do.


BTW, Rectangle has a method to detect intersections. Assume planeBounds and missileBounds are two rectangles. You can do that:
1  
planeBounds.overlaps(missileBounds);

This returns true or false.
Offline 65K
« Reply #23 - Posted 2013-05-13 06:38:13 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class BadForLoopRemoval {

   public static void main(String[] args) {
      List<String> items = new ArrayList<String>();
      items.add("a");
      items.add("b");
      items.add("c");
      items.add("d");
      items.add("e");

      for (int i = 0; i < items.size(); i++) {
         String item = items.get(i);
         System.out.println(item);
         
         if (item.equals("c")) {
            //            items.remove(item);
           items.remove(i);
         }
      }
   }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (7 views)
2014-07-30 21:08:39

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!