java - Side Collisions -
i'm making tilemap game , i've got simple collisions down. make game work have know side of rectangle hit wall/brick. putting simple collision code inside of main collision:
if(spritex < brickx + brickwidth) {}
doesn't work. main collision code @ moment is:
for(int counter = 0; counter < 31; counter++) { if(spritex + 40 >= collisionx[counter] && collisionx[counter] + 100 >= spritex && spritey + 40 >= collisiony[counter] && collisiony[counter] + 100 >= spritey) { velx = 0; vely = 0; collisions = counter; } else { if(counter == collisions && jumping == false) { fall(); } } }
if want entire 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, }; 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 >= collisionx[counter] && collisionx[counter] + 100 >= spritex && spritey + 40 >= collisiony[counter] && collisiony[counter] + 100 >= spritey){ 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) { }}
let's try divide problem little bit. don't need find out when rectange hit wall: need find when left side of rectangle hit right side of wall , when right side of rectangle hit left side of wall.
to more precise: don't test objects. divide object in collision surfaces (left , right sides in case), find way modelize them (usually x , x+width left , right side of rectangle, if x x coordinate of topleft corner). , test both @ same time "or" condition.
if(spritex < brickx + brickwidth || spritex + spritewidth > brickx) {}
edit reading complete class little more closely, looks similar use "&&" condition, meaning can never true since both side collisions cannot happen @ same time. may wrong, can tell me ?
Comments
Post a Comment