Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  Arraylist removing when 2 of the same object collide!!!  (Read 1703 times)
0 Members and 1 Guest are viewing this topic.
Offline VIrtueeL
« Posted 2014-11-19 08:03:17 »

so what i do is i spawn in 1000 flowers with different Colors
and the flowers goes to the block its colliding with!

what i want to do is to check if there is a flower already colliding with that block and remove it from the list if this 2 diffrent flowers
are colliding!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
         for(Block b : PlayingState.blocks){
            if(getObjectRect().intersects(b)){
               
               if(this.xpos < b.x){
                  xpos = b.x;
               }
               if(this.xpos > b.x){
                  xpos = b.x;
               }
               if(this.ypos < b.y){
                  ypos = b.y;
               }
               if(this.ypos > b.y){
                  ypos = b.y;
               }
               
               if(xpos == b.x && ypos == b.y){
                  //LOOK IF THERE IS 2 IN THE SAME PLACE
                  //THEN REMOVE THE FIRST ONE AND MOVE SECOND ONE THERE INSTEAD
                  choosedLocation = true;
               }
            }
         }
      }



i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #1 - Posted 2014-11-19 08:42:34 »

If this is in a tile based generation, you are better off storing the objects in a 2D array like so:

1  
Tile[] [] tiles;


Fill it with say grass tiles. Then you can iterate over it and replace the grass tiles with flower tiles or you can set a flag in that tile so that you know it is occupied by some object.

1  
2  
3  
4  
5  
6  
7  
for(int x = 0; x < tiles.len; x++) {
    for(int y = 0; y < tiles[0].len; y++) {
        Tile tile = tiles[x] [y] ;
        // do something

}
    }


For generation you can iterate a for loop n times, then use a random number generator to pick an x and y value and grab that tile, check if it is occupied or is already a flower tile and go from there.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline VIrtueeL
« Reply #2 - Posted 2014-11-19 09:26:06 »

here!

all flowers has
1  
2  
   protected double xpos;
   protected double ypos;


and
1  
   private Rectangle objectRect;


the objectRect is init like this in tick

1  
objectRect = new Rectangle((int)xpos,(int)ypos,width,height);


so if 2 flowers x and y are = and that flower objectRect is intersecting with the other objectRect then remove it

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DonDon

Senior Newbie


Exp: 10-12 months



« Reply #3 - Posted 2014-11-19 18:20:01 »

I've not seen this "intersect" before, I only have "overlap" for collision detection  Huh
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #4 - Posted 2014-11-19 20:11:12 »

That is pointless. There is not much point in even adding the flower in the first place, from what I understand you are adding a flow and THEN checking to see if there is one there already, then removing the previous one?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline VIrtueeL
« Reply #5 - Posted 2014-11-20 07:59:15 »

yeah to remove flowers that are on top off etch other!

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #6 - Posted 2014-11-21 06:42:19 »

Why don't you just check during the generation of the flowers? The is no point in generating them,  then going over the list again to remove overlapping flowers.

Why not do something like this:

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  
public class FlowerTest
{
   
   Array<Flower> flowers = new Array<Flower>();
   
   public FlowerTest(){
      // we try to generate 500 flowers
      for(int x = 0; x < 500; x++){
         // create a new flower at a random positions
         Flower flower = new Flower(MathUtils.random(0,200), MathUtils.random(0, 200));
         for(int i = 0; i < flowers.size - 1; i++){
            // get a flower from the list
            Flower flower2 = flowers.get(i);
            // compare positions, if they are tge dame just jump to tge mext iteration
            if(flower2.comparePos(flower)) continue;
            // we got this far, so good to go
            flowers.add(flower);
         }
      }
   }
   
   public class Flower{

      float x;
      float y;

      public Flower(float x, float y){
         this.x = x;
         this.y = y;

      }

      public boolean comparePos(Flower otherFlower){
         return (this.x == otherFlower.x && this.y == otherFlower.y);
      }


   }
   
}


Wrote using aide so probably full or errors.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline VIrtueeL
« Reply #7 - Posted 2014-11-21 09:39:38 »

it does not generate any flowers what so ever!

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline VIrtueeL
« Reply #8 - Posted 2014-11-21 09:54:36 »

solved it another way by setting the block to choosed and if the block was already choosed the flower that wanted to go there was removed from the list =D

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1569 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!