Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (605) 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 1918 times) 0 Members and 1 Guest are viewing this topic.
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

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

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

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.

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

ra4king

JGO Kernel

Medals: 367
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

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

Circle collision is the easiest: test if the radii overlap

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

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

JGO Kernel

Medals: 367
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));}`

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

ra4king

JGO Kernel

Medals: 367
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

SubzeroX5

Senior Newbie

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

That's correct.
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;    }`
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

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

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

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
Cero
 « Reply #17 - Posted 2011-10-15 18:51:23 »

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

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.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (37 views) 2015-03-13 17:50:16 Olo (43 views) 2015-03-13 17:47:07 ClaasJG (56 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 basil_ 22x BurntPizza 21x KevinWorkman 21x LiquidNitrogen 21x EgonOlsen 17x theagentd 16x Roquen 16x princec 12x Varkas 10x Riven 10x SHC 9x 65K 9x Ashedragon 8x lcass 7x teletubo 7x Ecumene 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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