![]() |
|
Change angle of ball when it bounces off the paddle... |
Greg Lockwood
Member #3,491
May 2003
|
G'day! I'm a HSC student doing an Arkanoid/Breakout-style ball-and-paddle-and-bricks game. Pretty simple. However, on all of the other games of this genre I've played, the angle that the ball is reflected at after hitting the paddle changes so that it is more flat if it hits at the edges of the paddle and more vertical (pref. almost vertical) when the ball hits near the middle. Here is what I mean in ASCII art: How do I do this? I would prefer that the ball doesn't appear to change speed, just the angle (i.e. as a vector, the angle changes, but the magnitude is the same). I have got seperate ball x and y speeds, of course. Any hints gratefully received. Thanks in advance! |
gillius
Member #119
April 2000
|
If you are bouncing off a paddle, why can't you simply reverse the direction of the x or y velocity vector? If you have dx and dy, when you hit the paddle, just flip the appropriate sign. Gillius |
Yellofish Software
Member #470
June 2000
|
I think I know what you mean. I made a similar game, and looking at arkanoid (the single greatest paddle-game ever) when the ball hits the middle of the paddle it's angle is simply reflected, but if it hits the edges it bounces off at a much sharper angle. I tried this in two ways, pick which one works best for you. 1) A look up table of modifiers. make up a zone in the middle that's "flat" and zones on the edges with increasingly "sharp" angles, and just put multipliers into a table for that. In this case it's sometimes best to just do cases, if the ball hits the sides always reflect at a certain angle, in the middle reflect in a "normal" way. 2) The cooler (in my opinion) and more general way. Use a sine and cosine, and multiply the produced angle by that value. Basically what you're looking for here is a new angle to shoot the ball in no matter where it's coming from, so I would probably just treat the result of the sine and cosine as a new unit vector pointing in the direction the ball "should" go and multiply in the speed of the ball. If you want to make sure that the ball's current angle is always taken into account then I would reccomend using a dot-product between the inverse of the unit-vector produced by the sine/cosine and the ball's current angle. warning using a dot-product will sometimes cause the ball to shoot striaght downward in the right circumstances if you're not careful with the above-mentioned method. Now, suppose you want to use the cosine/sine method for some smooth transitions and less work, but more thinking. Alright, how long is the paddle? let's say wid. ok. now, where are we? Now, we know that cos(pi) = 1, which would be the right vector for the middle of the paddle. We also need to know the position of the ball on the paddle relative to the left-hand side of the paddle (x=0), we'll call that pos. new y vector = cos(pi*((pos-wid/2)/(wid/2))); the x vector is similar. Now, my math may be off as it is late here, so I'll explain what you're going for as an input to sine and cosine. You want to make a range like so: 0 *****pi***** 2*pi that's right, 0 on the far right, 2*pi on the far left, that means you have a half circle radiating from the paddle. Now, this may not be quite what you want so fiddle with the extreme ranges, like, say .2 on the left and 2*pi-.2 on the right, but you get the idea. I would reccomend combining the two cases, though, and making sure that an area in the middle always reflects the ball perfectly so that the players don't get too frustrated. Enjoy Life |
Johan Halmén
Member #1,550
September 2001
|
I'm working on a game where I have a ball bouncing off different objects. I use vectors for the ball velocity and for the forces involved (gravity and tilted playground). I have my own vector class for dealing with vectors, including adding, subtracting, multiplying, dot and cross products. My last addition to the class was a method that divided a vector into two component vectors in right angle to each other. When the ball hits another ball (or any obstacle), I compute the coordinates of the touching point and define a vector with the direction of the tangent at that point. Then I separate the velocity vector into one tangent vector and one normal vector (meaning a right angled vector to the tangent). Then I negate the normal vector and add it to the tangent vector to get the new velocity vector. See below. v1 is the original velocity. When the ball hits the obstacle, I define the tangent (green line). I separate (what's the right verb, guys?) v1 into v3 and v2. Then I get v4 by simply reversing v2. If the speed is to be remained, v4 should be of same length as v2. Then I add v3 and v4 to get v5, which is the new velocity. In your case you just have to define how the green line changes along the paddle. If the value of the hit is x and -1 < x < 1, I would do something like: Vector tangent(2, x); v1.Separate(&v3, &v2, tangent); v2 = v2 * -1; v1 = v3 + v2;
First line defines a vector with the direction of the imagined wall that my ball hits. I guess there are standard libraries for vectors, but I don't know them. I belong to the WIS (Wheel Inventors' Society). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
Thomas Harte
Member #33
April 2000
![]() |
First of all, most breakout clones do not keep a constant ball speed. When the ball leaves at a a sharp angle it travels much more quickly than when it bounces more or less vertically. Indeed horizontal speed is usually completely decoupled from vertical. Indeed the common solution is this: vy = -vy; If you really want to keep a common speed then obviously follow any of the other pieces of advice posted before this. [My site] [Tetrominoes] |
Johan Halmén
Member #1,550
September 2001
|
I believe the reason why most breakout games have a constant y-speed is the simple programming (vy = -vy every time direction changes). This is how they worked in the 70's, when they appeared as b/w slot games. I guess no one even thought of doing vector or trigonometry maths by machine code without even a simple assembler. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
Trezker
Member #1,739
December 2001
![]() |
Well, how much harder is it to accelerate the ball? vy=vy*-1.001; I think a breakout game would be boring if the ball didn't accelerate. |
|