Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Another collision question  (Read 14095 times) 0 Members and 1 Guest are viewing this topic.
CyanPrime
 « Posted 2011-03-28 03:37:00 »

This is over my head, can someone explain it to me better? http://mathworld.wolfram.com/Reflection.html

I'm making a 2d breakout fighting game, so I need the ball to be able to reflect when it hits a wall, paddle, or enemy (or a enemy hits it).

all their formula's are like: x_1^'-x_0=v-2(v·n^^)n^^.

And I can't fallow that. (What does ' mean or x_0? or ^^?)
bobjob

JGO Knight

Medals: 12
Projects: 4

David A M

 « Reply #1 - Posted 2011-03-28 04:30:42 »

A paddle war game should be considerably simple.

Currently how are you doing your collision? Rectangles or does the game support diagnal lines?

If no diagnal lines, then all you need to do is negate the direction of the ball on one of the axis, like if the game flow is up and down, then just negate on the y axis. And if it hits a side wall negate the x axis.

Also take into consideration either: the momentum of the paddle when the ball is hit, or the area on which the ball hit that paddle. This will make a sharper angle, depending on how you want the game to feel.

If you are using diagnal line collisions, then it will work a little differently.

CyanPrime
 « Reply #2 - Posted 2011-03-28 04:35:19 »

Well, it's going to be you vs a boss. Kinda like this: http://www.youtube.com/watch?v=jo5tW7fk1sc

There are no diagonal lines, but if the boss swats the ball I would need to know the angle he hit the ball at, and make it go flying in the reverse angle. That's why I'm trying to make the collision detection so complicated.
bobjob

JGO Knight

Medals: 12
Projects: 4

David A M

 « Reply #3 - Posted 2011-03-28 04:42:36 »

that game is really cool. I thought you ment a paddle war, but this seems more like a pinball machine.

In that case you would need to make the ball entity store an angle (direction it is moving) and a momentum value to store its speed.

then apply a gravity value on each game cycle.

are you familar with line equations (eg. y = mx + c)?
CyanPrime
 « Reply #4 - Posted 2011-03-28 05:04:03 »

No, no. Mine's a paddle game.

Nope, don't know what that formula means at all.

This is what I got for my collision right now, but the ball just stops when it hits a wall.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 for(int i = 0; i < wall.length; i++){         if(new Rectangle((int) position.x, (int) position.y, size, size).intersects(new Rectangle(wall[i].x, wall[i].y, wall[i].width, wall[i].height))){            Vector2f rvelo = velo.copy(); //get velo (direction)            rvelo.set(rvelo.x*-1, rvelo.y*-1); //reverse it for back tracking                        position.add(rvelo);            Vector2f prevpos = position.copy();            prevpos.add(velo);            System.out.println("Angle: " + Math.toDegrees(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))));                        velo.x = (float) (Math.sin(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))) * 5);            velo.y = (float) (Math.cos(Math.atan2(-(position.x -prevpos.x), (position.y - prevpos.y))) * 5);                        //position.x += (wall[i].x + wall[i].width) - position.x;            //velo.x = -velo.x;            accel = velo.scale(speed);            velo.add(accel);                      }      }
lhkbob

JGO Knight

Medals: 32

 « Reply #5 - Posted 2011-03-28 06:41:33 »

The ^ symbol over the n in the variable names is a common symbol for a normalized vector.  If the variable doesn't have a ^, then in the equations in that link, it represents a point.  When you subtract two points, you get a vector representing the difference.  The ' symbol is just part of the variable name, it usually symbolizes "new version".  So x1' means the new x1, or in this case the reflected x1.

Another way to write the reflection equation (x1' = -x1 + 2x0 + 2n[(x1-x0)•n]) in a more programmatic way is:
 1  2  3  4 Vector2f dir = ...Vector2f n = dir.normalize();Vector2f reflected = x0.scale(2).sub(x1).add(n.scale(2 * dot(n, x1.sub(x0)));

In the above example, x0 and x1 are the points in the diagram from the link, but they're stored as the same Vector2f type.  Vector2f is a type I made up for this example, the methods used are scale(float), sub(Vector2f), add(Vector2f), normalize() and dot(Vector2f, Vector2f).  These are all vector math operations that are pretty easy to implement.  In this code, I'm assuming that a new Vector2f is created by each operation so that the proper order of operations is preserved.

HTH

ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #6 - Posted 2011-03-28 09:41:53 »

Does dot product mean multiplying 2 vectors?
And what does normalize really mean?

DzzD
 « Reply #7 - Posted 2011-03-28 10:18:33 »

Does dot product mean multiplying 2 vectors?
And what does normalize really mean?

dot product is :
dotProduct= x1*x2+y1*y2+z1*z2
or
dotProduct= |V1| * |V2| * cos(angleBetweenV1AndV2)

it is especially usefull to find angle between two vectors, or to make a projection of one vector onto another (changing frame of reference)

normalizing is an operation that make the final vector 1 unit lenght by dividing all its components by the initial vector length :
vectorLength=sqrt(x*x+y*y+z*z);
x/=vectorLength
y/=vectorLength
z/=vectorLength

refri89

Senior Newbie

 « Reply #8 - Posted 2011-03-28 10:30:13 »

.
Roquen

JGO Kernel

Medals: 517

 « Reply #9 - Posted 2011-03-28 15:30:40 »

I'm assuming this is actually 2D and not 3D.  It's easy to just remember that:  "The angle of incidence equals the angle of reflection".  So the angle (with respect to the normal of the surface) that the ball strikes the surface is the same as the angle that it will move away from the surface, on the opposite side of the normal.  In break-out style games, most of the surfaces will be aligned to an axis and the "equation" is simple:  Striking the top/bottom = negate y (assuming y is up/down) and likewise striking the sides negates x.
CyanPrime
 « Reply #10 - Posted 2011-03-28 20:29:06 »

x0 and x1 are the points in the diagram from the link
Everything looks good so far, but I can't figure out what points x0 and x1 are, and what they mean. I think x0 is 0,0 bu I have no idea what to put for x1. This is my code (Using slick2d's Vector2f class)

 1  2  3  4  5  6 Vector2f n = velo.normalise();            Vector2f x0 = new Vector2f(0,0);            Vector2f x1 = new Vector2f(0,0);                        Vector2f reflected = x0.scale(2).sub(x1).add(n.scale(2 * n.dot(x1.sub(x0))));            velo = reflected.copy();
avm1979
 « Reply #11 - Posted 2011-03-28 21:02:05 »

x0 is the point where the collision occurs.

n is the line which the ball hit (i.e., one of the 4 edges of the rectangle you're checking against).  x0 is on that line.

Looking at your code, it doesn't seem like you're computing the actual collision point, just that a collision actually occurred.  That's not enough to compute reflection, though you could possibly hack around it.

... also, you're setting both x0 and x1 to (0, 0) and then doing stuff with them like scaling.

In your case, if I understand correctly, all of the wall pieces are aligned with the x and y axis.  If so, all you have to do is flip the x or y component of your velocity, depending on whether the wall piece you are checking against is to the left/right/above/below the ball.  For example, if it's above or below the ball, you'd flip the y component.

You might get into trouble if multiple collisions are detected - but that's going to be tricky regardless of what you do.

I'd also suggest taking a step back to try and really understand the math. Vectors are very useful.

CyanPrime
 « Reply #12 - Posted 2011-03-28 21:39:01 »

Anyone know why this collision for my enemy doesn't work right? It seems when hit from the side it goes through him instead of bouncing off.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 if(new Rectangle((int) position.x, (int) position.y, size, size).intersects(new Rectangle((int) enemy.x, (int) enemy.y, enemy.width, enemy.height))){         if(position.y + size >= enemy.y && position.y + size <= enemy.y + (enemy.height/6))            velo.y = -velo.y;                  else if(position.y <= enemy.y + enemy.height && position.y >= enemy.y + enemy.height - (enemy.height/6))            velo.y = -velo.y;                  else            velo.x = -velo.x;                  enemy.hp--;      }
Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #13 - Posted 2011-03-28 21:50:22 »

Sorry to be so intrusive, but I merged your topics.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
CyanPrime
 « Reply #14 - Posted 2011-03-28 22:08:34 »

That's fine. But this last question wasn't using reflection, or even trying to ^^;
Riven

« JGO Overlord »

Medals: 1340
Projects: 4
Exp: 16 years

 « Reply #15 - Posted 2011-03-28 22:09:39 »

I merged it because you're littering JGO. You shouldn't start a new thread for every tiny problem you run into.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
CyanPrime
 « Reply #16 - Posted 2011-03-28 22:15:53 »

Fair enough. +1 rep.
ra4king

JGO Kernel

Medals: 508
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #17 - Posted 2011-03-28 22:33:09 »

Fair enough. +1 rep.
Whaattt? Y did u give him a rep?!? He has too many alreadddyy

appel

JGO Wizard

Medals: 80
Projects: 4

I always win!

 « Reply #18 - Posted 2011-03-28 23:22:30 »

This is why I prefer to have x,y of my entities as the center point, and not the top-left point.

If you have two rectangles intersecting, and you know the center point of both of them, and the width and height outward from the center point, you can simply check if x+width of A reaches beyond x-width of B to check if right side of A goes over the left side of B.

You do the same for all the sides: (size being the distance from centre to any side)

Check if x+size of A reaches beyond x-size of B to check if right side of A goes over the left side of B.
if Ax+size > Bx-size: Aright hit Bleft.

Check if x-size of A reaches beyond x+size of B to check if left side of A goes over the right side of B.
if Ax-size > Bx+size: Aleft hit Bright.

Check if y+size of A reaches beyond y-size of B to check if bottom side of A goes over the top side of B.
if Ay+size > By-size: Abottom hit Btop.

Check if y-size of A reaches beyond y+size of B to check if top side of A goes over the bottom side of B.
if Ay-size > By+size: Atop hit Bbottom.

You should try to visualize this using two cubes or rectangular shapes in the real world, e.g. with some cards. There are only four ways for the rectangles to collide, left to right, right to left, top to bottom, bottom to top. (that is if they don't rotate)
appel

JGO Wizard

Medals: 80
Projects: 4

I always win!

 « Reply #19 - Posted 2011-03-29 09:50:45 »

btw. you need to ensure that your game uses a fixed step delta.

If the objects were to move using delta 3, collision is detected and direction is reversed.
Then the objects were updated to move using delta 1, then you would again have a collision and you would reverse direction again!

If the delta is always 1, or 3, then you would not encounter this.

But there are other ways to solve this as well.
CyanPrime
 « Reply #20 - Posted 2011-03-29 21:51:07 »

Back to The Future Reflection!

I asked a question on stackoverflow, and I got this for a answer: http://stackoverflow.com/questions/5476384/how-do-i-calculate-a-normal/5476665#5476665

Now, I am just lost, so the first thing I am going to ask is what all do I need to keep track of for the
Wall, and in what format, in order reflect things off it?
CyanPrime
 « Reply #21 - Posted 2011-03-29 22:34:18 »

After talking to MatthiasM on IRC I got to this point, but it doesn't seem to work The ball goes right through the paddle.

 1  2  3  4  5  6  7  8  9  10 if(velo.y > 0){         float t = position.distance(paddle.position)/ velo.y;         t -= size/2;                  if(t <= 1.0){            if(velo.x * t >= paddle.position.x && velo.x * t <= paddle.position.x + paddle.width){               velo.y = -velo.y;            }         }      }

Correct code:

 1  2  3  4  5  6  7  8  9  10  11 if(velo.y > 0){         float t = ((position.y - radius) - paddle.position.y)/ velo.y;                  float ballHitX = position.x + velo.x * t;                  if(t <= 1.0){            if(ballHitX >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){               velo.y = -velo.y;            }         }      }

CyanPrime
 « Reply #22 - Posted 2011-03-30 01:34:03 »

ARG! Why isn't my top collision working right? (the ball goes up when under the paddle, and down when not)

 1  2  3  4  5  6  7 if(velo.y < 0){            float t = ((position.y - radius) - (wall[2].y + wall[2].height))/ velo.y;                        if(t <= 1.0){            velo.y = -velo.y;            }         }
CyanPrime
 « Reply #23 - Posted 2011-03-30 03:27:15 »

Fixed it!
 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 if(velo.y < 0){            float t = ((position.y - radius) - (wall[2].y + wall[2].height))/ velo.y;                        //float ballHitX = position.x + velo.x * t;                        if(t >= 1.0){               //if(ballHitX >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){                  velo.y = -velo.y;               //}            }         }      //}            if(velo.y > 0){         float t = ((position.y + radius) - paddle.position.y)/ velo.y;                  float ballHitX = position.x + velo.x * t;                  if(t >= 1.0){            if(ballHitX >= paddle.position.x && ballHitX <= paddle.position.x + paddle.width){               velo.y = -velo.y;               System.out.println("hit paddle");            }         }      }
CyanPrime
 « Reply #24 - Posted 2011-03-30 07:47:50 »

Can someone tell me whats wrong with my collision? After I added in the enemy parts it just went haywhire.

CyanPrime
 « Reply #25 - Posted 2011-03-30 19:54:36 »

 1 if(Math.abs(t) >= 1.0){

to

 1 if(Math.abs(t) <= 1.0){
Captain Awesome

Junior Devvie

Medals: 2

Hi

 « Reply #26 - Posted 2011-03-30 20:13:16 »

 1 if(Math.abs(t) >= 1.0){

to

 1 if(Math.abs(t) <= 1.0){

+rep for solving your own problem
Pages: [1]
 ignore  |  Print

 EgonOlsen (47 views) 2018-06-10 19:43:48 EgonOlsen (29 views) 2018-06-10 19:43:44 EgonOlsen (49 views) 2018-06-10 19:43:20 DesertCoockie (214 views) 2018-05-13 18:23:11 nelsongames (130 views) 2018-04-24 18:15:36 nelsongames (129 views) 2018-04-24 18:14:32 ivj94 (870 views) 2018-03-24 14:47:39 ivj94 (131 views) 2018-03-24 14:46:31 ivj94 (782 views) 2018-03-24 14:43:53 Solater (146 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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