Keep an object a fixed distance from the ground

Hi all,

In my ongoing battle with Python (and Blender) I am thinking of ways to fix a long standing problem with a script I have. The script enables an object to stick to the ceiling- however, I cannot get it to play well with the material physics. To combat this, I am considering using getDistanceTo and applyMovement to keep the object from colliding with the floor. I am beginning to sort of understand Python, but will this approach work? I expect the script to do a couple of things:

getDistanceTo(other) would check the distance in the -z co-ordinate to the floor (i.e. the track). If it is less than say 2 blender units then applyMovement(movement, local) would be used to move the object back to 2 b/u away again. If the distance is > 2 then the opposite would be done.

Thing is, is this method compatible with material physics in general? I mean, are the above related to delta location movement- and if so, is there a better way of doing things?

Here is the current movement code below without my planned changes.


from mathutils import Vector
import GameLogic
controller = GameLogic.getCurrentController()
owner = controller.owner
    
def alignToSurface(owner, align_vect=2, align_speed = 0.2, relative_ray_cast=True, ray_length=10.0, ray_prop=''):
    """ Align an object to the ground below it.

    align_vect: axis to be aligned, [0,1,2]=[x,y,z]
    align_speed: how quickly the object aligns to the surface
    relative_ray_case: set to true to cast a ray in local space, for doing loop-the-loops
    ray_length: how far to cast the ray, the ray is cast from the object's centre
    ray_prop: a property to look for when ray casting
    """
    if relative_ray_cast:
        direction = -owner.worldOrientation[2]
    else:
        direction = Vector([0,0,-1])

    hit_obj, hit_pos, hit_norm = owner.rayCast(owner.worldPosition+direction, owner, ray_length, ray_prop)
    
    if hit_norm:
        owner.alignAxisToVect(hit_norm, align_vect, align_speed)
        return True
alignToSurface(owner)


Many thanks

Paul