Firstly to connect a python script simple add a controller, set its type to python than its sub-type to script. Select the text file you wish to use (script only uses internal text files, module acts more as an import operation and can use external modules) than connect sensors to it. The script will be run on a single positive pulse from any sensor and will only send pulses to actuators if explicitly stated in the script (see agoose77’s post).
The math library provides access to many math functions such as:
math.degrees()
math.radians()
math.pi
math.asin()
math.acos()
math.atan()
which may be of use to you. Note that all trigonometric functions will assume radian parameters and return radian values.
Position and orientation data can be accessed by using the .worldPosition and .worldOrientation properties of a game object within a script such as:
import bge # top-level GE module
cont = bge.logic.getCurrentController() # the python controller logic brick
own = cont.owner # the object that "owns" the contorller
# getting the data
pos = own.worldPosition
ori = own.worldOrientation
# setting the data
own.worldPosition = [0,0,0]
own.worldOrientation = [[0,0,0], [0,0,0], [0,0,0]]
#alternatively, position data for each axis can be accessed
own.worldPosition.z = 0
own.worldPosition.x += 5
worldPosition returns a mathtuils.Vector object (3D) and worldOrientation returns a mathutils.Matrix object (3x3). These classes make it relatively easy to manipulate the position and rotation. For example, the vector class support vector addition and subtraction:
import bge
from mathutils import Vector
cont = bge.logic.getCurrentController()
pos = own.worldPosition
relative_pos = Vector([10,10,10]) - pos
The matrix class supports conversion from matrices to Euler angle lists as well as aligning vectors with a matrix:
import math
import bge
from mathutils import Vector
cont = bge.getCurrentController()
pos = own.worldPosition
ori = own.worldOrientation
# Move an object along a local axis
pos = pos + Vector([0,5,0]) * ori # move the object 5 units along its local +y axis
# Set an objects rotation:
eul = ori.to_euler() # euler angles are 'basic' angles, essentially easier to edit
eul.z = math.pi/2
own.worldOrientation = eul.to_matrix() # convert back to matrix and set
# Rotate an object
own.applyRotation([0,0,math.pi/2]) # Note: am unsure if radians or degrees are assumed
In all these example the “own” variable is a bge.types.KX_GameObject. For more information on the bge API visit the docs:
http://www.blender.org/documentation/250PythonDoc/