# Deforming a mesh in realtime (Game Engine)

I have a subdivided plane I want to act as a niveau map.

I want to change the niveau extremes' locations and want the map to display the niveau in the form of z=f(x,y) with x,y as the location of a significant niveau point and want to plot z to the plane with a function f of my choosing.

How can I change and apply the vert's location (z value)?

1) I'd like the changes to be real time. I'd like to show the movement of the niveaus with it.

2) Because of that they need to happen while the game runs

3) Yes I tried to find and change the objects vertices like this:

bpyobject=bpy.data.scenes[scenename].objects[bgeobject.name]
for vert in bpyobject.data.vertices:
vert.co[2]=vert.co[2]+1
bpyobject.data.update()


But this only updates the mesh after the game engine ends.

• Your not really explaining the context here, 1) do the changes need to be real time, 2) do the changes happen in-game, or before the game runs? 3) did you try this already and have problems? – ideasman42 Dec 17 '13 at 16:46

Firstly you'll need to use the bge module, bpy data access wont work since the game engine stores its own data.

For this you will need to use KX_MeshProxy, In the link there is an example for changing a meshes vertex color, but from the API docs you can see how you could change location.

eg: from bge import logic

cont = logic.getCurrentController()
object = cont.owner

for mesh in object.meshes:
for m_index in range(len(mesh.materials)):
for v_index in range(mesh.getVertexArrayLength(m_index)):
vertex = mesh.getVertex(m_index, v_index)
vertex.XYZ = 1, 2, 3 * v_index


## Note

• that this isnt really optimal from Python, for a high poly mesh the Python script may slow down execution a lot, if the motion isnt so fast you could move N verts per script execution, so at least you dont get jerky playback which would happen if you simply execute the script less often.

• You may want to look into GLSL vertex shaders, this is quite invovled but has the advantage that moving vertices about is offloaded onto the graphics card.

• Does updating the mesh this way automatically update the collision mesh? Or do you have to call kx_obj.reinstancePhysicsMesh()? – Mike Pan Dec 17 '13 at 23:33
• You need to explicitly re-run kx_obj.reinstancePhysicsMesh() since it will rebuild the entire collision shape and can get quite slow. – ideasman42 Dec 18 '13 at 6:33