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:

<Vector (1.0000, 1.0000, 1.0000)>
<Vector (1.0000, -1.0000, 1.0000)>
<Vector (-1.0000, -1.0000, 1.0000)>
<Vector (-1.0000, 1.0000, 1.0000)>
<Vector (1.0000, 1.0000, -1.0000)>
<Vector (-1.0000, 1.0000, -1.0000)>
<Vector (-1.0000, -1.0000, -1.0000)>
<Vector (1.0000, -1.0000, -1.0000)>
<Vector (1.0000, 1.0000, 1.0000)>
<Vector (1.0000, 1.0000, -1.0000)>
<Vector (1.0000, -1.0000, -1.0000)>
<Vector (1.0000, -1.0000, 1.0000)>
<Vector (1.0000, -1.0000, 1.0000)>
<Vector (1.0000, -1.0000, -1.0000)>
<Vector (-1.0000, -1.0000, -1.0000)>
<Vector (-1.0000, -1.0000, 1.0000)>
<Vector (-1.0000, -1.0000, 1.0000)>
<Vector (-1.0000, -1.0000, -1.0000)>
<Vector (-1.0000, 1.0000, -1.0000)>
<Vector (-1.0000, 1.0000, 1.0000)>
<Vector (1.0000, 1.0000, -1.0000)>
<Vector (1.0000, 1.0000, 1.0000)>
<Vector (-1.0000, 1.0000, 1.0000)>
<Vector (-1.0000, 1.0000, -1.0000)>

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]
    angle = radians(own['angle'])
    rotation_matrix = Matrix.Rotation(angle, 4, 'Z')
    mesh.transform(-1, rotation_matrix)

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

Bad link…

But thank you,
this was exactly what i was looking for!

doing a new particle script ?

Nope, just turning the idea of solarlunes flatten script into a game template thingy…
This is for rotating foliage/grass blocks in static mesh to look more natural.

Oops, yes, it was a link to the API on disk. Changed it. Glad it worked out. This method is also much faster than iterating through the vertexes.