Advertisement

bouncing off a wall

Started by August 04, 2002 11:10 PM
7 comments, last by evilclown 22 years, 6 months ago
I can bounce the golf ball off a wall thats horizontal or vertical. a 45 degree one I am having trouble with. I''m getting a seg fault on the last line that says ball.vector.x = .... If it''s because I''m assigning a too high of number to my float, why didn''t my typecasting take care of it? Is there a better way to do this? Maybe even a function I could write where it would deal with any angle wall? (I tried working it out on paper, but im not sure exactly why i got the (1/hypotenuse). Even without that it still has a segmentation fault. Here''s what I have.
  
//collision so bounce


            {  //cause im under a case

              double deg;
              double newdeg;
              float hyp = ball.vector.Magnitude();
              deg = radtodeg(atan2(ball.vector.y, ball.vector.x)) + 90; //get degree of vector,

                                                  //add 90 because computers origin is the top left

                                                  //this might be wrong

              cout << "x " << ball.vector.x << " y " << ball.vector.y << endl;
              cout << deg;

              newdeg = (45 - deg) + 45;  //get new degree

              ball.vector.x = (float)degtorad((1/hyp)*(acos(degtorad(newdeg)))); //rotate vector


              //these also seg faulted:


              //ball.vector.x = (float)((1/hyp)*(acos(degtorad(newdeg))));

              //ball.vector.y = (float)((1/hyp)*(asin(degtorad(newdeg))));

            }
  
L = incident vector (oriented away from surface)
R = reflected vector (oriented away from surface)
N = surface normal (unit vector)

L+R = 2N (N.L)

That''s all you need.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
what is (N.L)?

So I just do

NewBallVector = 2*WallVector - BallVector?

And if the wall was a diagnol in a square, starting at the top left to the bottom right, would the vector be x = 1, y = -1?(Then normalize it?) Would this still work if the ball came at the wall from the other side?

Then if the wall was verticle, the vector would be x = 1, y = 0?
(N.L) is a vector operation that computes the dot product of two vectors. If you have v1 = (x1 y1 z1) and v2 = (x2 y2 z2), then (v1.v2) = (x1*x2 + y1*y2 + z1*z2). Note that the dot products gives a scalar result and NOT a vector.

The surface normal is a unit vector thats orthogonal (perpendicular) to the point of contact. If your wall is from (0,0) to (1,1) then the surface normal for the entire line is (-1, 1). Actually its (-sqrt(2)/2, sqrt(2)/2) after normalization. You will need to adjust this normal depending on the direction you are approaching the wall from so that the normal points towards you.

Read up on vectors. Once you understand them, they are a lot easier and more powerful than using angles like you are doing now.
I myself am still working on a more complete understanding of vector math and I''ve been looking for a formula like this for a while. I noticed something interesting when testing it out, the result vector is incorrect if you have a vertical surface normal and a vertical incident (I believe that''s what its called) vector or both of them are horizontal. Actually there seem to be about four cases:

L = {1,0}
N = {-1,0}
R = {1,0} ???What just happened here

L = {-1,0}
N = {1,0}
R = {-1,0} ???

L = {0,1}
N = {0,-1}
R = {0,1} ???

L = {0,-1}
N = {0,1}
R = {0,-1} ???

Does anyone know why this is and/or can explain the reasoning behind the equation?

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
quote:
I noticed something interesting when testing it out, the result vector is incorrect if you have a vertical surface normal and a vertical incident (I believe that's what its called) vector or both of them are horizontal. Actually there seem to be about four cases:

L = {1,0}
N = {-1,0}
R = {1,0} ???What just happened here

L = {-1,0}
N = {1,0}
R = {-1,0} ???

L = {0,1}
N = {0,-1}
R = {0,1} ???

L = {0,-1}
N = {0,1}
R = {0,-1} ???

Does anyone know why this is and/or can explain the reasoning behind the equation?


In the equation L+R = 2N(L.R) all of the vectors must be oriented away from the surface. What you are doing is just using the velocity vector for the incident vector. You must reverse it (scale by -1) for the equation to work properly.

Therefore if you want to simulate a ball coming straight down towards a vertical wall, the vectors would be:

L = {0,1,0}
N = {0,1,0}

To solve:
{0,1,0)+R = 2{0,1,0}({0,1,0}.R)

And you will get that R = {0,1,0}

[edited by - Neosmyle on August 5, 2002 5:31:54 PM]
Advertisement
If the case is always using a 45 degree angle to bounce off of then why don''t you just swap the x and y velocities?
Oops, I forgot, not only swap but you will also have to do some negation, but otherwise swapping and negating will work just fine with any 45 degree slope.
I'm not entirely sure what you were trying to tell me to do, but the example you posted suffers from the same problem that I was talking about.

[EDIT] sorry I missed the last couple of posts while pondering your answer Neo...it just turns out that I did a horrible job of visualizing the other vectors I tested

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________



[edited by - Thunder_Hawk on August 5, 2002 6:10:29 PM]
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement