# Can anyone help with this vector maths to get normal?

After all the help i’ve had, here is my reflection script - shows the normal, and then the angle of incidence and angle of reflection. Only ‘bounces’ once

``````import bge
from mathutils import*
from math import*
from bge import render as Rasterizer
c= bge.logic.getCurrentController()
o = c.owner
range = 50
ob = o.position + (Vector([0,range,0])*o.orientation)

#first ray

ray = o.rayCast(ob,o, ob.magnitude, "", 0,0,1)

point = ray[1]
normal = ray[2]
start = o.position
color = [ 1.0, 0.0, 0.0]
Rasterizer.drawLine( start, point, color)

def get_reflection(v,n):
N=Vector(n).normalize()

V = Vector(v)
R = []

#print(v)
dotS = V[0]*N[0] + V[1]*N[1] + V[2]*N[2]
r1 = -2*dotS*N[0] + V[0]
r2 = -2*dotS*N[1] + V[1]
r3 = -2*dotS*N[2] + V[2]
R.append(r1)
R.append(r2)
R.append(r3)
return(Vector(R))

v = o.getVectTo(point)[1]
Rasterizer.drawLine(point,point+normal*3, [0.0,1.0,0.0])
end = get_reflection(v,normal)
end.magnitude = Vector(point - o.position).magnitude
print(end.magnitude,sqrt(v[0]**2+v[1]**2+v[2]**2))
last = point+end

Rasterizer.drawLine(point,last, color)

``````

Look at the reflect() method on vectors in mathutils.

In 2.49b reflect() is broken and returns incorrect values. In 2.57 it is fine.
You can normalize your vectors with normalize().

Just checked my game AI script. Didn’t find reflect() so that’s good. I seem to remember I couldn’t get it to work well.

``````import bge
from mathutils import*
from math import*
from bge import render as Rasterizer
c= bge.logic.getCurrentController()
o = c.owner
ob = bge.logic.getCurrentScene().objects["Empty"]

#first ray
ray = o.rayCast(o,ob, 203.0, "", 0,0,1)
point = ray[1]
normal = ray[2]
start = o.position
end = point
color = [ 1.0, 0.0, 0.0]
Rasterizer.drawLine( start, end, color)

v = o.getVectTo(point)
n = normal.normalize()

poi = v.reflect(n)

Rasterizer.drawLine(point,poi, color)

#what i am basically looking for is to take the data from the normal, and use that to reflect
``````

Second drawLine should be:

Rasterizer.drawLine(point, point+poi, color)

And possible You want to change length of poi with poi.magnitude(length) first.

WOW, thanks LAH!