Java: Hit Detection improperly detecting overlap among two objects -


so have rowing game in boat object must dodge randomly placed obstacle objects using arrow keys. if boat hits 1 of obstacles, boat takes random amount of damage. boat 27x52 picture (with 2 pixels of nothing on either side code shows 2-25) , obstacles rectangles.

i have hit detection working part; however, whenever boat on right side of rectangle taller wide, it'll take damage, if boat 5-10 pixels away right edge of rectangle. refer image better understanding of issue: http://imgur.com/pqdlmrl

in code, create array of 15 obstacles. obstacles take parameters(color color, int damagedealt, int xpos, int ypos, int width, int height, boolean hashit).

//create array of obstacles for(int x = 0; x < 15; x++) {         obstacles[x] = new obstacle((new color(rand.nextint(81), rand.nextint(51), rand.nextint(51))), rand.nextint(21) + 10, rand.nextint(601),              (-x * (rand.nextint(51) + 31)), (rand.nextint(31) + 5), (rand.nextint(31) + 5), false);      } 

here hit detection code (this in loop cycling through array of obstacle objects:

for(int y = 2; y <= 25; y++) {                 //obstacles hit detection                 for(int z = 0; z <= obstacles[x].getw(); z++) {                                  if(boat.getx() + y == obstacles[x].getx() + z && boat.gety() == obstacles[x].gety()) {                             if(!obstacles[x].getdamaged()) {                                  boat.sethealth(boat.gethealth() - obstacles[x].getdmg());                                  obstacles[x].setdamaged(true);                             }                       }                              } 

right cycles through boat's x values, 2 25, instead of 0 27 because of 2 pixels of nothing on either side. cycles through obstacle's x values(xpos xpos+width) , sees if of values match. if there match, , y-values match, boat take damage. keep in mind happens when boat on right side of obstacle , obstacle taller wide. not see problem code yet cannot figure out solution error. thanks.

edit: here code boat , obstacle classes.

import java.awt.color; import java.awt.rectangle;  public class obstacle {     private int dmg, xpos, ypos, height, width;     private color color;     private boolean hasdamaged;      public obstacle(color hue, int damage, int x, int y, int w, int h, boolean damaged) {         dmg = damage;         xpos = x;         ypos = y;         width = w;         height = h;         color = hue;         hasdamaged = damaged;     }      public boolean getdamaged() {         return hasdamaged;     }     public void setdamaged(boolean damaged) {         hasdamaged = damaged;     }      public color getcolor() {         return color;     }      public int getdmg() {         return dmg;     }     public void setdmg(int damage) {         dmg = damage;     }      public int getx() {         return xpos;     }     public void setx(int x) {         xpos = x;     }      public int gety() {         return ypos;     }     public void sety(int y) {         ypos = y;     }      public int getw() {         return width;     }     public void setw(int w) {         width = w;     }      public int geth() {         return height;     }     public void seth(int h) {         height = h;     }      public rectangle getbounds() {         return new rectangle(xpos, ypos, width, height);     } } 

and boat class:

import java.awt.image; import java.awt.image.bufferedimage; import java.awt.rectangle;  public class boat {  private int hp, xpos, ypos, dx; private bufferedimage boatpic;  public boat(bufferedimage img, int health, int x, int y, int velx) {     boatpic = img;     hp = health;     xpos = x;     ypos = y;     dx = velx; }   public bufferedimage getimage() {     return boatpic; } public void setimage(bufferedimage img) {     boatpic = img; }  public int gethealth() {     return hp; } public void sethealth(int health) {     hp = health; }  public int getx() {     return xpos; } public void setx(int x) {     xpos = x; }  public int gety() {     return ypos; } public void sety(int y) {     ypos = y; }  public int getdx() {     return dx; } public void setdx(int velx) {     dx = velx; }  public rectangle getbounds() {     return new rectangle(xpos, ypos, 25, 49); }     } 

if ship alway facing same direction, can check collision easier.

by creating method ship , obstacles returns rectangle size of object.

public rectangle getbounds() {     return new rectangle(shipposx, shipposy, widht, height); } 

like this. way can test collision in 1 line:

getbounds().intersects(obstacle.getbounds()); 

this return true if intersect.

if want more precise, or want different cases collisions different directions, can create rectangle different parts of ship, it's body , wings.

public rectangle getrightwingbound(){} public rectangle getleftwindbound(){} 

and on.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo