return face normal?

okay if i send a ray and hit a face how do i return the face normal, or rotation??

Check out rayCast() in Game object docs:

dosn’t help.

If you want to align an object to the ground, check this code out.
It’s adapted from andrew-101’s code to help you understand.

from mathutils import Vector, Matrix # Importing these modules helps with vector math
import bge

own = bge.logic.getCurrentController().owner
ori = own.orientation
pos = own.position

range =  5
ob_axis = 2
ray_axis = 2

mat = Matrix(ori[0], ori[1], ori[2])
vec = Vector([pos[0],pos[1],pos[2]]) + (Vector([0,0,-1]) * mat) 
ray = own.rayCast(vec, own, range, '')

if ray[2]:own.alignAxisToVect(ray[ray_axis], ob_axis, 1) 

okay a couple questions, sorry for so many questions, i’m trying to learn python and sometimes its confusing. a matrix is another type of angle right. when i run this it pops up a few errors.

one looks like this

   mat = Matrix(ori[0], ori[1], ori[2])

TypeError: mathutils.Matrix(): expects no args or 2-4 numeric sequences

what is ori[0] and so on even for? is that how matrix angles work??

This code is for 2.57
the [0] part of the code simple represents postitioning.
In pyton, we have many different types of data, one of which is a ‘list’
Lists simply store data, appending it like a shopping list.
Orientation is stored as a matrix, but the important part is that you can access it like a list.
Orientation matrices are 33 lists.
Because of this, there are three ‘items’ in the list, and three ‘sub-items’ for each item.
However, to access these from the list, you access their position.
The tricky part is that python starts enumerating at 0, not at 1, so the first item is actually at position 0.
Therefore, if this is how list data is accessed, you would be able to do the following to a 3
3 orientation matrix .

matrix = [[1.0,0.0,0.0][0.0,1.0,0.0][0.0,0.0,1.0]]
you can see that there are 3 main parenthesis [] and 3 values per parenthesis.
Anything in these square brackets is in list format, and so is treated as thus.
So, to access the first parenthesis, (remmeber python starts at 0) you would use :
matrix = [[1.0,0.0,0.0][0.0,1.0,0.0][0.0,0.0,1.0]]
this accesses the first item.
to get the first value in the first parenthesis, use

This is why we use ori[0],
it gets the globalX, Y, and z orientation by

It is failing i presume due to a different version of blender.

actually i’m using blender 2.57b so i don’t think its beacuse of a different version, however do i need to say something like

from mathutils import Matrix

actually i think i’m going to stop for now, i’ve gotten kinda burnt out of doing constat google searches for like two days now. i ended up figuring out part of my problem by downloading social’s template and looking at the bullet hole script, however thers still a lot of work to do. i’ll probably resume tommorrow.

You may chcek the tech part to collect more pro inforamtion abou this!

Just for explaination, this diagram shows you what you can get from a ray (also mouse over uses a ray):

you see the normal of the ground = hitNormal

Matrix is not a “type of angle”. A matrix is an equation system. The rotation matrix (in BGE orientation) is the transformation to rotate a vector around one or more axis (see wikipedia for transfromation matrix or rotation matrix).

If you want to apply the rotation of the matrix to a vector you multiply the transformation matrix with the vector. The result is the transformed vector:

m * v = v’

As this are quite complex operations you might better tell us what you want to achive. So we can provide a better help.


okay so i’ve gotten up to the point where i can make a ray using logic bricks and get the vector of the face and alighn my object to that. however, when i do some coding like this, which pretty much sends a ray from one point to another point (these points are objects)

def GroundCheck(size = 1.0, prop = ‘floor’):
cont = logic.getCurrentController()
obj = cont.owner
sce = logic.getCurrentScene()
objList = sce.objects

start = objList[“RayStart”]
end = objList[“RayEnd”]

return obj.rayCast(end, start, 0, prop, 0, 1)

however if i use that later on like this,

ray = GroundCheck(1)[0]

if GroundCheck(1)[0] != None:
posR = Vector(ray.hitPosition)
normalR = Vector(ray.hitNormal)

if i have my ray equal as a sensor it works such as this

ray = cont.sensors[‘Ray’]

however it says something like this when i use ray = GroundCheck(1)[0]

posR = Vector(ray.hitPosition)
AttributeError: ‘KX_GameObject’ object has no attribute ‘hitPosition’

any way to get this to work??

p.s. by the way thanks for all the help so far.

When You do GroundCheck(1)[0] You get the hitobject, that have no hitPosition. Do:

(hitobject, posR, normalR) = GroundCheck(1)

And You get all the values in one go. If there is no hit they are all None.

What are you trying to acheive?
Here is my post which gets the normal and then goes on to reflect it onto a surface to create a “mirror”

okay thanks guys, i actually just shortcutted some stuff, but in the end i got it all working.

@agoose77 i’m working on a project to help me learn python. it is make the movement of a skateboarder on a halfpipe.