Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Collision Detection with some Rectangles  (Read 1829 times) 0 Members and 1 Guest are viewing this topic.

Junior Newbie

 « Posted 2012-09-18 13:08:42 »

Hey everyone,
I started learning Java some weeks ago and tried to make a little game. It should be like this game.
I made some walls using rectangles and also a little ball. The collision works really great, but only with one rectangle, wich I wrote in the code like this:
 1  2  3  4  5  6  7  8  9  10 `public void checkCollision(){            player = new Rectangle(player.x,player.y,player.width,player.height);            rect = floor_mid;      if (player.intersects(rect)){         stopMovement();         setOffWall(rect);      }   }`

And the rectangles i'm using:
 1  2  3  4  5  6  7  8 `           public Rectangle floor_south;   public Rectangle floor_north;   public Rectangle floor_east;   public Rectangle floor_west;   public Rectangle floor_mid;      public Rectangle player;`

How can I code it so the player would stop on every rectangle?
Embedded

Junior Devvie

Medals: 1

 « Reply #1 - Posted 2012-09-18 13:22:49 »

You could put all the Rectangles into an array of rectangles then you can go through them all in a for loop e.g.
 1  2  3  4  5  6 `for(int i = 0; i < rectangles.length(); i++){       if(player.intersects(rectangles[i])){         stopMovement();         setOffWall(rect);       }}`

Junior Newbie

 « Reply #2 - Posted 2012-09-18 13:28:44 »

You could put all the Rectangles into an array of rectangles then you can go through them all in a for loop e.g.
 1  2  3  4  5  6 `for(int i = 0; i < rectangles.length(); i++){       if(player.intersects(rectangles[i])){         stopMovement();         setOffWall(rect);       }}`

How do I do an array of recangles?
aazimon
 « Reply #3 - Posted 2012-09-18 17:33:02 »

Like this: Rectangle[] rectangles = new Rectangles[1];
I would recommend creating an ArrayList instead. Like this: ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>(), that way you don't have to know the number of Rectangles at the beginning. You just keep adding Rectangles you need for collision detection to the list. Then you can iterator through the list.
 1  2  3  4  5 `Iterator iter = rectangles.iterator();while (iter.hasNext()) {Rectangle rectangle = iter.next();// Do your stuff here}`

Regards,
sproingie

JGO Kernel

Medals: 202

 « Reply #4 - Posted 2012-09-18 17:37:35 »

Unless you actually need the iterator object (such as to remove things), you ought to use foreach syntax:
 1  2  3 `for (Rectangle r: rectangles) {   // do stuff here}`
Pages: [1]
 ignore  |  Print

 EgonOlsen (77 views) 2018-06-10 19:43:48 EgonOlsen (57 views) 2018-06-10 19:43:44 EgonOlsen (77 views) 2018-06-10 19:43:20 DesertCoockie (259 views) 2018-05-13 18:23:11 nelsongames (157 views) 2018-04-24 18:15:36 nelsongames (156 views) 2018-04-24 18:14:32 ivj94 (897 views) 2018-03-24 14:47:39 ivj94 (161 views) 2018-03-24 14:46:31 ivj94 (810 views) 2018-03-24 14:43:53 Solater (174 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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