Bullet script

Hi all, im having problems with this bullet script. when it adds the hole, if you move, the hole flickers, indicating it is not aligned properly. any help?:

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.sensors["rayhole"]
lmb_sensor = cont.sensors["fire"]
add_bullet_act = cont.actuators["add_bullet"]
from GameLogic import*
getScene = {}
for a in GameLogic.getSceneList():
     getScene[a.name] = a

camerascene = getScene['FPS']
objList = camerascene.objects
camera = objList["OBCamera"]
ammo = getattr(camera, "ammo")
# position the last added bullet hole
newobj = add_bullet_act.objectLastCreated
if obj.ebhole and ray_sensor.isPositive():
    hit_pos = ray_sensor.hitPosition 
    hit_norm = ray_sensor.hitNormal
    newobj.orientation = (MAT_trackvector(hit_norm, [0.0,0.0,1.0]))
    newobj.localPosition = hit_pos
    obj.ebhole = 0

# determine wether or not to create a new bullet hole
make_bullet = ray_sensor.isPositive() and lmb_sensor.isPositive() and ammo > 0

if make_bullet:
    GameLogic.addActiveActuator(add_bullet_act, 1)
    obj.ebhole = 1

That issue may be because the decal is overlapping the wall’s face
To correct this, instead of using this code :

hit_pos = ray_sensor.hitPosition
hit_norm = ray_sensor.hitNormal
newobj.localPosition = hit_pos

I would suggest :

Offset = 0.1 # Offset between the decal and the wall hit (In Blender units) = (In meters most probably)
hit_pos = Vector(ray_sensor.hitPosition )
hit_norm = Vector(ray_sensor.hitNormal).normalize * Offset
newobj.localPosition = hit_pos + hit_norm

newobj.orientation = (MAT_trackvector(hit_norm, [0.0,0.0,1.0]))

There is surely a big bunch of syntax errors in there since I can’t test it here, but I guess you have something to base you there. Hopefully it might work just fine once you correct my noobish ehheeh

what is the term VECTOR?

It’s a mathutils’s method to create a vector object out of a list.

…which can then be bent under the laws of vectorial math.

but if you’re using blender 2.5, I believe most instance variables are already converted to vectors or matrices by default if it apply.


import Blender
from Blender import Mathutils
from Blender.Mathutils import *
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.sensors["rayhole"]
lmb_sensor = cont.sensors["fire"]
add_bullet_act = cont.actuators["add_bullet"]
from GameLogic import*
getScene = {}
for a in GameLogic.getSceneList():
     getScene[a.name] = a

camerascene = getScene['FPS']
objList = camerascene.objects
camera = objList["OBCamera"]
ammo = getattr(camera, "ammo")
# position the last added bullet hole
newobj = add_bullet_act.objectLastCreated
if obj.ebhole and ray_sensor.isPositive():
    Offset = 0.1
    hit_pos = Vector(ray_sensor.hitPosition )
    hit_norm = Vector(ray_sensor.hitNormal).normalize * Offset
    newobj.localPosition = hit_pos + hit_norm
    
    newobj.orientation = (MAT_trackvector(hit_norm, [0.0,0.0,1.0]))
    obj.ebhole = 0

# determine wether or not to create a new bullet hole
make_bullet = ray_sensor.isPositive() and lmb_sensor.isPositive() and ammo > 0

if make_bullet:
    GameLogic.addActiveActuator(add_bullet_act, 1)
    obj.ebhole = 1

error TypeError: unsupported operan type(s) for *: ‘builtin_function_or_method’ and ‘float’
any help in porting