#bullethole.py
#
# object properties
# type name value use
# Int ebhole 0 keep track of if I need to position a bullet hole still
#
# random note about implementation:
# the gameblender python api give me access to added objects the frame after I trigger the actuator
# so I have to react accordingly.
from math import sin, cos, sqrt
# vector functions!!
def VEC_length(x):
return sqrt(x[0]*x[0]+x[1]*x[1]+x[2]*x[2])
def VEC_normalize(x):
length = VEC_length(x)
return [x[0]/length,x[1]/length,x[2]/length]
def VEC_cross(x, y):
return [x[1]*y[2] - x[2]*y[1],
x[2]*y[0] - x[0]*y[2],
x[0]*y[1] - x[1]*y[0]]
def VEC_min(x, y):
return [x[0] - y[0], x[1] - y[1], x[2] - y[2]]
def MAT_trackvector(fw, y):
if abs(abs(fw[2]) - abs(y[2])) < .001: #prevent gimbol lock
y.append(y[0])
del y[0]
right = VEC_normalize(VEC_cross(y, fw))
up = VEC_cross(fw, right)
return [[right[0], up[0], fw[0]],
[right[1], up[1], fw[1]],
[right[2], up[2], fw[2]]]
cont = GameLogic.getCurrentController()
obj = cont.getOwner()
ray_sensor = cont.getSensor("ray_bullet")
lmb_sensor = cont.getSensor("fire")
add_bullet_act = cont.getActuators()[0]
# position the last added bullet hole
newobj = add_bullet_act.getLastCreatedObject()
if obj.ebhole and ray_sensor.isPositive():
hit_pos = ray_sensor.getHitPosition()
hit_norm = ray_sensor.getHitNormal()
# woohoo!
newobj.setOrientation(MAT_trackvector(hit_norm, [0.0,0.0,1.0]))
pass
newobj.setPosition(hit_pos)
obj.ebhole = 0
# determine wether or not to create a new bullet hole
make_bullet = ray_sensor.isPositive() and lmb_sensor.isPositive()
if make_bullet:
# I am making the bullet hole, w00t
GameLogic.addActiveActuator(add_bullet_act, 1)
obj.ebhole = 1
hou to copy only X and Y orientation from material ?