java - Sticky Collisions -


i'm making quick tilemap game, tilemap collisions. problem when run wall sticks. can out of it, i'm aiming make when hit wall, still fall, instead of stay on wall. i've tried detect collisions separately, doesn't work. here's collision section of code:

//31 amount of blocked tiles. for(int counter = 0; counter < 31; counter++) {          if(spritex + 40 + velx >= collisionx[counter] && collisionx[counter] + 100 >= spritex + velx &&spritey + 40 + vely >= collisiony[counter] && collisiony[counter] + 100 >= spritey + vely) {              velx = 0;             vely = 0;              collisions = counter;          } else {              if(counter == collisions && jumping == false) {                  fall();              }          } 

i know array bulky, i'm fixing that. here's whole class:

package main;  import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.image.bufferedimage;  import javax.swing.jpanel;  public class panel extends jpanel implements runnable, keylistener {  // dimensions  public static final int width = 800; public static final int height = 800; public static final int scale = 1;  // main loop  private thread thread; private boolean running = false; private int fps = 60; private int targettime = 1000 / fps;  // drawing  private graphics2d g; private bufferedimage image;  int x; int y;  boolean makecollision = false;  // sprite  int spritex = 210; int spritey = 200; int velx = 0; int vely = 10;  public boolean notonground = true;  int counter; int collisionscounter; int jumps = 0;  public int row; public int column;  public boolean collision; public boolean jumping = false;  public string side = null;  // tilemap  int[][] map = {          {1, 1, 1, 1, 1, 1, 1, 1},         {1, 0, 0, 1, 0, 0, 1, 1},         {1, 0, 0, 0, 0, 1, 1, 1},         {1, 0, 0, 0, 1, 1, 0, 1},         {1, 0, 0, 1, 1, 0, 0, 1},         {1, 0, 0, 1, 0, 0, 0, 1},         {1, 0, 0, 0, 0, 0, 0, 1},         {1, 1, 1, 1, 1, 1, 1, 1}  };  int[] collisionx = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; int[] collisiony = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; int[] jump = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; int collisions;  public panel() {      setpreferredsize(new dimension(width * scale, height * scale));     setfocusable(true);     requestfocus();  }  public void addnotify() {     super.addnotify();      if(thread == null) {          running = true;         addkeylistener(this);         thread = new thread(this);         thread.start();      }  }  public void init() {      image = new bufferedimage(width, height, bufferedimage.type_int_rgb);     g = (graphics2d) image.getgraphics();  }  public void update() {      if(spritey < jump[0]) {          system.out.println(jump[0]);          jumping = false;          fall();      }  }  public void draw() {      g.clearrect(0, 0, width, height);      x = 0;     y = 0;      for(column = 0; column <= 7; column++) {          x = 0;          for(row = 0; row <= 7; row++) {              changecolor(row, column, g);              g.fillrect(x, y, 100, 100);              x = x + 100;          }          y = y + 100;      }      g.setcolor(color.yellow);     g.fillrect(spritex, spritey, 40, 40);      spritex += velx;     spritey += vely;      for(int counter = 0; counter < 31; counter++) {          if(spritex + 40 + velx >= collisionx[counter] && collisionx[counter] + 100 >= spritex + velx &&spritey + 40 + vely >= collisiony[counter] && collisiony[counter] + 100 >= spritey + vely) {              velx = 0;             vely = 0;              collisions = counter;          } else {              if(counter == collisions && jumping == false) {                  fall();              }          }      }  }  public void changecolor(int rowgive, int columngive, graphics g) {      if(map[rowgive][columngive] == 1) {          g.setcolor(color.black);          if(counter < 30) {              collisionx[counter] = x;             collisiony[counter] = y;          }          counter++;      } else {          g.setcolor(color.white);      }  }  public void fall() {      vely = 5;  }  public void drawtoscreen() {      graphics g2 = getgraphics();     g2.drawimage(image, 0, 0, width * scale, height * scale, null);     g2.dispose();  }  public void run() {      init();      long wait;     long elapsed;     long start;      while(running) {          start = system.nanotime();         update();         draw();         drawtoscreen();          elapsed = system.nanotime() - start;          wait = targettime - elapsed / 1000000;          if(wait < 0) wait = 5;          try {              thread.sleep(wait);          } catch(exception e) {              e.printstacktrace();          }      }  }  public void keypressed(keyevent e) {      int code = e.getkeycode();      if(code == keyevent.vk_right) {          velx = 5;      }     if(code == keyevent.vk_left) {         velx = -5;      }     if(code == keyevent.vk_space && jumping == false) {          jumping = true;          vely = -5;         jump[0] = spritey - 100;      }  } public void keyreleased(keyevent e) {    } public void keytyped(keyevent e) {   } 

}

for sprite stop sticking , continue fall, program needs distinguish between different types of collision: collision 'bottom-tile', ground, versus tile above or next sprite doesn't stop gravity. collision section treats types of collisions same stopping of sprites movement.

a simple solution make of 'bottom-tiles' different number in matrix. example:

{1, 2, 2, 1, 1, 1, 1, 1}, {1, 0, 0, 2, 0, 0, 1, 1}, {1, 0, 0, 0, 0, 1, 2, 1}, {1, 0, 0, 0, 1, 2, 0, 1}, {1, 0, 0, 1, 2, 0, 0, 1}, {1, 0, 0, 2, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1} 

the complex solution make program differentiate 'bottom-tiles' checking see if tile above white-tile (non-collision tile.) if this, tile can hit above , can stop sprite falling.

in both cases, alter collision section stop sprite falling if collides 'bottom-tile'. if collides tile on sides or above, can make sprite bounce in opposite direction.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -