Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Collision with Tiles not Working Properly  (Read 1081 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-11-27 05:34:26 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
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) {
            }
        }

    }
}
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-27 07:50:11 »

- Looking at row1 to row24 makes my eyes bleed and fall off.....pleeeeeaaaase use arrays? Pretty please? With a cherry on top? Smiley

- 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... 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.

Cheesy

Offline Lordpomeroy
« Reply #2 - Posted 2011-11-27 08:18:26 »

When you are at a point where you use Debugger have a close look at these lines:
 
1  
2  
3  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SubzeroX5

Senior Newbie





« Reply #3 - Posted 2011-12-19 04:42:58 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
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;
    }
}
Offline Regenuluz
« Reply #4 - Posted 2011-12-23 12:18:06 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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. Smiley Maybe someone better at this whole game making thing can? ^_^
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!