x


physics & rotation question

how two perpendicular vectors can have a dot product not equal to 0 in Unity?

Thats my script for a simple Cube, that moves only in 2D:

void OnCollisionEnter(Collision collision)
{
        ContactPoint contact = collision.contacts[0];



        //Calculate reflection Vector3 on collision normal
        reflect_direction = Vector3.Reflect(rigidbody.velocity.normalized, contact.normal);
        impact_direction = rigidbody.velocity.normalized;
        angle = Vector3.Dot(reflect_direction, impact_direction);        
}

Now for example In my on screen display I see values as:

angle == 0,9852035

impact_direction == 0.1, 1.0, 0.0

reflect_direction == -0.1, 1.0, 0.0

How on earth is the dot product close to 1 !?!?!?!

Also what worries me is the fact, that for this example, the ship was flaying with big velocity in the OX axis and the impact_direction shows, that the OY value is bigger ?!?

Any help?

more ▼

asked Nov 11 '10 at 04:50 PM

Jack 4 gravatar image

Jack 4
3 1 1 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, those vectors are not perpendicular, they are reflected along the X axis.

And their dot product is:

(-0.1*0.1)+(1.0*1.0)+(0.0*0.0) = (-0.01)+(1.0) = 0.99

So to get the angle, you'd have to divide the dot product by the product of the magnitudes of the two vectors. Ex:

angle /= (Vector3.Magnitude(reflect_direction) * Vector3.Magnitude(impact_direction));

Edit: Woops, don't forget to take arc-cosine of the above result to get the angle!

Or, you can just use the Vector3::Angle function:

angle = Vector3.Angle(reflect_direction, impact_direction);
more ▼

answered Nov 11 '10 at 05:13 PM

VS48 gravatar image

VS48
550 3 4 15

So stupid of me! Ofcourse they're not perpendicular. What was I thinking :/ Thanks a lot!

Nov 11 '10 at 07:23 PM Jack 4
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2174
x1882
x289
x212
x5

asked: Nov 11 '10 at 04:50 PM

Seen: 1154 times

Last Updated: Nov 11 '10 at 04:50 PM