# Use Rotation of face normal for Zaxis of TorqueBalence

ok here is my file

http://www.pasteall.org/blend/25355

it’s a little big due to models+textures etc.

So, I am using rays to get hit normal to apply gravity,

I also need to get the alignment of the face and pass it using

Center[‘Zaxis’] = Vector

To another script to balance more then 1 limb with the new Z axis

Right now, the Z gravity works, but I have something wrong in the script text.004 (great name huh?):spin:

``````
import bge
from mathutils import Vector
from bge import render

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()

own = cont.owner
Center=scene.objects['Center']
sen = cont.sensors['Ray']

hitNormal = sen.hitNormal
if hitNormal: # check before if the ray hit something
render.drawLine(sen.hitPosition,own.worldPosition,(255,0,0))
hitNormal = Vector(hitNormal) # transform in to Vector
axisZ = own.localOrientation.col[2]
v1 = axisZ
v2 = hitNormal
rotDif = Vector(v1.rotation_difference(v2).to_euler())
Center['ZaxisX']=(Center['ZaxisX']+axisZ.x)/2 #wrong?
Center['ZaxisY']=(Center['ZaxisY']+axisZ.y)/2 #wrong?
Center['ZaxisZ']=(Center['ZaxisZ']+axisZ.z)/2 #wrong?
rot = own.worldAngularVelocity
rot += rotDif *.5 #"elasticity"
rot *= 0 # inertia

own.applyForce(-hitNormal*9.8*own.mass*6)#gravity to the normal

``````

So, Can someone take a look at it,

I want the player to run on the surface, regardless of it’s orientation.

here is the balance code that uses the Center[‘Zaxis’]
GamelogicSimple.py

``````
import bge
cont = bge.logic.getCurrentController()
from mathutils import Vector
own = cont.owner
scene = bge.logic.getCurrentScene()
Center=scene.objects['Center']
axisZ = own.worldPosition.copy()
axisZ.x= Center['ZaxisX']
axisZ.y= Center['ZaxisY']
axisZ.z= Center['ZaxisZ']

quat = axisZ.rotation_difference(Vector((0,0,1)))
vec = Vector(quat.to_euler())
vecmag = vec.magnitude
torque = own['Force']*1

own.applyTorque(torque*vec*vecmag,0)

``````

How do I convert a string to a rot?

I think this is my only issue ATM

I have a rot

rotDiff

that I need to pass to from text.004(normal gravity)

into Center[‘Zaxis’]

and then I need to use it in

GameLogicSimple

As Zaxis,

But I am having trouble with my brain leaking out my ear.

Ok I am silly,

how do I convert a Vector to a Quat?

I have a vector rotDiff, that I need to get a Quat from, I believe.

I am bad at this vector-> quat and

Quat -> vector stuff

I have been looking at equations for doing this, and I just don’t get it:spin:

you cannot re-use rotDif for other obj.
rotDif is valid only for “own”

you can instead pass the hitNormal to the head.

and when head know the hitNormal , he can align to it

I have been trying, and have tried just using the “center” to apply gravity / orientation and not the feet,

However I can’t seem to get it to work

I am having issues still with getting vectors vs rotations

``````
import bge
cont = bge.logic.getCurrentController()
from mathutils import Vector
own = cont.owner
scene = bge.logic.getCurrentScene()
Center=scene.objects['Center']
axisZ = own.worldPosition.copy()
axisZ.x= Center['ZaxisX']
axisZ.y= Center['ZaxisY']
axisZ.z= Center['ZaxisZ']

if own.name=="Center":
quat = axisZ.rotation_difference(Vector((0,0,1)))
else:
quat = Center.localOrientation.col[2] ## throws error
vec = Vector(quat.to_euler())
vecmag = vec.magnitude
torque = own['Force']*1

own.applyTorque(torque*vec*vecmag,1)

``````

Basically, Align center with hit normal, attempt to align all else with center, but it’s not right

Here is what I have, (it does not work at all )

Stripped out textures to make easier downloads