Setting Vertex coordinates from global positions?

Hey all!
I was going to help someone to create a floor following shadow, but my hypothesis failed *kind of!
I was trying to take the original vertex coordinates (which i saved to a read only dict) of the object, add them to the object position, because both the position and vertex coordinates are relative to the object center. Then, i would cast a ray from these global coordinates, and detect the Z coordinate of the hitPosition.
Then, i would take the difference between the object position and the hitposition (only Z) and set each vertex as its original coordinates, but add the difference from each [hitpos to object] to the original Z, so that it would look something like:

new = Vector(orig[0],orig[1],orig[2]+difference)
vertex.setXYZ(new)

However it appears as though there is a ratio offset, as the mesh moves above and below the hitMesh, as though scaled by a factor…
Could you give this python a glance?


import bge
import bpy 

from mathutils import*

cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

matrix_world = bpy.data.objects[own.name].matrix_world
world_orientation = own.worldOrientation

mesh = own.meshes[0]
mat = mesh.materials[0]
vertices = mesh.getVertexArrayLength(0)
if not "orig" in own:
    own["orig"] = {}
for i in range(0,vertices):
    vertex = mesh.getVertex(0,i)
    vertex_co = vertex.getXYZ()
    
    if not "init" in own:
        own["orig"][i] = vertex_co
        print("added",i)
    
        
    _vertex_global = own["orig"][i]
    vertex_global = own.worldPosition-_vertex_global
    
    
    range = 40
    start = Vector([vertex_global[0],vertex_global[1],vertex_global[2]])
    extend = Vector([vertex_global[0],vertex_global[1],vertex_global[2]-range])
    
    bge.render.drawLine(start,extend,[1,0,0])
    
    ray = own.rayCast(start,extend,range)
    
    if ray[0]:
        hit_pos = ray[1]
        
        _hit_pos = hit_pos[2]-0.1
        _new_vert_pos = own.worldPosition[2]-_hit_pos
       
        
        new_vert_pos = Vector([vertex_co[0],vertex_co[1],own["orig"][i][2]-_new_vert_pos])
        
        vertex.setXYZ(new_vert_pos)
   

own["init"] = False

Anyone with some ideas?