Results 1 to 7 of 7
Thread: multiple rect collisions
- 01-08-2013, 01:35 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
multiple rect collisions
hello all, and thanks in advance for the replies
i have an arraylist of enemies, and i want to check collision between each enemy, and move any enemies out of the way when they collide..
this is the collision code i have so far..
enemies are moved byJava Code:for(int i=0;i<eList.size();i++){ //compare this enemy with for(int j=i+1;j<eList.size();j++){ //this one Rectangle Rect1 = new Rectangle(eList.get(i).x, eList.get(i).y, eList.get(i).w, eList.get(i).h); Rectangle Rect2 = new Rectangle(eList.get(j).x, eList.get(j).y, eList.get(j).w, eList.get(j).h); if(Rect1.intersects(Rect2){ //check collision between two enemies if(eList.get(i).x < eList.get(j).x){ //if its to the left eList.get(i).x -= 1; //move more left }else{ eList.get(i).x += 1; //else its to the right, move right } } } }
and in the update method each enemy's.x += vx; and .y+=vy;Java Code:int speed = 2; float deltaX = px-this.x; float deltaY = py-this.y; float distance = (float) Math.sqrt(((deltaX*deltaX) + (deltaY*deltaY))); this.vx = speed / distance * deltaX; this.vy = speed / distance * deltaY;
px is the players x, so the enemies are always following the player
currently i have two problems:
(1): when the player "herds" them all into a small space(circling around them), collision doesnt seem to trigger between certain enemies, and
(2): once all enemies reach the same point (or players position), they "teleport" to 0,0 on screen instead of moving out of the way
if i didnt post enough information, or posted in the wrong place, please let me know.. i posted what i thought was necessary for the question, i thought other classes and unused variables for soon-to-be methods would just confuseLast edited by DarrylBurke; 01-09-2013 at 08:18 AM.
- 01-08-2013, 04:54 AM #2
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: multiple rect collisions
Well, yeah, I think that you should have a short self contained correct example that compiles at least.if i didnt post enough information
Short, Self Contained, Correct Example
I did notice that there might be a problem when an enemy is colliding with itself. That might be a problem.
I'd like to try out the game. If the code is too much to post in this forum, you can email it to me and I'd be interested in looking it over. My email address is: kaydell@yahoo.com
- 01-08-2013, 06:11 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Re: multiple rect collisions
sorry about that..
my main class-
and my enemy class..Java Code:import java.applet.Applet; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.Timer; public class Main extends Applet implements ActionListener{ private static final long serialVersionUID = 1L; ArrayList<enemy> eList = new ArrayList<enemy>(); int spawnCount = 0; Timer t; public void init(){ setSize(900,800); for(int i=0;i<=11;i++){ eList.add(new enemy(spawnCount, i)); spawnCount++; if(spawnCount >= 4){ spawnCount = 0; } } t = new Timer(50, this); t.setInitialDelay(0); t.start(); setBackground(Color.black); } public void paint(Graphics g){ g.setColor(Color.blue); for(int i=0;i<eList.size();i++){ g.fillRect(eList.get(i).x, eList.get(i).y, eList.get(i).h, eList.get(i).w); g.drawString(Integer.toString(i), eList.get(i).x, eList.get(i).y); } } public void actionPerformed(ActionEvent e) { repaint(); for(int i=0;i<eList.size();i++){ eList.get(i).setV(450, 400); eList.get(i).x += eList.get(i).vx; //move enemies eList.get(i).y += eList.get(i).vy; for(int j=i+1;j<eList.size();j++){ //enemy collision Rectangle Rect1 = new Rectangle(eList.get(i).x, eList.get(i).y, eList.get(i).w, eList.get(i).h); Rectangle Rect2 = new Rectangle(eList.get(j).x, eList.get(j).y, eList.get(j).w, eList.get(j).h); if(Rect1.intersects(Rect2) || Rect2.intersects(Rect1)){ if(eList.get(i).x < eList.get(j).x){ eList.get(i).x -= 1; }else{ eList.get(i).x += 1; } } } } } public void update(Graphics g) { //for double buffering Graphics offgc; Image offscreen = null; Dimension d = size(); offscreen = createImage(d.width, d.height); offgc = offscreen.getGraphics(); offgc.setColor(getBackground()); offgc.fillRect(0, 0, d.width, d.height); offgc.setColor(getForeground()); paint(offgc); g.drawImage(offscreen, 0, 0, this); } }
enemies will spawn consecutively in the middle of the four lines of the screen and begin towards player, or in this instance 450,400Java Code:public class enemy { int x; int y; int h = 10; int w = 10; float vx; float vy; int spawnPoint1x = 450; int spawnPoint1y = 0; int spawnPoint2x = 0; int spawnPoint2y = 400; int spawnPoint3x = 900; int spawnPoint3y = 400; int spawnPoint4x = 450; int spawnPoint4y = 800; public enemy(int n, int i) { switch(n){ case 0: this.x = spawnPoint1x; this.y = spawnPoint1y; break; case 1: this.x = spawnPoint2x; this.y = spawnPoint2y; break; case 2: this.x = spawnPoint3x; this.y = spawnPoint3y; break; case 3: this.x = spawnPoint4x; this.y = spawnPoint4y; break; } } public void setV(int px, int py) { int speed = 2; float deltaX = px-this.x; float deltaY = py-this.y; float distance = (float) Math.sqrt(((deltaX*deltaX) + (deltaY*deltaY))); this.vx = speed / distance * deltaX; this.vy = speed / distance * deltaY; } }
i sentthe coordinates 450,400 instead of locating player position & implementing player classJava Code:eList.get(i).setV(450, 400);
also, at the beginning of spawn enemies 4 & 11 seem not to be colliding..
- 01-08-2013, 07:12 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: multiple rect collisions
If the distance between the player and enemy is 0, then how can you divide by 0 in that instance:
you can't divide by 0.Java Code:this.vx = speed / distance * deltaX; // AKA this.vx = speed / 0 * deltaX;
- 01-09-2013, 12:49 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: multiple rect collisions
Oh yeah. With floats, dividing by zero produces Infinity.you can't divide by 0.
- 01-09-2013, 06:12 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 13
- Rep Power
- 0
Re: multiple rect collisions
of course!!
its always something simple...thats why i didnt want to post the whole program..
thank you so much for the help
edit: i searched for how to mark a post [solved], i cannot edit thread since it has been over 24 hrs..
any other suggestions on how to to so would also be appreciated,
or if a moderator can mark this as solved it would be cool :)Last edited by totj; 01-09-2013 at 06:17 AM.
- 01-09-2013, 08:19 AM #7
Similar Threads
-
Need help setting up collisions for a Java game
By sur4j in forum New To JavaReplies: 5Last Post: 12-08-2012, 02:12 PM -
MouseEntered to rect
By g6pd in forum New To JavaReplies: 3Last Post: 03-10-2011, 01:52 PM -
Solving Collisions in a car game
By Rectal Exambot in forum Advanced JavaReplies: 1Last Post: 09-29-2010, 02:13 PM -
Inelastic circle collisions
By Atriamax in forum New To JavaReplies: 9Last Post: 08-28-2010, 05:55 PM -
Check The Collisions
By Luff in forum AWT / SwingReplies: 7Last Post: 06-23-2010, 12:30 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks