Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
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  
  Java pong game, with level collision not working  (Read 3349 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-08-13 20:15:24 »

          I've been trying to make a pong game recently, and I can't get the rectangle collision right, between a paddle(one rectangle) and a few tiles (another few rectangles). I've tried many ways to do the collision, but I can't get it right.

Here are some Images you might need
http://s1201.photobucket.com/albums/bb356/SubzeroX5/

Here is my current code

Element
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  
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;


public class Element {

    public int x;
    public int y;
    public int w;
    public int h;
    public int id;
    public Image image;
    public Rectangle cr;

    public Element(int x2, int y2, int n, Image i) {
        this.x = x2;
        this.y = y2;
        this.id = n;
        this.image = i;
    }

    public int getID() {
        return id;
    }

    public Image getImage() {
        return image;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return w;
    }

    public int getHeight() {
        return h;
    }
    public Rectangle getCollisionRec(){
        return cr = new Rectangle(getX(), getY(),getWidth(),getHeight());
    }
    public void draw(Graphics2D g) {
        g.drawImage(getImage(), getX(), getY(), null);
        g.draw(getCollisionRec());
        g.setPaint(null);
        g.setColor(Color.GRAY);
    }
}



Templetes
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
import java.awt.Image;

public class Templetes extends Element {

    ///////////////For 4 sided shapes///////////////
   public Templetes(int x, int y,int n, Image i) {
        super(x, y, n,i);
    }
    ////////////////////////////////////////////////
}


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  
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;

public class Level {

    public Element[][] overallLevel;
    public Element[] elements;
    public Rectangle[][] collisionRecs;
    public Rectangle[] recColElements;
    public int Lines;
    public int levelLength;
    public int leftBorder;
    public int rightBorder;
    public int unit = 20;
    /////////Images/////////
   public Image background;
    public Image square13;
    /////////////////////////

    public Image getBackground() {
        return background;
    }

    public Level() {
        square13 = new ImageIcon(this.getClass().getResource("/ultrapong/levels/images/Square13.png")).getImage();

    }
    /*
    Legend
    : = empty slot
    S = square with all borders
   
     */


    public void defineLevel(String[] definitions) {
        overallLevel = new Element[Lines][definitions[0].length()];
        collisionRecs = new Rectangle[Lines][definitions[0].length()];

        levelLength = definitions[0].length() * unit;
        leftBorder = 0;
        rightBorder = 500;

        int elementsCounter = 0;
        for (int i = 0; i < definitions.length; i++) {
            char[] lineDefinition = definitions[i].toCharArray();
            for (int j = 0; j < lineDefinition.length; j++) {
                if (lineDefinition[j] == ':') {
                    overallLevel[i][j] = null;
                    collisionRecs[i][j] = null;
                } else if (lineDefinition[j] == 'S') {
                    Templetes t13 = new Templetes(j * unit, i * unit, 1, square13);
                    overallLevel[i][j] = t13;
                    collisionRecs[i][j] = t13.getCollisionRec();
                    elementsCounter++;
                }
            }
        }
        elements = new Element[elementsCounter];
        recColElements = new Rectangle[elementsCounter];

        int counter = 0;

        for (int i = 0; i < overallLevel.length; i++) {
            for (int j = 0; j < overallLevel[i].length; j++) {
                if (overallLevel[i][j] != null) {
                    elements[counter] = overallLevel[i][j];
                    recColElements[counter] = collisionRecs[i][j];
                    counter++;
                }
            }
        }

    }

    public void checkCollision(Rectangle p, Rectangle t) {
        if (recColElements != null) {
            if (t.intersects(p)) {
                if (p.x + p.width == t.x) {
                    p.x = t.x - p.width;
                } else if (p.x == t.x + t.width) {
                    p.x = t.x + t.width;
                }

            }
        }
    }

    public void drawLevel(Graphics2D g) {
        try {
            for (int i = 0; i < elements.length; i++) {
                elements[i].draw(g);
            }
        } catch (Exception e) {
            System.out.toString();
        }
    }

    public void drawBackground(Graphics2D g) {
        g.drawImage(getBackground(), 0, 0, null);
    }
}

BoxLevel
[code]
import javax.swing.ImageIcon;
import ultrapong.levels.Config.*;

public class BoxLevel extends Level {

    public static final String row1 = "SS:::::::::::::::::::::SS";
    public static final String row2 = "SS:::::::::::::::::::::SS";
    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 = ":::::::::::::::::::::::::";
    public static final String row16 = ":::::::::::::::::::::::::";
    public static final String row17 = ":::::::::::::::::::::::::";
    public static final String row18 = ":::::::::::::::::::::::::";
    public static final String row19 = ":::::::::::::::::::::::::";
    public static final String row20 = ":::::::::::::::::::::::::";
    public static final String row21 = ":::::::::::::::::::::::::";
    public static final String row22 = "SS:::::::::::::::::::::SS";
    public static final String row23 = "SS:::::::::::::::::::::SS";
    public static final String row24 = ":::::::::::::::::::::::::";
    public static final String row25 = ":::::::::::::::::::::::::";

    public BoxLevel() {
        Lines = 25;
        background = new ImageIcon(this.getClass().getResource("/ultrapong/levels/images/backgrounds/whitebackground.PNG")).getImage();
        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);

    }
}


Paddle
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  
import java.awt.*;
import javax.swing.*;

public class Paddle {

    public Image paddleImage;
    public int width;
    public int height;
    public Rectangle paddleRec;
    public float x, y;
    public float originalX;
    public int centerX = width / 2;

    public Paddle(int i) {
        paddleImage = new ImageIcon(this.getClass().getResource("/ultrapong/Paddle" + i + ".png")).getImage();
        width = paddleImage.getWidth(null);
        height = paddleImage.getHeight(null);

    }
    public float recX;
    public float recY;
    public float moveX;
    public boolean movable = false;

    public Rectangle paddleRec() {
        return new Rectangle(Math.round(getX()), Math.round(getY()), getWidth(), getHeight());
    }

    public void update() {
        keepinbounds();
        move();
    }

    public void keepinbounds() {
        if (x < 0) {
            x = 0;
            moveX = 0;
        }
        if (y < 0) {
            y = 0;
        }
        if (x + width > 500) {
            x = 500 - width;

        }
        if (y + height > 500) {
            y = 500 - height;
        }
        if (x < 0) {
            x = 0;

        }
        if (y < 0) {
            y = 0;
        }

    }

    public float move() {
        return x = x + moveX;

    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public Image getImage() {
        return paddleImage;
    }
}


Player
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  
import java.awt.event.KeyEvent;

public class Player extends Paddle {

    public Player() {
        super(1);
        y = 450;
        x = 220;
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
            moveX = 4;
        }
        if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
            moveX = -4;

        }

    }

    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
            moveX = 0;
        }
        if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
            moveX = 0;
        }
    }
}


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  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import ultrapong.levels.*;
import ultrapong.levels.Config.*;

public class MainConfig extends JPanel implements Runnable {

    public int setSizeX, setSizeY;
    public Player player;
    public Thread mainLoop;
    public Ball ball = new Ball();
    public Level currentLevel;
    public int counter;

    public MainConfig() {
        player = new Player();
        setDoubleBuffered(true);
        addKeyListener(new KL());
        setFocusable(true);
        setSizeX = 506;
        setSizeY = 498;
        currentLevel = new BoxLevel();
    }

    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        currentLevel.drawBackground(g2d);
        g2d.drawImage(player.getImage(), player.paddleRec().x, player.paddleRec().y, null);
        g2d.drawImage(ball.getImage(), ball.ball().x, ball.ball().y, null);
        currentLevel.drawLevel(g2d);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public class KL extends KeyAdapter {

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

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

    public void update() {
        ball.updateBounce(player.paddleRec(), ball.ball());
        ball.update();
        player.update();
        for (int i = 0; i < currentLevel.elements.length; i++) {
            currentLevel.checkCollision(player.paddleRec(), currentLevel.recColElements[i]);
        }
    }

    public void run() {

        float startingTime = System.currentTimeMillis();
        float cumTime = System.currentTimeMillis() - startingTime;
        while (true) {
            float timePassed = System.currentTimeMillis() - cumTime;
            cumTime += timePassed;
            update();
            repaint();
            try {
                Thread.sleep(20);
            } catch (Exception e) {
                System.err.toString();
            }
        }
    }
}


Ball
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  
import java.awt.Rectangle;
import java.util.Random;
import java.awt.Image;
import javax.swing.ImageIcon;
import static java.lang.Math.*;


public class Ball {

    public int x;
    public int y;
    public int dx = 0;
    public int dy = 0;
    public int diameter;
    public Image ballImg;
    public Player player = new Player();

    public Ball() {
        x = 250;
        y = 250;
        dx = 3;
        dy = 3;
        diameter = 5;
        ballImg = new ImageIcon(this.getClass().getResource("/ultrapong/Ball.png")).getImage();
    }
    public Random random = new Random();

    public void updateBounce(Rectangle r, Rectangle b) {
        if (r.intersects(b)) {
            bounceY();
        }
        if (getY() >= 500 - 10 && getX() >= player.getX() && getX() <= player.x + player.paddleRec().width) {
            bounceY();
        }
    }

    public void update() {
        if (y < 0) {
            bounceY();
        }
        if (y + diameter > 460) {
            for (int i = 250; i < 350 && i > 100; i++) {
                y = random.nextInt(i);
            }
            for (int i = 250; i < 350 && i > 100; i++) {
                x = random.nextInt(i);
            }
            for (int i = 0; i < 3 && i > 0; i++) {
                dx = random.nextInt(i);
            }
            for (int i = 0; i < 3 && i > 0; i++) {
                dy = random.nextInt(i);
            }
        }
        if (x < 0) {
            bounceX();
        }
        if (x + diameter + (diameter / 2) > 500) {
            bounceX();
        }
        x = x + dx;
        y = y + dy;

    }

    public Rectangle ball() {
        return new Rectangle(getX(), getY(), diameter(), diameter());
    }

    public Image getImage() {
        return ballImg;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int diameter() {
        return diameter;
    }

    public void velocityX(int vx) {
        this.dx = vx;
    }

    public void velocityY(int vy) {
        this.dy = vy;
    }

    public void bounceX(Paddle p) {
        double angle = atan(dx / dy);
        if (dx < 0) {
            angle += PI;
        }
        double mag = sqrt(pow(dy, 2) + pow(dx, 2));
        dx *= -1;
    }

    public void bounceX() {
        dx *= -1;
    }

    public void bounceY(Paddle p) {
        double angle = atan(dx / dy);
        if (dy < 0) {
            angle += PI;
        }
        double mag = sqrt(pow(dy, 2) + pow(dx, 2));
        dy += -1;
    }

    public void bounceY() {
        dy *= -1;
    }
}


UltraPongCopy
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
import javax.swing.JFrame;


public class UltraPongCopy {

       public UltraPongCopy() {
        JFrame f = new JFrame();
        MainConfig mc = new MainConfig();
        f.add(mc);
        f.setTitle("UltraPong");
        f.setSize(mc.setSizeX , mc.setSizeY);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setResizable(false);
    }

    public static void main(String[] args) {
        UltraPongCopy ultraPongCopy = new UltraPongCopy();
    }
}

[/code]
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-08-13 22:24:29 »

Without look at much of your code, what exactly is going wrong?

Offline SubzeroX5

Senior Newbie





« Reply #2 - Posted 2011-08-13 22:55:16 »

Every time my paddle intersects the tile, it goes through.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #3 - Posted 2011-08-13 23:02:53 »

You need to check your variables of all your rects/x/y etc..

debug or do a system.out or something so you can manually see ALL the variables involved in the collision
x/y/width/height of ball rect and of your paddle rect


Are you sure you have a proper width/height set in your paddle rect?
1  
2  
3  
4  
5  
6  
....
  public Paddle(int i) {
        paddleImage = new ImageIcon(this.getClass().getResource("/ultrapong/Paddle" + i + ".png")).getImage();
        width = paddleImage.getWidth(null);
        height = paddleImage.getHeight(null);
...



Do all the other aspects of your code work? i.e. does the ball bounce off the sides and top?  does it bounce off the blocks?
does anything thats not supposed to go outside the screen, can go outside the screen?

"Experience is what you get when you did not get what you wanted"
Offline SubzeroX5

Senior Newbie





« Reply #4 - Posted 2011-08-13 23:58:35 »

Thanks for reminding me to check again, I forgot to set the size of t13's rectangle width and height. But even though that, I made a System.out.println in Level's checkCollision(), and even though the system says "Has Intersected", it still couldn't return paddleRec()'s x and width.

Method checkCollision() in Level.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    public void checkCollision(Rectangle p, Rectangle t) {
        if (recColElements != null) {
            for (int i = 0; i < recColElements.length; i++) {
                if (t.intersects(p)) {
                    System.out.println("Has intersected");
                    if (p.x + p.width >= t.x) {
                        p.x = t.x - p.width;
                    }
                    if (p.x <= t.x + t.width) {
                        p.x = t.x + t.width;
                    }

                }
            }
        }
    }
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #5 - Posted 2011-08-14 04:23:49 »

The next problem I see is that when you have them intersect, you have it multiply dy *= -1

The problem with this, is that sometimes the rectangle in 1 move goes far enough inside the other rectangle that it tries to go "up" but is still inside the other, so thereby intersects a 2nd time and flips it back to down. and this repeats and it behaves oddly. Sometimes itll fly "right through it" sometimes itll jitter around really strangely

You should have a "hitPaddle()" function that forces it to be upward moving, and not have it do a *=-1 but a straight =-1 or whatever value. So that even if its slightly "overlapping" itll still continue on an upward path.  Then just use a different function for your other direction

"Experience is what you get when you did not get what you wanted"
Offline SubzeroX5

Senior Newbie





« Reply #6 - Posted 2011-08-14 18:42:25 »

Thanks, I'll try it out.
Offline SubzeroX5

Senior Newbie





« Reply #7 - Posted 2011-08-14 19:04:47 »

I'm not sure I did it right,but is it ok if I do it this way.
1  
2  
3  
public void bounceY() {
        dy = dy + -(dy * 2);
    }
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2011-08-14 19:30:16 »

unless I am mistaken you can make that a lot smaller, and just be

dy-=2dy
Offline SubzeroX5

Senior Newbie





« Reply #9 - Posted 2011-08-14 19:48:36 »

Sorry,the java compiler doesn't like it.

If your curious, this is what it said
1  
2  
3  
4  
5  
6  
Exception in thread "Thread-2" java.lang.RuntimeException: Uncompilable source code - not a statement
   at ultrapong.Ball.bounceY(Ball.java:127)
   at ultrapong.Ball.updateBounce(Ball.java:40)
   at ultrapong.MainConfig.updatePosistion(MainConfig.java:61)
   at ultrapong.MainConfig.run(MainConfig.java:73)
   at java.lang.Thread.run(Thread.java:722)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #10 - Posted 2011-08-14 19:49:03 »

The way you have your intersect code written, I was suggesting a bounceYUp() and bounceYDown()

Although I haven't personally tested the code, Ive only looked at it, the problem arises when the rectangle intersects the same rectangle several frames in a row, and the bounceY is called 4 or 5 times during its interaction. That is why I was suggesting to not use a function/method that flip/flips back and forth.  In some cases that flip/flop function works fine, but the way its set up currently, It could cause potential problems.

An easy way to test this, is to add a "system.out.println("bounce") inside your bounceY code and see if it is triggered multiple times when it hits the paddle.

"Experience is what you get when you did not get what you wanted"
Offline SubzeroX5

Senior Newbie





« Reply #11 - Posted 2011-08-14 21:35:10 »

Thanks for your reply Cheesy

I did some variations and made the code work.

New Variable
1  
public int hit = 0;

Edited bounceY()
1  
2  
3  
4  
5  
6  
7  
8  
public void bounceY() {
        if (hit == 0) {
            dy *= -1;
            hit++;
        }else{
            dy *= 1;
        }
    }


If you don't mind, is their a way, so that when paddleRec() intersect recColElements[], it dosen't go through recColElements[].
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #12 - Posted 2011-08-14 21:40:21 »

what do you mean
 so that when paddleRec() intersect recColElements[], it dosen't go through recColElements[].


You can have it just exit the for loop once it intersects one.

"Experience is what you get when you did not get what you wanted"
Offline SubzeroX5

Senior Newbie





« Reply #13 - Posted 2011-08-14 21:48:32 »

Like,when paddleRec() intersects a rectangle from recColElements[], the paddle goes right through the actual rectangle.
Offline SubzeroX5

Senior Newbie





« Reply #14 - Posted 2011-08-15 03:49:01 »

Sorry If I didn't make sense, its kinda hard to explain.

This is what it looks like.
http://s1201.photobucket.com/albums/bb356/SubzeroX5/?action=view&current=UltraPong1.png
Offline aazimon
« Reply #15 - Posted 2011-08-15 20:21:01 »

Looking at your Element class, I don't see the height and width getting set. The int will default to zero, which would explain why you are not getting a collision with them. Get the height and width from the image passed in the constructor. That should help.
Offline SubzeroX5

Senior Newbie





« Reply #16 - Posted 2011-08-15 20:44:31 »

Actually, I already fixed that on my third post in this thread, but thanks.
Offline SubzeroX5

Senior Newbie





« Reply #17 - Posted 2011-08-16 19:48:14 »

I've fixed some of collision between the paddle and recColElements[]. But the new problem is, that every time my paddle intersects the right of a rectangle from recColElements[], it teleports to the left of it.

Heres the code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void checkCollision(Player p, Level r) {
        for (int i = 0; i < r.recColElements.length; i++) {
            int recLeftBorder = r.recColElements[i].x;
            int recRightBorder = r.recColElements[i].x + r.recColElements[i].width;
            System.out.println("Has intersected");
            if (p.paddleRec().intersects(r.recColElements[i]) && p.x + p.width >= recLeftBorder) {
                p.x = recLeftBorder - p.width;
                System.out.println("right");
            } else if (p.paddleRec().intersects(r.recColElements[i]) && p.x <= recRightBorder) {
                System.out.println("left");
                p.x = recRightBorder;

            }

        }
    }


Picture for reference
http://s1201.photobucket.com/albums/bb356/SubzeroX5/?action=view&current=UltraPong2.png
Offline SubzeroX5

Senior Newbie





« Reply #18 - Posted 2011-08-16 21:18:30 »

I've finally found a solution to my bug  Grin

Here is the checkCollisionCode

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    public void checkCollision(Player p, Level r) {
        for (int i = 0; i < r.recColElements.length; i++) {
            int recLeftBorder = r.recColElements[i].x;
            int recRightBorder = r.recColElements[i].x + r.recColElements[i].width;
            int middleBorder = r.recColElements[i].x + (r.recColElements[i].width/2);
            System.out.println("Has intersected");
            if (p.paddleRec().intersects(r.recColElements[i]) && p.x + p.width > recLeftBorder && p.x + p.width < middleBorder) {
                p.x = recLeftBorder - p.width;
            }
            if (p.paddleRec().intersects(r.recColElements[i]) && p.x < recRightBorder && p.x > middleBorder) {
                p.x = recRightBorder;
            }

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

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!