I want to move a polygone using its 4 vertices. I get the MeshProxy of that vertex then I change its XYZ positions. The problem is that it separates the whole polygone from the object. I want to obtain a smooth deformation (proportional edit as in edit mode). How can I do that in BGE?


Smooth Shading.

A look at the docs bge.types.KX_MeshProxy reveals that

Vertices will be split by face if necessary. Vertices can only be shared between faces if:

  1. They are at the same position
  2. UV coordinates are the same
  3. Their normals are the same (both polygons are “Set Smooth”)
  4. They are the same color, for example: a cube has 24 vertices: 6 faces with 4 vertices per face

Smooth shading will stop the face separating from the mesh. Un-comment the lines marked with #SS in script below to test.

Pseudo linear falloff on a sphere.

Test script, on sensor event selects a random face, calculates the face center, finds all verts within a distance of that face center (arbitrary hardcoded 0.4) and moves them away using the face center vector. (Using a sphere to test where the origin is centroid, so any local point on surface is also a radial vector (normal))

A vert at the center of the face can be moved the maximum (1.1) units away falling off linearly to zero for verts on or outside 0.4 from calculated center of face.

from mathutils import Vector
from random import randint

def deform(cont):
    ob = cont.owner

    if not cont.sensors["Mouse"].positive:
        return None

    mesh = ob.meshes[0]
    poly = mesh.getPolygon(randint(0, mesh.numPolygons - 1))
    material_index = poly.getMaterialIndex()
    # find center of poly
    cent = Vector()
    n = poly.getNumVertex()
    for i in range(n):
        v = mesh.getVertex(material_index, getattr(poly, "v%d" % (i + 1)))
        cent += v.XYZ
        v.color = [1.0, 1.0, 0.0, 1.0] # juicy yellow head.
        #v.XYZ *= 1.1 #SS
    cent /= n
    #return # SS
    # get all the verts within 0.4 radius of cent
    for v_index in range(mesh.getVertexArrayLength(material_index)):
         v = mesh.getVertex(material_index, v_index)
         d = (v.XYZ - cent)
         if d.length <= 0.4:
             v.XYZ += 1.1 / 0.4 * (0.4 - d.length) * cent.normalized()

enter image description here

  • $\begingroup$ Do you know how I can save the modified object to .obj file @batFINGER ? $\endgroup$
    – yanisk
    Mar 14 '18 at 17:37
  • $\begingroup$ from the game engine? $\endgroup$
    – batFINGER
    Mar 14 '18 at 19:46
  • $\begingroup$ yes from the game engine after I deformed the shape? $\endgroup$
    – yanisk
    Mar 15 '18 at 14:05
  • $\begingroup$ Also, How can I define and apply a force that will not deform the shape continuously at every frame, but instead apply it once? $\endgroup$
    – yanisk
    Mar 16 '18 at 11:10

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.