Hint: copy the code below to a file (e.g.: bgeQuickRef.py) and save in the template folder (…\2.60a\scripts emplates</i>).
All examples are entirely drawn from a number of working and tested scripts of mine.
Corrections and comments are welcome.
### bgeQuickRef for blender 2.60a - version: 12 nov 2011 ###
''' Change Log:
10 nov 2011:
added Keyboard and Joystick inputs
12 nov 2011:
keyboard inputs testing logic modified
mappings for hat button DnLeft/ DnRight swapped
improved readability
'''
import bge #, GameKeys # bge.logic is equivalent to GameLogic
from math import pi, degrees, radians, sin, cos, tan#, asin, acos, atan, atan2, sqrt
from mathutils import Matrix, Vector
#from numpy import array, matrix
#import numpy as np
'''Game Objects acquisition:'''
# objs = bge.logic.getCurrentScene().objects
# obj = objs['Cube']
# print(objs) # list all objects
#========================
'''Getting & testing Keyboard Inputs:'''
# sens = obj.sensors['SensorName']
# if sens.positive:
# code for True
# else:
# code for False
'''Getting status of an Always Sensor named Reset:'''
''' deprecated method:'''
# reset = obj.sensors['Reset'].getKeyStatus(134)# where 134 = ASCII for "del" key
''' preferred method: (needs importing GameKeys)'''
#reset = obj.sensors['Reset'].getKeyStatus(GameKeys.DELKEY)
'''...in both cases:'''
# if reset == bge.logic.KX_INPUT_JUST_ACTIVATED:
# code for reset
'''Sensors states:
bge.logic.KX_SENSOR_INACTIVE = 0
bge.logic.KX_SENSOR_JUST_ACTIVATED = 1
bge.logic.KX_SENSOR_ACTIVE = 2
bge.logic.KX_SENSOR_JUST_DEACTIVATED = 3'''
#========================
'''Getting Joystick inputs (may change with joystick model):'''
# StickAndThr = obj.sensors['PitchRollThr'] #Joystick sensor named 'PitchRollThr', Event Type = Axis
# StickButtons = obj.sensors['Buttons'] # Joystick sensor named 'Buttons', Event Type = Button
# StickHat = obj.sensors['Hat'] # Joystick sensor named 'Hat', Event Type = Hat
# [fiStick, tetStick, thrStick] = StickAndThr.axisValues
# triggerBtn = StickButtons.getButtonStatus(0) # Boolean (True/False)
# stripedBtn = StickButtons.getButtonStatus(1) # Boolean (True/False)
# lowBtn = StickButtons.getButtonStatus(2) # Boolean (True/False)
# topBtn = StickButtons.getButtonStatus(3) # Boolean (True/False)
# hatDir = StickHat.hatSingle # Integer (see below)
''' Mapping for Hat button:
- Center/Up/Right/Dn/Left ==> 0/1/2/4/8;
- UpRight/DnRight/DnLeft/UpLeft ==> 3/6/12/9;
'''
''' Scaling pitch/roll stick to +/-60 deg:'''
# scale = 60./32768
# StickX = -scale*fiStick # degrees (notice sign inversion)
# StickY = scale*tetStick # degrees
'''Scaling throttle input within range [0-1]:'''
# Throttle = (32768 - thrStick)/65536 # Throttle units (notice sign inversion)
#========================
''' Getting orientation Matrices & position Vectors:'''
# Mw = obj.worldOrientation
# Ml = obj.localOrientation
# vw = obj.worldPosition
# vl = obj.localPosition
# phi, tet, psi = obj.worldOrientation.to_euler() # get euler angles [rad]
#========================
''' Rotating & moving objects (angles in rad):'''
# Incremental:
# obj.applyRotation([p*dt, q*dt, r*dt], local = false)
# obj.applyMovement([Vx*dt, Vy*dt, Vz*dt], local = false)
# Absolute:
# obj.localOrientation = [phi, tet, psi]
# obj.worldOrientation = [phi, tet, psi]
# obj.localPosition = [NewX, NewY, NewZ]
# obj.worldPosition = [NewX, NewY, NewZ]
#========================
'''MATRIX & VECTORS'''
'''
WARNING: Matrix and vector multiplication are in reversed order, i.e.:
- M1*M2 returns M2*M1;
- M*v returns M'*v;
where M, M1, M2 are square matrices, M' is the transpose of M, and v is a vector
The product of a 3x3 matrix by a 3x2 matrix returns a 3x3 matrix (!!!).
If M represents a rotation matrix, then M*v returns v rotated counterclockwise
The orientation matrix of obj2 in the reference frame of obj1 is given by:
'''
# M = M1.transposed()*M2,where:
# M1 = obj1.worldOrientation
# M2 = obj2.worldOrientation
#========================
'''VARIABLE INITIALIZATION'''
''' - Method 1: assigning to a gameobject: '''
# var1 = obj['prop1'] # retrieve from previous iteration
# var1 = var1 + 1 # update
# obj['prop1'] = var1 # save for next iteration
''' - Method 2: define variables in an initialization script TO BE EXECUTED FIRST AND ONLY ONCE.
The script should be triggered by an always sensor put on top of the sensor list, and with both "level triggering" buttons disabled.
This latter method is more convenient when one or more initialization values are the result of calculations, rather than constants.'''
# Initialization script:
# import bge
# bge.var1 = InitVal
# bge.var2 = bge.var1/2
# ...
# Main script:
# import bge
# v1 = bge.var1 # retrieve stored variables
# v2 = bge.var2
# ... (process all variables as necessary)
# bge.var1 = v1 # save for next iteration
# bge.var2 = v2
#========================
'''Converting euler rates to local axis angular rates:'''
# direct formulas:
# p = phip - sin(tet)*psip
# q = cos(phi)*tetp + cos(tet)*sin(phi)*psip
# r = -sin(phi)*tetp + cos(tet)*cos(phi)*psip
# inverse formulas:
# phip = p + sin(phi)*tan(tet)*q + cos(phi)*tan(tet)*r
# tetp = cos(phi)*q - sin(phi)*r
# psip = sin(phi)/cos(tet)*q + cos(phi)/cos(tet)*r
#=======================
''' Timing software execution:'''
# from time import clock
# start = clock()
# ... (code)
# end = clock()
# print(end-start) # returns elapsed time in seconds (floating)
#=======================
''' Changing the sampling period (default=60 FPS): Properties => World'''
#=======================
mb