vec_bounce (VECTOR* vRay,VECTOR* vNormal)

Calculates a reflection vector from a ray and a surface normal.

Parameters:

vRay - Speed vector, to be reflected.
vNormal - Normal of the reflecting surface.

Returns:

vRay

Modifies:

vRay

Speed:

Fast

Algorithm:

vRay = vRay + 2*dot(vNormal,-vRay)*vNormal

Example:

action bouncer() // moving object that bounces off all obstacles
{
  var vSpeed[3];
  while(1)
  {
    vec_set(vSpeed,vector(5*time_step,0,0));
    vec_rotate(vSpeed,my.pan);
    c_move(me,nullvector,vSpeed,0); // move ahead..
    if (trace_hit) {                // if an obstacle was hit..
      vec_bounce(vSpeed,hit.nx);
      vec_to_angle(my.pan,vSpeed);  // set the new direction
    } 
    wait(1);
  }
}

See also:

Vectors, vec_add, vec_set, vec_inverse, vec_diff, vec_fill ...
► latest version online