Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Dot product and 2D projections.

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to simply bounce a 2D ball off a surface. It's very frustrating. I'm bouncing off the walls trying to figure it out! (yes, a bad joke :-) I have a vector, v1, that represents the wall. I have a circle with a point at x,y. I need to establish a perpendicular line to the wall so i can A) collision detect and find my distance to the wall, and B) find what my exit angle for bouncing should be. So what i do is create a second vector from the tail of the first vector to the x,y point of the circle: (black line = v1 aka "wall", blue line = v2, green = perpendicular line to wall) If i calculate the dot product between the two vectors, for this visual example, the result should be the cosine of the angle, or the projection of v2 onto v1, correct? For the above example, let's say i get a DP of 0.5. Does this mean that the perpendicular line intersects vector v1 at exactly half it's length? If so, consider this example: Wall "v1": (5,0) to (5,10) Circle (currently touching the wall): @ (6,5) radius of 1 So the ball is half way up the wall, 1 unit away from the wall (the radius of the ball, that is, it is touching the wall) to "vectorize" the origin-destination form (sorry, not brushed up on my math lingo), we get: v1x = 5 - 5 = 0 v1y = 10 - 0 = 10 v2x = 6 - 5 = 1 v2y = 6 - 0 = 6 now we normalize them: v1x_norm = 0 / (doesn't matter) = 0 v1y_norm = 10 / sqrt( 0^2 + 10^2 ) = 1 v2x_norm = 1 / sqrt( 1^2 + 6^2 ) = 0.16439 v2y_norm = 6 / sqrt( 1^2 + 6^2 ) = 0.98639 Now we get the DP: (0 * 0.16439) + (1 * 0.98639) = 0.98639 So, if the dot product is the projection of vector v2 onto v1, and v2 falls half way up v1, why am i getting a DP of 0.98639 for this vertical line ??? any help appreciated. I've checked it over a million times and i just need a nother pair of eyes for this. (hopefully attached to a brain as well) The goal of all this by the way, is to find the point on the wall at which the ball makes contact. That way i can spin the angle around to find the exit bounce angle. But i need the exact x,y point on the wall first of course. Thanks so much. [edited by - leiavoia on April 6, 2004 2:26:13 AM]

Share this post

Link to post
Share on other sites
I'm not quite sure what your doing so I'll throw out a guess here. If your looking for the angle of incidence.

theta = arccos[ (A.B)/|A||B| ]

but it sounds to me like you want to know the length of the V2 when projected onto V1. When I thought about this problem I only normalized the wall vector such that

V1 = (0,1), V2 = (1,5)
V1.V2 = 5

which is the length of V2 projected onto V1.

This actually gives the projection of V2 onto the line along V1 rather than a line segment. Of course you also get the same answer for the line segment (0,0) ,(0,10) and the segment (0,1),(0,12).

To generalize this problem I would translate the line segment V1 so that one point was at 0,0 then translate V2 by the same amount. You could then project V2 onto V1 and perform the reverse translation to get point of projection in the original coordinate system.

Hope this helps some,


[edited by - vTheHero on April 6, 2004 2:58:56 AM]

Share this post

Link to post
Share on other sites
yeah, don;t normalise v2.

if you normalise v2, and dot product the vectors, what you will get is the cos of the angle between the two (cos(angle) = A.B/|A||B|, cos(angle) = A.B/1 = A.B).

say you have

| .
| .
| .
| P
+ B

[x] are vectors, |x| are lengths

cos(a) = [AP].[AB] / |AP||AB|

|AQ| = |AP| * cos(a)
,[AQ] = |AQ| * ([AB] / |AB|)

[AQ] = ([AP].[AB] / |AB|) * ([AB] / |AB|)
[AQ] = ([AP].[AB] / |AB|2) * [AB]

and [AB].[AB] = |AB|2

[AQ] = ([AP].[AB] / [AB].[AB]) * [AB]

[AQ] = t * [AB],
t = ([AP].[AB] / [AB].[AB])

and to finish it off

[QP] = [QA] + [AP]
[QP] = [AP] - ([AB] * t)

[edited by - oliii on April 6, 2004 5:55:38 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!