#### Archived

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

# 2D Angle of Incidence ?

## Recommended Posts

Hi guys, Is there an elegant way to compute the 2D angle of incidence (I think that''s what it''s called) to a value between 0.0 and 1.0 ? I have two vectors, the normal and the incoming direction, and would like to get a value between 0.0 and 1.0 depending on the ''directness'' of the incoming direction, so if an incoming direction is the opposite of the normal (heading straight towards it) the value would be 1.0, and if the angle is heading parallel it would be 0.0, and between those would be evaluate to 0.0...1.0, so if the normal were facing right and the incoming direction was up-left it would be 0.5. I''m using this for sound volume, so if an object smashes into a wall head-on it will be loud, and if it just grazes the wall it will be very low. It would be cool if I can use dot products or something so I can avoid sin() and cos() calls. Any ideas ? Thanks.

##### Share on other sites
smitty1276    560
Just calculate the angle between each vector and the (1, 0) vector (in terms of x and y). Divide the difference by 180.

If you know a 2d vector, you can easily compute the angle using the pythagorean theorum and the law of cosines.

##### Share on other sites
oliii    2196
it''s just a dot product. You just need to rescale it from [-1, 1] to [1, 0]

I usually have such a function always at hand. It''s a simple math tool, but handy.

//--------------------------------------------------------------// simulate a simple linear amplifier.//// Calculate the percentage of a value from an imput range, // and return an output value from within an output range//--------------------------------------------------------------float LinearAmp(float t, float in0, float in1, float out0, float out1){    //------------------------------------------    // Make sure we are sorted    //------------------------------------------    if (in1 < in0)        return LinearAmp(t, in1, in0, out1, out0);    float din = (in1 - in0);    //------------------------------------------    // This is a gate amplifier. Infinite amplification,    // means it''s like an ON/OFF switch.    //------------------------------------------    if (din < 0.00001f)         return (t < in0)? out0 : out1;    //------------------------------------------    // check range limits    //------------------------------------------    if (t < in0)       return out0;    if (t > in1)       return out1;    //------------------------------------------    // calculate the interpolant (the percentage, if you will).    //------------------------------------------    float interpolant = (t - in0) / din;    //------------------------------------------    // ...and return a value from within the output range    //------------------------------------------    float output = out0 + interpolant * (out1 - out0);    return output;    }// normal of impact DOT velocity directionfloat dot = N.x * V.x + N.y * V.y + N.z * V.z; // map the dot product from [-1, 1] into the range [1, 0]float fIntensity = LinearAmp(dot, -1, 1, 1, 0);