I’ve been testing and trying to debug my tile collision, and I can’t figure out why my sprite goes through the top of the tiles. Any ways to fix this.
Here are the source codes dealing with this problem
Smiley
import collisiontest.Sprite;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Smiley extends Sprite {
public int jCount = 0;
public float g = .002f;
public Smiley() {
i = new ImageIcon(this.getClass().getResource("/collisiontest/smiley/sprites/Face1.png")).getImage();
x = 250;
y = 250;
}
public void update() {
moveX();
moveY();
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT) {
setDx(6);
} else if (keyCode == KeyEvent.VK_LEFT) {
setDx(-6);
}
if (keyCode == KeyEvent.VK_UP) {
if (jCount == 0) {
setDy(-3);
jCount = 1;
} else if (y < 150 && jCount == 1) {
setDy(3);
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT) {
setDx(0);
} else if (keyCode == KeyEvent.VK_LEFT) {
setDx(0);
}
if (keyCode == KeyEvent.VK_UP) {
setDy(3);
}
}
public void keyTyped(KeyEvent e) {
}
public Rectangle colRec() {
return new Rectangle(Math.round(x + 4), Math.round(y + 3), getWidth() - 10, getHeight() - 5);
}
public boolean collisionTRight(Rectangle r, Smiley smiley) {
if (smiley.x >= r.x + r.width) {
if (r.intersects(smiley.colRec())) {
smiley.x = r.x + r.width;
return true;
} else if (!r.intersects(smiley.colRec())) {
return false;
}
}
return false;
}
public boolean collisionTLeft(Rectangle r, Smiley smiley) {
if (smiley.x <= r.x) {
if (r.intersects(smiley.colRec())) {
smiley.x = r.x - smiley.colRec().width;
return true;
} else if (!r.intersects(smiley.colRec())) {
return false;
}
}
return false;
}
public boolean collisionTTop(Rectangle r, Smiley smiley) {
if (smiley.y >= r.y) {
if (r.intersects(smiley.colRec())) {
smiley.y = r.y - smiley.colRec().height;
return true;
} else if (!r.intersects(smiley.colRec())) {
return false;
}
}
return false;
}
public boolean collisionTBottom(Rectangle r, Smiley smiley) {
if (smiley.y <= r.y + r.getHeight()) {
if (r.intersects(smiley.colRec())) {
smiley.y = r.y + r.height;
return true;
} else if (!r.intersects(smiley.colRec())) {
return false;
}
}
return false;
}
public float moveX() {
return x = x + getDx();
}
public float moveY() {
return y = y + getDy();
}
public Image getFrame() {
return getImage();
}
}
Level
package collisiontest.levels;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ImageIcon;
import collisiontest.levels.Element;
import collisiontest.smiley.Smiley;
public class Level {
public Element[][] overall;
public Element[][] overallLevelColision;
public Element[] elements;
public Rectangle[][] collisionRecs;
public Rectangle[] recColElements;
public Rectangle[][] collisionRecsLeft;
public Rectangle[] recColElementsLeft;
public Rectangle[][] collisionRecsRight;
public Rectangle[] recColElementsRight;
public Rectangle[][] collisionRecsTop;
public Rectangle[] recColElementsTop;
public Rectangle[][] collisionRecsBottom;
public Rectangle[] recColElementsBottom;
///////////Ints///////////
public int Lines;
public int levelLength;
public int leftBorder;
public int rightBorder;
public int unit = 16;
/////////Images/////////
public Image background;
public Image a5;
///////Templetes////////
Templetes t13;
////////Strings////////
public String levelName;
///////Booleans////////
public boolean screenStart = false;
public boolean menu = false;
public boolean gameOver = false;
public boolean canPlayBack;
///////Add to level/////////
public Level currentLevel;
public int playingLevel;
public Image getBackground() {
return background;
}
public Level(String n) {
background = new ImageIcon(this.getClass().getResource("/collisiontest/levels/" + n + "/background.png")).getImage();
a5 = new ImageIcon(this.getClass().getResource("/collisiontest/levels/" + n + "/A5.png")).getImage();
}
public Level() {
}
public void defineLevel(String[] definitions) {
overall = new Element[Lines][definitions[0].length()];
collisionRecs = new Rectangle[Lines][definitions[0].length()];
collisionRecsLeft = new Rectangle[Lines][definitions[0].length()];
collisionRecsRight = new Rectangle[Lines][definitions[0].length()];
collisionRecsTop = new Rectangle[Lines][definitions[0].length()];
collisionRecsBottom = new Rectangle[Lines][definitions[0].length()];
levelLength = definitions[0].length() * unit;
rightBorder = 500;
int elementsCounter = 0;
// Blocks side names facing my right/left
for (int i = 0; i < definitions.length; i++) {
char[] lineDefinition = definitions[i].toCharArray();
for (int j = 0; j < lineDefinition.length; j++) {
if (lineDefinition[j] == ':') {
overall[i][j] = null;
collisionRecs[i][j] = null;
collisionRecsLeft[i][j] = null;
collisionRecsRight[i][j] = null;
collisionRecsTop[i][j] = null;
collisionRecsBottom[i][j] = null;
} else if (lineDefinition[j] == 'E') {
t13 = new Templetes(j * unit, i * unit, unit, unit, 1, a5);
overall[i][j] = t13;
collisionRecs[i][j] = t13.getCollisionRec();
Rectangle rLeft = new Rectangle(j * unit, i * unit, 4, unit);
Rectangle rRight = new Rectangle(j * unit + unit - 5, i * unit, 4, unit);
Rectangle rTop = new Rectangle(j * unit, i * unit, unit, 4);
Rectangle rBottom = new Rectangle(j * unit, (i * unit) + unit - 5, unit, 4);
collisionRecsLeft[i][j] = rLeft;
collisionRecsRight[i][j] = rRight;
collisionRecsTop[i][j] = rTop;
collisionRecsBottom[i][j] = rBottom;
elementsCounter++;
}
}
}
elements = new Element[elementsCounter];
recColElements = new Rectangle[elementsCounter];
recColElementsLeft = new Rectangle[elementsCounter];
recColElementsRight = new Rectangle[elementsCounter];
recColElementsTop = new Rectangle[elementsCounter];
recColElementsBottom = new Rectangle[elementsCounter];
int counter = 0;
for (int i = 0; i < overall.length; i++) {
for (int j = 0; j < overall[i].length; j++) {
if (overall[i][j] != null) {
elements[counter] = overall[i][j];
recColElements[counter] = collisionRecs[i][j];
recColElementsLeft[counter] = collisionRecsLeft[i][j];
recColElementsRight[counter] = collisionRecsRight[i][j];
recColElementsTop[counter] = collisionRecsTop[i][j];
recColElementsBottom[counter] = collisionRecsBottom[i][j];
counter++;
}
}
}
}
public void drawLevel(Graphics2D g) {
try {
for (int i = 0; i < elements.length; i++) {
elements[i].draw(g);
g.draw(recColElementsLeft[i]);
g.draw(recColElementsRight[i]);
g.draw(recColElementsTop[i]);
g.draw(recColElementsBottom[i]);
}
} catch (Exception e) {
System.out.toString();
}
}
public void drawBackground(Graphics2D g) {
g.drawImage(getBackground(), 0, 0, null);
}
////Override methods//////
public void drawLevelObjects(Graphics2D g) {
}
public void update(Smiley s, Level l) {
}
/////////////Other elements/////////////
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
Sprite
import java.awt.Image;
public class Sprite {
public float x, y, dx, dy;
public Image i;
public int getWidth() {
return i.getWidth(null);
}
public int getHeight() {
return i.getHeight(null);
}
public float getX() {
return x - getWidth() / 2;
}
public float getY() {
return y - getHeight() / 2;
}
public float getDy() {
return dy;
}
public float getDx() {
return dx;
}
public void setDy(float my) {
dy = my;
}
public void setDx(float mx) {
dx = mx;
}
public Image getImage() {
return i;
}
}
TestLevel
import collisiontest.levels.Level;
import collisiontest.smiley.Smiley;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
public class TestLevel extends Level {
public static final String row1 = ":::::::::::::::::::::::::::::::::::";
public static final String row2 = ":::::::::::::::::::::::::::::::::::";
public static final String row3 = ":::::::::::::::::::::::::::::::::::";
public static final String row4 = ":::::::::::::::::::::::::::::::::::";
public static final String row5 = ":::::::::::::::::::::::::::::::::::";
public static final String row6 = ":::::::::::::::::::::::::::::::::::";
public static final String row7 = ":::::::::::::::::::::::::::::::::::";
public static final String row8 = ":::::::::::::::::::::::::::::::::::";
public static final String row9 = ":::::::::::::::::::::::::::::::::::";
public static final String row10 = ":::::::::::::::::::::::::::::::::::";
public static final String row11 = ":::::::::::::::::::::::::::::::::::";
public static final String row12 = ":::::::::::::::::::::::::::::::::::";
public static final String row13 = ":::::::::::::::::::::::::::::::::::";
public static final String row14 = ":::::::::::::::::::::::::::::::::::";
public static final String row15 = "::::::::::::::::::::::::::::EEEEEEE";
public static final String row16 = "::::::::::::::::::::::::::::EEEEEEE";
public static final String row17 = "::::::::::::::::::::::::::::EEEEEEE";
public static final String row18 = "::::::::::::::::::::::::::::EEEEEEE";
public static final String row19 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row20 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row21 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row22 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row23 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row24 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public static final String row25 = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
public Smiley mario = new Smiley();
///35 elements in each line
public TestLevel() {
super("test");
Lines = 35;
String[] definitions = {row1, row2, row3, row4, row5,
row6, row7, row8, row9, row10, row11, row12, row13,
row14, row15, row16, row17, row18, row19, row20,
row21, row22, row23, row24, row25};
super.defineLevel(definitions);
}
@Override
public void drawLevelObjects(Graphics2D g) {
drawBackground(g);
drawLevel(g);
g.drawImage(mario.getFrame(), Math.round(mario.moveX()), Math.round(mario.moveY()), null);
g.draw(mario.colRec());
}
@Override
public void update(Smiley m, Level l) {
for (int i = 0; i < l.Lines; i++) {
if (m.collisionTLeft(l.recColElementsLeft[i], m)) {
m.collisionTLeft(l.recColElementsLeft[i], m);
} else if (m.collisionTRight(l.recColElementsRight[i], m)) {
m.collisionTRight(l.recColElementsRight[i], m);
} else if (m.collisionTTop(l.recColElementsTop[i], m)) {
m.collisionTTop(l.recColElementsTop[i], m);
} else if (m.collisionTBottom(l.recColElementsBottom[i], m)) {
m.collisionTBottom(l.recColElementsBottom[i], m);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
mario.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
mario.keyReleased(e);
}
}
MainConfig
import collisiontest.levels.test.TestLevel;
import collisiontest.smiley.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class MainConfig extends JPanel implements Runnable {
public Thread mainLoop;
public TestLevel tl = new TestLevel();
public MainConfig() {
setFocusable(true);
addKeyListener(new KL());
}
@Override
public void addNotify() {
super.addNotify();
mainLoop = new Thread(this);
mainLoop.start();
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
tl.drawLevelObjects(g2d);
g.dispose();
}
public class KL extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
tl.keyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
tl.keyReleased(e);
}
}
public void update(){
tl.update(tl.smiley, tl);
}
@Override
public void run() {
long lastTime = System.currentTimeMillis();
while (true) {
long nowTime = System.currentTimeMillis();
long timePassed = nowTime - lastTime;
lastTime = nowTime;
update();
repaint();
try {
Thread.sleep(20);
} catch (Exception e) {
}
}
}
}