Collision with Tiles not Working Properly

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) {
            }
        }

    }
}


  • Looking at row1 to row24 makes my eyes bleed and fall off…pleeeeeaaaase use arrays? Pretty please? With a cherry on top? :slight_smile:

  • Why do the collisionTXXX methods take a Smiley argument when it’s already in the Smiley class? Now I can’t tell if either Top is the one that’s not working or Bottom but they both look wrong. Also in the collision methods, why are you re-calling intersects??? If intersects(Rectangle) returns “true”, handle that, else…there are no options other than “false” left… :stuck_out_tongue:

  • Why so many arrays in Level?!? You either like em (Level) or you absolutely hate em (TestLevel) :cranky:
    I’m not gonna look over every single one of those arrays and track down their use but I bet you my entire soul you’re doing something wrong [tm].

  • In MainConfig.update()…you call collisionTXXX twice if it’s true?! facepalm

I’m not trying to be harsh here with these constructive criticism, but please look over your code carefully and learn how to use a debugger when things go south.

:smiley:

When you are at a point where you use Debugger have a close look at these lines:

public Rectangle colRec() {
        return new Rectangle(Math.round(x + 4), Math.round(y + 3), getWidth() - 10, getHeight() - 5);
}

Here you create a Rectangle that is smaller than your Sprite (why don’t you use the existing Sprite class?) and so the Image will go through your Tiles.

Thanks for the replies, I’m really sorry that I haven’t posted in weeks. I’ve been doing lots of research with this collision, and found out point collision was better for what I was doing. Since the rectangular collision wasn’t good and the algorithm wasn’t good either.

But even though that, I’m just testing this new collision from a Java book I read, and for some reason, the tiles can’t be drawn onto the screen. Any Ideas or thoughts to fix this?

Here are the important source codes.

Smiley


public class Smiley extends Sprite {

    public int jCount = 0;
    public float g = .002f;

    public Smiley() {
        i = new ImageIcon(this.getClass().getResource("/extrememario/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;
                if (y < 150 && jCount == 1) {
                    setDy(3);
                }
            } else if (y < 150 && jCount == 1) {
                setDy(3);
            } else if (jCount == 1 && y >= 250) {
                jCount = 0;
            }

        }
    }

    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), Math.round(y), getWidth(), getHeight() + 3);
    }

    public boolean collisionTRight(Rectangle r) {
        if (x >= r.x + r.width) {
            if (r.intersects(colRec())) {
                x = r.x + r.width;
                System.out.println("right");
                return true;
            } else if (!r.intersects(colRec())) {
                return false;
            }
        }

        return false;
    }

    public boolean collisionTLeft(Rectangle r) {
        if (x <= r.x) {
            if (r.intersects(colRec())) {
                x = r.x - colRec().width;
                System.out.println("left");
                return true;
            } else if (!r.intersects(colRec())) {
                return false;
            }
        }

        return false;
    }

    public boolean collisionTTop(Rectangle r) {
        //if (y >= r.y) {
        if (r.intersects(colRec())) {
            y = r.y - colRec().height;
            System.out.println("top");
            return true;
        } else if (!r.intersects(colRec())) {
            return false;
        }
        //}
        return false;
    }

    public boolean collisionTBottom(Rectangle r) {
        //if (y <= r.y + r.getHeight()) {
        if (r.intersects(colRec())) {
            y = r.y + r.height;
            System.out.println("bottom");
            return true;
        } else if (!r.intersects(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


public class Level extends TileMap{

    public ArrayList<Image> tilesArray = new ArrayList();
    public Point pointCache = new Point();

    public Level(){
        super(16,16);
    }
    public synchronized TileMap loadMap(URL url) throws IOException {        
        ArrayList lines = new ArrayList();
        int width = 16;
        int height = 16;
        File f;
        try{
           f = new File(url.toURI());
        }catch(URISyntaxException e){
           f = new File(url.getPath());
        }
        //BufferedReader reads text files
        BufferedReader reader = new BufferedReader(new FileReader(f));
        while (true) {
            String line = reader.readLine();
            //no more lines to read
            if (line == null) {
                reader.close();
                break;
            }
            //Add lines, except with comments(#)
            if (!line.startsWith("#")) {
                lines.add(line);
                width = Math.max(width, line.length());
            }
        }

        height = lines.size();
        TileMap newMap = new TileMap(width, height);
        for (int y = 0; y < height; y++) {
            String line = (String) lines.get(y);
            for (int x = 0; x < line.length(); x++) {
                char ch = line.charAt(x);

                //check if the char represents tile           
                if (ch =='1') {
                    newMap.setTile(x, y, new ImageIcon(this.getClass().getResource("/extrememario/levels/normal/1.png")).getImage());
                    System.out.println(newMap.getHeight());
                }
            }
        }

        return newMap;
    }
    public void drawLevel(Graphics g){       
        for (int y = 0; y < this.getHeight(); y ++){
            for(int x = 0; x < this.getWidth(); x ++){
                Image image = this.getTile(x, y);
                if(image != null){
                    g.drawImage(image, TileMapRender.tilesToPixels(x), TileMapRender.tilesToPixels(y), null);
                }
            }
        }
    }

    public Point getTileCollision(Sprite sprite, float newX, float newY) {
        float fromX = Math.min(sprite.getX(), newX);
        float fromY = Math.min(sprite.getY(), newY);
        float toX = Math.min(sprite.getX(), newX);
        float toY = Math.min(sprite.getY(), newY);

        int fromTileX = TileMapRender.pixelsToTiles(fromX);
        int fromTileY = TileMapRender.pixelsToTiles(fromY);
        int toTileX = TileMapRender.pixelsToTiles(toX + sprite.getWidth() - 1);
        int toTileY = TileMapRender.pixelsToTiles(toY + sprite.getHeight() - 1);

        for (int x = fromTileX; x <= toTileX; x++) {
            for (int y = fromTileY; y <= toTileY; y++) {
                if (x < 0 || x > this.getWidth()
                        || this.getTile(x, y) != null) {
                    pointCache.setLocation(x, y);
                    return pointCache;

                }
            }
        }
        return null;
    }
}


TileMap


import extrememario.Sprite;
import java.awt.Image;
import java.util.Iterator;
import java.util.LinkedList;

    public class TileMap {

        public Image[][] tiles;
        public LinkedList sprites;

        public TileMap(int w, int h) {
            tiles = new Image[w][h];
            sprites = new LinkedList();
        }

        public int getWidth() {
            return tiles.length;
        }

        public int getHeight() {
            return tiles[0].length;
        }

        public Image getTile(int x, int y) {
            if (x < 0 || x >= getWidth()
                    || y < 0 || y >= getHeight()) {
                return null;
            } else {
                return tiles[x][y];
            }
        }

        public void setTile(int x, int y, Image tile) {
            tiles[x][y] = tile;
        }

        public void addSprite(Sprite sprite) {
            sprites.add(sprite);
        }

        public void removeSprite(Sprite sprite) {
            sprites.remove(sprite);
        }

        public Iterator getSprites() {
            return sprites.iterator();
        }
    }

TileMapRender


public class TileMapRender {
    
    public static int TileSize = 16;
    //the size in bits of the tile
    //Math.pow(2,Tile_Size_Bits) = TileSize
    public static int TileSizeBits = 4;
    
    public static int pixelsToTiles(float pixels){
        return pixelsToTiles(Math.round(pixels));
    }
    
    public static int pixelsToTiles(int pixels){
        return pixels >> TileSizeBits;
    }
    
    public static int tilesToPixels(int numTiles){
    
        return numTiles << TileSizeBits;
    }
    
}


Test


public class Test extends Level {

    public Test(){
        getMap();
    }

    public void getMap(){
        try {
            this.loadMap(this.getClass().getResource("/extrememario/levels/testlevel/Map.txt"));
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
}

MainConfig


public class MainConfig extends JPanel implements Runnable {

    public Smiley mario;
    public Thread mainLoop;
    public Test test = new Test();

    public MainConfig() {
        mario = new Smiley();
        setFocusable(true);
        addKeyListener(new KL());
    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }
    public Image bg = new ImageIcon(this.getClass().getResource("/extrememario/BackgroundDemoStage.png")).getImage();

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);
        g.drawImage(mario.getFrame(),Math.round(mario.moveX()), Math.round(mario.moveY()), null);
        test.drawLevel(g);
        g.dispose();
    }

    public class KL extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            mario.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mario.keyReleased(e);
        }
    }

    public void updateCollisionPlayer(){
        
    }
    @Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            long timePassed = nowTime - lastTime;
            lastTime = nowTime;
            mario.update();
            repaint();
            try {
                Thread.sleep(20);
            } catch (Exception e) {
            }
        }

    }
}

Sprite


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;
    }
}


You’re still calling intersects() twice in your collisionTxxxx() functions. Better to just do:


public boolean collisionTRight(Rectangle r) {
    if (x >= r.x + r.width) {
        if (r.intersects(colRec())) {
            x = r.x + r.width;
            System.out.println("right");
            return true;
        }
    }
    return false;
}

This way you get rid of the second intersects() call.

And are you sure that your image isn’t null in your drawLevel(Graphics g) function? Other than that, I can’t really spot if there’s something wrong. :slight_smile: Maybe someone better at this whole game making thing can? :slight_smile: