Java Slick2d rectangle collision detection strange behavior -
i want make slick2d java game , encountered problem not solve in last couple of hours. i've looked many other posts non of fits problem.
the collission detection works fine in 9/10 cases. everytime intersects rectangle moves according code below , shown in picture1. if near possble edge edges seem overlap shown here picture2. if go detects intersection , moves down. if go down fine. if go left or right nothing happens. happens if corners of rectangles intersects. if go , left or right @ same time goes in wrong direction sometimes. seems if go , sideways @ same time detects collision , moves in opposite direction until reaches point intersects corners , stops again. point after detecting , collision never moves more he has moves foreward before. further shouldn't detect intersection of rectangles , move again why doesn't detect intersection @ edge shown in picture2? hope has ideas why happening. problem occures on every side. here code.
public class player extends creature { int posx = 0; int posy = 0; rectangle hitbox; input input; public player(handler handler) { super(handler); // todo auto-generated constructor stub } @override public void init() { hitbox = new rectangle(posx + 320, posy + 240, 32, 32); } @override public void render(graphics g) { g.setcolor(color.red); g.draw(hitbox); g.setcolor(color.cyan); (int = 0; < handler.getworldcollision().size(); i++) { g.draw(handler.getworldcollision().get(i)); } } @override public void update(gamecontainer gc, int delta) { input = gc.getinput(); if (input.iskeydown(input.key_w)) { // move posy -= 100 * delta / 1000f; // if collision move (int = 0; < handler.getworldcollision().size(); i++) { if (hitbox.intersects(handler.getworldcollision().get(i))) { posy += 100 * delta / 1000f; break; } } hitbox.sety(posy + 240); } if (input.iskeydown(input.key_s)) { // move posy += 100 * delta / 1000f; // if collision move (int = 0; < handler.getworldcollision().size(); i++) { if (hitbox.intersects(handler.getworldcollision().get(i))) { posy -= 100 * delta / 1000f; break; } } hitbox.sety(posy + 240); } if (input.iskeydown(input.key_a)) { // move posx -= 100 * delta / 1000f; // if collision move (int = 0; < handler.getworldcollision().size(); i++) { if (hitbox.intersects(handler.getworldcollision().get(i))) { posx += 100 * delta / 1000f; break; } } hitbox.setx(posx + 320); } if (input.iskeydown(input.key_d)) { // move posx += 100 * delta / 1000f; // if collision move (int = 0; < handler.getworldcollision().size(); i++) { if (hitbox.intersects(handler.getworldcollision().get(i))) { posx -= 100 * delta / 1000f; break; } } hitbox.setx(posx + 320); } handler.setplayerx(posx); handler.setplayery(posy); }
}
handler.getworldcollision() array containing rectangles drawn in cyan in pictures.
public class testworld extends world { int[][] map = new int[87][35]; arraylist<rectangle> worldcollision; maploader maploader; public testworld(handler handler) { super(handler); } @override public void init() throws ioexception { maploader = new maploader(handler); map = maploader.readmapfile("gameresources/map/testmap.map"); worldcollision = new arraylist<rectangle>(); } @override public void render(graphics g) throws slickexception { // render tiles given map of world. (int = 0; < 87; i++) { (int j = 0; j < 35; j++) { g.drawimage(handler.gettilelist()[map[j][i]].getimage(), * 16 - handler.getplayerx(), j * 16 - handler.getplayery()); } } } @override public void update(gamecontainer gc, int delta) { worldcollision.clear(); (int = 0; < 87; i++) { (int j = 0; j < 35; j++) { if(handler.gettilelist()[map[j][i]].hascollision()){ worldcollision.add(new rectangle(i * 16 - handler.getplayerx(), j * 16 - handler.getplayery(), 16, 16)); } } } handler.setworldcollision(worldcollision); } }
thank advices!
i'm not sure totally got why overlapping occurs. " if near possble edge edges seem overlap", overlapping occurs when going up, not corrected when moving towards left or right?
by way, if it's not detected when moving left or right, it's because checking y colisions when going or down; although should sufficient, reason why isn't corrected/detected when going left , right , overlaping edge!
Comments
Post a Comment