# Rotating mesh (by moving vertex)?

Hello,

this is more a math question:
A cube’s mesh is made out of 24 vertices with relative to center positions:

``````&lt;Vector (1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (1.0000, 1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (1.0000, 1.0000, -1.0000)&gt;
&lt;Vector (1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, -1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, -1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (1.0000, 1.0000, -1.0000)&gt;
&lt;Vector (1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, 1.0000)&gt;
&lt;Vector (-1.0000, 1.0000, -1.0000)&gt;
``````

How would i go about rotating the mesh?
Simple approaches, like rotating the object or armatures to affect the vertex array…

Rotating 180 or 90 degrees is trivial.

``````import bgeimport random

def main():

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

rotate(own)

# would need:
#rotate(own, [0,0,random] )

def rotate(kx_obj):
matID           = 0
obj_mesh        = kx_obj.meshes[0]
obj_length      = obj_mesh.getVertexArrayLength(matID)

for i in range(obj_length):
mesh_vert = obj_mesh.getVertex(matID,i)

pos = mesh_vert.getXYZ()
print( pos )
pos.z *= -1

mesh_vert.setXYZ( pos )
print("")
main()

``````

### Attachments

rotate_vert.blend (524 KB)

If you have to transform every vertex in the mesh, the API provides a function for the mesh: KX_MeshProxy.transform. For example if you would like to rotate the mesh by some value in degrees on the Z-axis you could have this code:

``````from mathutils import Matrix
from math import radians

def turn_mesh(cont):
own = cont.owner
mesh = own.meshes[0]
rotation_matrix = Matrix.Rotation(angle, 4, 'Z')
mesh.transform(-1, rotation_matrix)
``````

Don’t forget to give the object a property with name ‘angle’.