Andre Lopes
|
 |
«
Posted
2013-05-12 01: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 ((m1.posY) > screenHeight) { missiles.remove(m1); System.out.println("missile removed!" + m1.id); }
missileSprite.getBoundingRectangle().setX(m1.posX); missileSprite.getBoundingRectangle().setY(m1.posY); for (int k = 0; k < enemyPlanes.size(); k++) { EnemyPlane ep1 = enemyPlanes.get(k); enemyPlaneSprite.getBoundingRectangle().setX(ep1.getPosX()); enemyPlaneSprite.getBoundingRectangle().setY(ep1.getPosY()); 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 ((m1.posY) > screenHeight) { missiles.remove(m1); System.out.println("missile removed!" + m1.id); }
missileSprite.getBoundingRectangle().setX(m1.posX); missileSprite.getBoundingRectangle().setY(m1.posY); for (int k = 0; k < enemyPlanes.size(); k++) { EnemyPlane ep1 = enemyPlanes.get(k); enemyPlaneSprite.getBoundingRectangle().setX(ep1.getPosX()); enemyPlaneSprite.getBoundingRectangle().setY(ep1.getPosY()); 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  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?
|
|
|
|
alaslipknot
|
 |
«
Reply #1 - Posted
2013-05-12 02: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  ) 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."
|
|
|
wreed12345
|
 |
«
Reply #2 - Posted
2013-05-12 02: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!
|
|
Andre Lopes
|
 |
«
Reply #3 - Posted
2013-05-12 02: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?
|
|
|
|
wreed12345
|
 |
«
Reply #4 - Posted
2013-05-12 02: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
|
|
|
|
Andre Lopes
|
 |
«
Reply #5 - Posted
2013-05-12 02:29:17 » |
|
I did and it didnt work sometimes.
|
|
|
|
wreed12345
|
 |
«
Reply #6 - Posted
2013-05-12 02:31:44 » |
|
When did those sometimes cases occur
|
|
|
|
alaslipknot
|
 |
«
Reply #7 - Posted
2013-05-12 02: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."
|
|
|
Andre Lopes
|
 |
«
Reply #8 - Posted
2013-05-12 02:55:30 » |
|
Guys , it worked. i had to do several modifications but the overlaps method works the same than intersect. Thanks  -- Thanks all, seriously  now im finnally being able to make this little games which was impossible before.
|
|
|
|
Jimmt
|
 |
«
Reply #9 - Posted
2013-05-12 03: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!
|
|
wreed12345
|
 |
«
Reply #10 - Posted
2013-05-12 03:07:43 » |
|
Do I get an appreciation? 
|
|
|
|
|
Jimmt
|
 |
«
Reply #12 - Posted
2013-05-12 03:08:43 » |
|
Do I get an appreciation?  Cmon, this is like the first result on google search.
|
|
|
|
Andre Lopes
|
 |
«
Reply #13 - Posted
2013-05-12 03:10:05 » |
|
Do I get an appreciation?  i clicked appreciate but idk wth happened because i cant appreciate all the answers , just one.... What this do anyway just tell me how and i will Appreciate you LOL ( not in a sexual way , ok?  ) NO jokes;
|
|
|
|
Jimmt
|
 |
«
Reply #14 - Posted
2013-05-12 03:13:57 » |
|
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.
|
|
|
|
Andre Lopes
|
 |
«
Reply #15 - Posted
2013-05-12 03:20:03 » |
|
Alright, i will try to find a tutorial about that. I need some theory before applying the code 
|
|
|
|
65K
|
 |
«
Reply #16 - Posted
2013-05-12 06:17:00 » |
|
Think about what happens when you remove list items the way you do while iterating with a for loop.
|
Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
|
|
|
Andre Lopes
|
 |
«
Reply #17 - Posted
2013-05-12 13: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  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  ? -- Also, im having a little hard time with texture atlas, anyone have a good tutorial or a step-by-step ?
|
|
|
|
kutucuk
|
 |
«
Reply #18 - Posted
2013-05-12 13: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=576He 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.
|
|
|
|
65K
|
 |
«
Reply #19 - Posted
2013-05-12 13: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  ? That would not fix the bug...
|
Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
|
|
|
Andre Lopes
|
 |
«
Reply #20 - Posted
2013-05-12 15:11:24 » |
|
hm, what would be the bug ? Synch issues?
|
|
|
|
Andre Lopes
|
 |
«
Reply #21 - Posted
2013-05-12 15: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  please  This is how the class is : 
|
|
|
|
kutucuk
|
 |
«
Reply #22 - Posted
2013-05-12 18: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.
|
|
|
|
65K
|
 |
«
Reply #23 - Posted
2013-05-13 04: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(i); } } } } |
|
Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
|
|
|
|