Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  how to check collision between object from the same arrayList  (Read 1129 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-29 13:12:45 »

hello,
i have this code to draw images and rectangles :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
ArrayList enemies = enemy.getE();
      for (int i = 0; i < enemies.size(); i++) {
         Enemy tmpE = (Enemy) enemies.get(i);
         g2d.drawImage(tmpE.getI(), (int) tmpE.getX(), (int) tmpE.getY(),
               tmpE.getW(), tmpE.getH(), this);
         Rectangle tmpR = (Rectangle) tmpE.getBounds();
         g2d.draw(tmpR);
         enemyRects.add(tmpR);

      }


and this code to make them moves :
1  
2  
3  
4  
5  
ArrayList enemies = enemy.getE();
      for (int i = 0; i < enemies.size(); i++) {
         Enemy tmpE = (Enemy) enemies.get(i);
         tmpE.moveForward(hero.getX(), hero.getY());
      }


i want to check if the Rectangles (inside enemyRects) intersects with each other, i tried this
1  
2  
3  
4  
5  
6  
for (int i = 0; i < enemyRects.size(); i++) {
         Rectangle tmpR = (Rectangle) enemyRects.get(i);
         if (tmpR.intersects(tmpR)) {
            System.out.println("Colliding");
         }
      }

but it always print "colliding" cause it's checking the rectangle with it self,
i tried to do this
1  
2  
3  
if (tmpR.intersects((Rectangle) enemyRects.get(i+1))) {
            System.out.println("Colliding");
         }

but i get an out of bounds error
so i want to ask if there is a code that can do this :
1  
2  
3  
if(tmpR!=itSelf && tmpR.intersects(tmpR)){
            System.out.println("Colliding");
         }


thank you

"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 Stranger

Senior Devvie


Medals: 7



« Reply #1 - Posted 2013-04-29 14:00:31 »

I think this could be useful for you.

Anton
Offline alaslipknot
« Reply #2 - Posted 2013-04-29 14:08:27 »

I think this could be useful for you.
yes i tried that before, it lags soooo much !!
and it also keep printing "collide" even when they are not colliding

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-04-29 14:10:25 »

A really simple thing would be to do something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public Enemey getFirstCollision(Enemy theEnemey, ArrayList<Enemey> enemies) {
    ArrayList<Enemey> enemeis = ...;

    for (int i=0; i<enemies.size(); i++) {
        Enemey e = enemies.get(i);
        if (e != theEnemey && theEnemey.intersects(e))
            return e;
    }
    return null;
}


The method returns the first other Enemy object that the given enemy collides with, or null if no collision exists.

Some things you should be doing:

1. Use generics; you should rarely ever need to cast objects like you are doing. e.g.

1  
2  
3  
4  
5  
//now the list only expects type Enemy... 
ArrayList<Enemy> list = new ArrayList<Enemey>();

//it will throw a compiler error if "e" is not of type Enemy
list.add(e);


2. Add important methods like intersects() into your Enemy definition:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class Entity {
     private Rectangle bounds = new Rectangle();
     private BufferedImage image;

     ....

     public boolean intersects(Entity e) {
         return e.bounds.intersects(this.bounds);
     }

     public void draw(Graphics g) {
         g.drawImage(this.image, (int)bounds.x, (int)bounds.y, (int)bounds.width, (int)bounds.height, null);
         g.draw(bounds);
     }
}

...

public class Enemy extends Entity {
   ... enemy specific stuff ...
}


Not only does this result in cleaner code when rendering and checking collisions (since you don't need to touch the Rectangle object at all), but it also is more flexible for future changes. Let's say you want one of your enemies to have a Circle bounds instead of Rectangle, you only need to change their draw() and intersects() methods.

3. Use descriptive method names like getImage() instead of getI(). It's important to code with some "good practice" as it will make your program easier to read/debug, and also less prone to errors.

Offline alaslipknot
« Reply #4 - Posted 2013-04-29 14:27:48 »

@davedes
thanks a lot, that seems pretty good
the only problem now is that once again i had to re-do everything  Undecided

PS :
all i know to do with Java2D is drawing shapes,images and make them move with or without Key and/or Mouse listeners,
if i start learning lwjgl today, how much would it take me to reach that very low level i have now with Java2D ?
cause am starting to feel that am wasting my time here, i know that all my errors are not because of Java2D but sooner or later i had to stop developing with it and be forced to learn more advanced things,
what do you think ?
if i should start learning now, can you please post some "getting started" links ?

thank you

"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 matheus23

JGO Kernel


Medals: 112
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2013-04-29 14:50:41 »

Basically, drawing Images:

1  
2  
3  
public void drawImage(SpriteBatch batch, Sprite sprite) {
    sprite.draw(batch, xposition, yposition);
}

you can even have fun:
1  
2  
sprite.setRotation(100f); // rotated.
sprite.setScale(10, 10); // 10x scaled on both x and y axis


Loading sprites is pretty trivial too:
1  
2  
Texture tex = new Texture("myfile.png");
Sprite sprite = new Sprite(tex);

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline SHC
« Reply #6 - Posted 2013-04-29 14:57:54 »

@matheus23

He is using Java 2D not LibGDX

Offline Roquen
« Reply #7 - Posted 2013-04-29 14:59:00 »

davedes example is n^2.  That's fine if n is small. If it gets larger, you only need to check entities which are ahead of itself in the list as all behind have already checked themselves against the current one.  Larger still: sweep and prune...larger still...spatial partitioning.
Offline matheus23

JGO Kernel


Medals: 112
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2013-04-29 15:00:36 »

He is using Java 2D not LibGDX

It was dedicated to this question:

all i know to do with Java2D is drawing shapes,images and make them move with or without Key and/or Mouse listeners,
if i start learning lwjgl today, how much would it take me to reach that very low level i have now with Java2D ?
cause am starting to feel that am wasting my time here, i know that all my errors are not because of Java2D but sooner or later i had to stop developing with it and be forced to learn more advanced things,
what do you think ?

When you stop quoting once... :/ Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline quew8

JGO Coder


Medals: 31



« Reply #9 - Posted 2013-04-29 15:31:37 »

Basically, drawing Images:

1  
2  
3  
public void drawImage(SpriteBatch batch, Sprite sprite) {
    sprite.draw(batch, xposition, yposition);
]

you can even have fun:
1  
2  
sprite.setRotation(100f); // rotated.
sprite.setScale(10, 10); // 10x scaled on both x and y axis


Loading sprites is pretty trivial too:
1  
2  
Texture tex = new Texture("myfile.png");
Sprite sprite = new Sprite(tex);

For the record this code is libgdx, not lwjgl (although libgdx uses lwjgl on desktop). Libgdx is a cross platform library and it is quite popular. I would consider it a less maths and graphics, more gamey library. lwjgl is more or less just a wrapping around the opengl functions with stuff like input and window creation thrown in.
In terms of lwjgl tutorials, check the wiki: http://www.lwjgl.org/wiki/index.php?title=Main_Page
There are tutorials on how to setup LWJGL, then read the basics section. Then learn about matrices (Nothing on the lwjgl wiki on them). Then read the 3.2 and newer stuff.
Or you can learn libgdx. I don't use it so I can't help you.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 112
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2013-04-29 15:33:17 »

For the record this code is libgdx, not lwjgl (although libgdx uses lwjgl on desktop).

Argh... Forgot to add that...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #11 - Posted 2013-04-29 15:49:53 »

I don't think LWJGL is a good idea for a beginner. LWJGL is very low level; it doesn't include anything like drawImage or drawRect. You have to build all of that yourself.

Instead, I would recommend LibGDX as suggested in many other threads. The good thing about LibGDX is that it tends to enforce better standards; i.e. using screens to make your game more modular, batching sprites, using texture atlases, etc.

Get started here:
http://libgdx.badlogicgames.com/documentation.html

It's also good to look at how other games are written, as it will teach you a lot about "proper coding standards."
https://github.com/libgdx/libgdx/tree/master/demos
https://github.com/siondream/freegemas-gdx
https://github.com/jeremangnr/Frogger-LibGDX

Quote
the only problem now is that once again i had to re-do everything
This is how things tend to go when you are just beginning. You write something, and then realize there is a better way of doing it. Smiley This is why your first couple games should be very simple; think Tic Tac Toe, Pong, etc.

Quote
if i start learning lwjgl today, how much would it take me to reach that very low level i have now with Java2D ?
With LWJGL, it might take a long time.

Here is an example in LibGDX that does all of this. If you study it you might be able to grasp it all within a day or two. It's actually a complete little game:
http://www.java-gaming.org/?action=pastebin&id=557


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.

xFryIx (57 views)
2014-11-13 12:34:49

digdugdiggy (35 views)
2014-11-12 21:11:50

digdugdiggy (29 views)
2014-11-12 21:10:15

digdugdiggy (23 views)
2014-11-12 21:09:33

kovacsa (46 views)
2014-11-07 19:57:14

TehJavaDev (50 views)
2014-11-03 22:04:50

BurntPizza (50 views)
2014-11-03 18:54:52

moogie (65 views)
2014-11-03 06:22:04

CopyableCougar4 (63 views)
2014-11-01 23:36:41

DarkCart (148 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!