Which way to bounce?
Okay, in the engine for my upcoming game I''m doing something a little more complicated but I will explain what I need in to know in simple terms. Lets say in 2d a ball bounces around the screen. That''s good and all, but then I want to add a brick to the center. Now, the question I have is how do you figure out whether to reverse the x velocity or the y velocity? The variables I have are previous_x, previous_y, current_x, current_y, x_velocity, and y_velocity, as well as the coordinates of the block. It''s easy to figure out which way to bounce normally, but when it starts getting closer to the corner all the solutions I seem to come up with fail!
Masters Software
Try moving the ball in 2 steps. First horizontal and then vertical. This can be totally transparent to the player.
Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
Sludge Softwarewww.sludgesoft.comDeveloping a secret of mana style role-playing-game
Use vectors & let the math decide.
-Magmai Kai Holmlor
I am brazen and fear no flames.
(So long as I don't lose this Ring of Fire Protection +5)
-Magmai Kai Holmlor
I am brazen and fear no flames.
(So long as I don't lose this Ring of Fire Protection +5)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Well, I have been trying to use math to figure it out. However, this still doesn't always seem to work. I'll show you an example of some of my code if you want. x,y are the top left coordinates of the block, the block is 32x32:
Basically, I'm letting the ball bounce around and if there is a collision then I check the vector to find out where the collision point would be at the same x-coordinate and the same y-coordinate as the bottom right of the block. So, if the x point of collision would be less than the x coordinate of the bottom right of the block, then I have to bounce it vertically. However, doing this technique for all 4 corners (with the obvious > and < changes) often allows the ball to cut corners. I know this is a bit slow coding wise but once I get it working I will optimize it. Thanks all for your responses and I hope someone can figure out what is wrong with this code.
Edited by - jtecin on July 8, 2000 8:07:21 PM
//We have already detected a collision between the block and the ball, //and we know that previously the ball was below and to the right of the block.//Let's get a slope/intercept equation first. . .//Step one is to find the y-intercept//This means converting it from y=mx+b to b=y-mxy_intercept = SpriteGuy.PreviousY - (SpriteGuy.y_vel / SpriteGuy.x_vel)*SpriteGuy.PreviousX; //Now let's find the x collision point plugging in the y//y = mx+b goes to x = (y-b)/mx_collision = (y+32-y_intercept)/(SpriteGuy.y_vel/SpriteGuy.x_vel);//Now alas we find the y collision point plugging in the x coordinatey_collision = (SpriteGuy.y_vel/SpriteGuy.x_vel)*(x+32)+y_intercept;if (y_collision < (y+32)) { SpriteGuy.x_vel = -SpriteGuy.x_vel*.75; SpriteGuy.WorldPosX = x+34; }if (x_collision < (x+32)) { SpriteGuy.y_vel = -SpriteGuy.y_vel*.75; SpriteGuy.WorldPosY = y+34; }
Basically, I'm letting the ball bounce around and if there is a collision then I check the vector to find out where the collision point would be at the same x-coordinate and the same y-coordinate as the bottom right of the block. So, if the x point of collision would be less than the x coordinate of the bottom right of the block, then I have to bounce it vertically. However, doing this technique for all 4 corners (with the obvious > and < changes) often allows the ball to cut corners. I know this is a bit slow coding wise but once I get it working I will optimize it. Thanks all for your responses and I hope someone can figure out what is wrong with this code.
Edited by - jtecin on July 8, 2000 8:07:21 PM
to do a perfect bounce use this
new velocity = (vel - (N*(vel DOT (N))) * 2;
N is the normal of the plane youre bouncing against
new velocity = (vel - (N*(vel DOT (N))) * 2;
N is the normal of the plane youre bouncing against
Two questions:
1)What does DOT stand for?
2)I don''t understand how this equation would work for my problem. It seems like a good way to find the new velocity but I still don''t know whether to change the y velocity or the x velocity.
1)What does DOT stand for?
2)I don''t understand how this equation would work for my problem. It seems like a good way to find the new velocity but I still don''t know whether to change the y velocity or the x velocity.
1) DOT stands for dot product. For 2D, it''s v1.x*v2.x + v1.y * v2.y, where v1 is the first operand and v2 is the second operand (both vectors.)
2) You don''t have to change one velocity or the other! It calculates a vector based on vector math.
Here''s the equation again:
new velocity = (vel - (N*(vel DOT (N))) * 2;
N is the normal of the wall it bounces off of. If the wall was facing left, it will be < -1, 0 >. If it''s facing down, it would be < 0, 1 >. You get the idea.
Anyway, say that you have calculated this normal (how you do this could be as simple as a switch() statement) and it is represented by n_x and n_y...
x_velocity = (x_velocity - (n_x*(x_velocity*n_x + y_velocity*n_y)))*2;
y_velocity = (y_velocity - (n_y*(x_velocity*n_x + y_velocity*n_y)))*2;
That should do it, right?
lntakitopi@aol.com - http://www.geocities.com/guanajam
2) You don''t have to change one velocity or the other! It calculates a vector based on vector math.
Here''s the equation again:
new velocity = (vel - (N*(vel DOT (N))) * 2;
N is the normal of the wall it bounces off of. If the wall was facing left, it will be < -1, 0 >. If it''s facing down, it would be < 0, 1 >. You get the idea.
Anyway, say that you have calculated this normal (how you do this could be as simple as a switch() statement) and it is represented by n_x and n_y...
x_velocity = (x_velocity - (n_x*(x_velocity*n_x + y_velocity*n_y)))*2;
y_velocity = (y_velocity - (n_y*(x_velocity*n_x + y_velocity*n_y)))*2;
That should do it, right?
lntakitopi@aol.com - http://www.geocities.com/guanajam
Well, I am starting to feel extremely stupid, but I still do not see how this would work for what I''m trying to do. This equation depends on finding the normal of the brick which is in the center of the screen. However, the difficult part is finding out which side of this block the ball hits, and doesn''t this determine which side of the brlock I would find the normal for (i.e. which way it is facing). And what I don''t understand about the last two equations is that if the wall is facing down, then the new x velocity will just be 2 times as much as the old x velocity and the y velocity will be 0. As you can see I am totally lost. Can anybody maybe give me an example of how to find the new x and y velocities of the ball if I have the velocities for the ball and I know that it hit the block. Once again, I know there is a collision, but I don''t know which side of the block the ball hit.
Try this:
This way it tests collision detection _before_ the ball hits the block. Take 2 blocksizes when the block isn''t square.
I hope it wraps around...
Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
unsigned char collide(short ballx ,short bally, short blockx,short blocky, unsigned short ballsize, unsigned short blocksize){ //collisions: //0: none //1: up side //2: down side //3: left side //4: right side if (ballx+ballsize==blockx) return(3); if (bally+ballsize==blocky) return(1); if (ballx==blockx+blocksize) return(4); if (bally==blocky+blocksize) return(2); return(0);};
This way it tests collision detection _before_ the ball hits the block. Take 2 blocksizes when the block isn''t square.
I hope it wraps around...
Sludge Software
www.sludgesoft.com
Developing a secret of mana style role-playing-game
Sludge Softwarewww.sludgesoft.comDeveloping a secret of mana style role-playing-game
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement