5
$\begingroup$

I'm using BGE and I need to move the vertices of a mesh using Python. I know how to change the position of the vertices in real time but the problem is that they move using LOCAL coordinates and I need to move them along NORMAL coordinates.

I found the code below but I don't know what to do with "nor", or maybe this is not the right way, I don't know.

###### get the vertex normal vector
# get controller
controller = GameLogic.getCurrentController()
# get object the controller is attached to
obj = controller.owner
# get the 1st mesh
mesh = obj.meshes[0]
# get the first vertex of the first material
vert  = mesh.getVertex( 0, 0)
# get normal of this vertex
nor = vert.normal
$\endgroup$
2
$\begingroup$

The vertex normals are the interpolated normals of the faces which are perpendicular to the faces. The vertex normals are normalized this means the length of this vector is 1, therefore you might want to scale them before adding the normal vector to the vertex coordinates.

enter image description here

Moving the vertices along their normals would create an effect as shown in the this image:

enter image description here

import bpy
import GameLogic
###### get the vertex normal vector
# get controller
controller = GameLogic.getCurrentController()
# get object the controller is attached to
obj = controller.owner
# get the 1st mesh
mesh = obj.meshes[0]
for v in range(mesh.getVertexArrayLength(0)):
    vert = mesh.getVertex(0, v)
    norm = vert.normal * .3
    vert.XYZ = (vert.x + norm.x, vert.y + norm.y, vert.z + norm.z )
$\endgroup$
  • $\begingroup$ how would you go about doing this "scale them before adding the normal vector" bit you speak of ? $\endgroup$ – user7500 Oct 15 '14 at 2:52
  • $\begingroup$ @red566 Didn't test it but multiplying each x,y,z value by scaling factor e.g. 1.1 in last line could do it (when I get you right). $\endgroup$ – stacker Oct 15 '14 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.