Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Pong Physics problem  (Read 1836 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-10-10 15:57:52 »

In my pong game,I have tiles/blocks in my game. With these blocks, I have 4 rectangles for collision, theirs one for the left side,right side, top side, bottom side. One of the problems, is that when the left and top side intersect with each other, the balls y axis isn't changed when they intersect.

Example : if dy(distance y) equals 3, and it intersects the left and top side rectangles, dy still equals 3.

This also happens with other rectangle(when left intersects bottom rectangle,when right intersects bottom top rectangle, etc.).

Source can be found here(updated link)
http://www.mediafire.com/?e66laoa29pys26c

Offline Cero
« Reply #1 - Posted 2011-10-10 16:43:41 »

Sounds to me as if your reaction to two collisions which are happening when in fact you should only react to one and drop the other.

would help if you tell us where in your code is this logic / the problem

Offline SubzeroX5

Senior Newbie





« Reply #2 - Posted 2011-10-10 17:07:59 »

Heres where the logic problem occurs.

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  
 public void updateBounceTilesL(Rectangle l, Ball ball) {
        if (dx > 0) {
            if (l.intersects(ball.ball())) {
                if (!bounced) {
                    dx = bounceX(dx);
                    bounced = true;
                } else if (bounced) {
                    dx = dx * 1;
                }
                //System.out.println("Intersected left");
            } else if (!l.intersects(ball.ball())) {
                bounced = false;
            }
        }
    }

    public void updateBounceTilesR(Rectangle r, Ball ball) {
        if (dx < 0) {
            if (r.intersects(ball.ball())) {
                if (!bounced) {
                    if (dx > 0) {
                        dx = bounceX(dx);
                        bounced = true;
                    }
                } else if (bounced) {
                    dx = dx * 1;
                }
                //System.out.println("Intersected right");
            } else if (!r.intersects(ball.ball())) {
                bounced = false;
            }
        }
    }

    public void updateBounceTilesT(Rectangle t, Ball ball) {
        if (dy > 0) {
            if (t.intersects(ball.ball())) {
                if (!bounced) {
                    dy = bounceY(dy);
                    bounced = true;
                } else if (bounced) {
                    dy = dy * 1;
                }
                //System.out.println("Intersected Top");
            } else if (!t.intersects(ball.ball())) {
                bounced = false;
            }
        }
    }

    public void updateBounceTilesB(Rectangle b, Ball ball) {
        if (dy < 0) {
            if (b.intersects(ball.ball())) {
                if (!bounced) {
                    dy = bounceY(dy);
                    bounced = true;
                } else if (bounced) {
                    dy = dy * 1;
                }
                //System.out.println("Intersected Bottom");
            } else if (!b.intersects(ball.ball())) {
                bounced = false;
            }
        }
    }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2011-10-10 17:15:52 »

ok so my first idea would be:

change these methods into boolean methods and instead of the bounced boolean, these methods return true or false

then, wherever you call these methods, if one of the is already true, dont check the others

you seem to check for it...

and the line dx = dx * 1; doesnt do anything

Offline Cero
« Reply #4 - Posted 2011-10-10 17:22:19 »

and the line dx = dx * 1; doesnt do anything

and it seems you do this * 1 thing more often

1  
2  
3  
public AI() {
super(2);x * 1;
...


not only does
1  
x * 1;
doesnt do anything, because something multiplied with 1 is still the same thing, also, you dont pass the value to anything, you dont do anything with it.

Offline SubzeroX5

Senior Newbie





« Reply #5 - Posted 2011-10-11 02:51:45 »

Thanks for your advice, it helped me a lot. But now, I think I have a problem with my math, and I can't seem to figure it out.
Heres the new code

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  
 public boolean updateBounceTilesL(Rectangle l, Ball ball) {
        if (dx > 0 && l.x <= ball.x + ball.diameter) {
            if (l.intersects(ball.ball())) {
                dx = bounceX(dx);
                hit.play();
                return true;
            } else if (!l.intersects(ball.ball())) {
                return false;
            }
        }
        return false;
    }

    public boolean updateBounceTilesR(Rectangle r, Ball ball) {
        if (dx < 0 && r.x >= ball.x + ball.diameter) {
            if (r.intersects(ball.ball())) {
                dx = bounceX(dx);
                hit.play();
                return true;
            } else if (!r.intersects(ball.ball())) {
                return false;
            }
        }
        return false;
    }

    public boolean updateBounceTilesT(Rectangle t, Ball ball) {
        if (dy > 0 && t.y >= ball.y + ball.diameter) {
            if (t.intersects(ball.ball())) {
                dy = bounceY(dy);
                hit.play();
                return true;
            } else if (!t.intersects(ball.ball())) {
                return false;
            }
        }
        return false;
    }

    public boolean updateBounceTilesB(Rectangle b, Ball ball) {
        if (dy < 0 && b.y <= ball.y + ball.diameter) {
            if (b.intersects(ball.ball())) {
                dy = bounceY(dy);
                hit.play();
                return true;
            } else if (!b.intersects(ball.ball())) {
                return false;
            }
        }
        return false;
    }
Offline Cero
« Reply #6 - Posted 2011-10-13 11:49:44 »

eeeh well

if your Rectangle is the AWT Rectangle then to use .intersects I guess your Ball is a sub class of Shape
I haven't done it like that, but if done right intersects() should return whether or not there is a collision

Actually haven't done circle collision, but its one of these "program once - never look into again" codes

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-10-13 21:38:00 »

Circle collision is the easiest: test if the radii overlap Smiley

Offline Cero
« Reply #8 - Posted 2011-10-14 09:34:06 »

Circle collision is the easiest: test if the radii overlap Smiley

Yeah its simple geometry. What I'm saying is that methods like these are black boxes that solve problems, and at some point I don't know how it is it's job, and I don't care, but I have to be sure that it is doing it correctly of course =P

Offline SubzeroX5

Senior Newbie





« Reply #9 - Posted 2011-10-14 20:13:51 »

Actually, the ball has a rectangle for collision, and isn't a sub class of anything. The ball isn't very big, so I just used a rectangle for collision, since using a circle for collision is harder than using a rectangle. For some reason, when the ball hits the left or top side of the tiles, it goes through or doesn't bounce the right way.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-10-14 21:01:09 »

Circle collision detection:
1  
2  
3  
4  
5  
6  
if(distance(circle1.center(),circle2.center()) <= circle1.radius()+circle2.radius())
    //INTERSECTION!

public static int distance(Point p1, Point p2) {
    return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

Offline Cero
« Reply #11 - Posted 2011-10-14 21:15:56 »

Actually, the ball has a rectangle for collision, and isn't a sub class of anything.

Cannot be. The method is Rectangle.intersects(Rectangle r), it excepts an Rectangle
so your Ball class extends Rectangle D=
or else it wouldnt compile

The ball isn't very big, so I just used a rectangle for collision
Yeah thats fine

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-10-14 21:21:27 »

Actually, the ball has a rectangle for collision, and isn't a sub class of anything.

Cannot be. The method is Rectangle.intersects(Rectangle r), it excepts an Rectangle
so your Ball class extends Rectangle D=
or else it wouldnt compile
I think he means he used a Rectangle object as a variable inside his Ball class Wink

Offline SubzeroX5

Senior Newbie





« Reply #13 - Posted 2011-10-15 03:24:34 »

That's correct.
Offline SubzeroX5

Senior Newbie





« Reply #14 - Posted 2011-10-15 03:51:25 »

So far, I almost found a solution to my problem. (I added some more thin)

Here are the changes in the code(You can also see the diffrences in the codes, if you download the source.Link also updated
http://www.mediafire.com/?4eubm7v6u8cyakq).

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  
    public void update() {
        boolean bounced = false;
        if (dx < 0) {
            ndx = Math.abs(dx);
        } else if (dx > 0) {
            ndx = dx - (dx * 2);
        }
        if (dy < 0) {
            ndy = Math.abs(dy);
        } else if (dy > 0) {
            ndy = dy - (dy * 2);
        }
        if (y + diameter > 460 || y < 0) {
            dx = 3;
            dy = 3;
            x = 250;
            y = 250;
        } else if (x + diameter + (diameter / 2) > 500 || x < 0) {
            if (!bounced) {
                dx = ndx;
                hit.play();
                bounced = true;
            }
        }
        if (moveable) {
            x = x + dx + pauseSpeedX;
            y = y + dy + pauseSpeedY;
        } else if (!moveable) {
            x = x + 0;
            y = y + 0;
        }
    }


    public boolean updateBounceTilesL(Rectangle l, Ball ball) {
        if (dx > 0) {
            if (l.intersects(ball.ball())) {
                dx = ndx;
                hit.play();
                return true;
            } else if (!l.intersects(ball.ball())) {
                return false;
            }
        }

        return false;
    }

    public boolean updateBounceTilesR(Rectangle r, Ball ball) {
        if (dx < 0) {
            if (r.intersects(ball.ball())) {
                dx = ndx;
                hit.play();
                return true;
            } else if (!r.intersects(ball.ball())) {
                return false;
            }
        }

        return false;
    }

    public boolean updateBounceTilesT(Rectangle t, Ball ball) {
        if (dy > 0) {
            if (t.intersects(ball.ball())) {
                dy = ndy;
                hit.play();
                return true;
            } else if (!t.intersects(ball.ball())) {
                return false;
            }
        }

        return false;
    }

    public boolean updateBounceTilesB(Rectangle b, Ball ball) {
        if (dy < 0) {
            if (b.intersects(ball.ball())) {
                dy = ndy;
                hit.play();
                return true;
            } else if (!b.intersects(ball.ball())) {
                return false;
            }
        }

        return false;
    }
Offline Cero
« Reply #15 - Posted 2011-10-15 09:57:41 »

Actually, the ball has a rectangle for collision, and isn't a sub class of anything.

Cannot be. The method is Rectangle.intersects(Rectangle r), it excepts an Rectangle
so your Ball class extends Rectangle D=
or else it wouldnt compile
I think he means he used a Rectangle object as a variable inside his Ball class Wink

ah yes
1  
if (r.intersects(ball.ball()))

Ball.ball()... returns a Rectangle
I would assume that one would just do intersects(ball) Grin

Offline SubzeroX5

Senior Newbie





« Reply #16 - Posted 2011-10-15 15:50:02 »

That is also correct.
Also, sorry about the wrong download link.
Here's the correct one.
http://www.mediafire.com/?e66laoa29pys26c
Offline Cero
« Reply #17 - Posted 2011-10-15 18:51:23 »

so what problem remains ? (not looking at your code, because lazy Grin)

Offline SubzeroX5

Senior Newbie





« Reply #18 - Posted 2011-10-15 19:08:31 »

For some reason, when the ball hits intersects the block diagonally the first time, it bounces the same direction.

(Example : before intersection, dx = 3, dy = 3, after intersection, dy = -3, dy = -3, where after intersection it should be dx = -3, dy = 3)
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.

Mr.CodeIt (12 views)
2014-12-27 04:03:04

TheDudeFromCI (16 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02

SHC (96 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

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