I tried to create a wind system in the foliage, but as I don’t know much about these things, I ended up getting this result:
I obviously didn’t want the vertices to split. what did I do wrong?
My script:
import bge
from collections import OrderedDict
from math import sin, pi
from mathutils import Vector
from random import uniform
class Component(bge.types.KX_PythonComponent):
args = OrderedDict([
("Force", 0.0),
("Velocity", 0.0),
])
def start(self, args):
self.force = args["Force"]
self.vel = args["Velocity"]
self.verts = []
mesh = self.object.meshes[0]
for mi in range(len(mesh.materials)):
for vi in range(mesh.getVertexArrayLength(mi)):
self.verts.append([
uniform(-pi*2, pi*2),
uniform(-pi*2, pi*2),
uniform(-pi*2, pi*2),
*mesh.getVertex(mi, vi).XYZ
])
def vetices(self):
mesh = self.object.meshes[0]
for mi in range(len(mesh.materials)):
for vi in range(mesh.getVertexArrayLength(mi)):
vec_pos = Vector([self.verts[vi][3], self.verts[vi][4], self.verts[vi][5]])
self.verts[vi][0] += self.vel
self.verts[vi][1] += self.vel
self.verts[vi][2] += self.vel
if self.verts[vi][0] > pi*2: self.verts[vi][0] = 0.0
if self.verts[vi][1] > pi*2: self.verts[vi][1] = 0.0
if self.verts[vi][2] > pi*2: self.verts[vi][2] = 0.0
x_sin = vec_pos[0] + sin(self.verts[vi][0])*self.force
y_sin = vec_pos[1] + sin(self.verts[vi][1])*self.force
z_sin = vec_pos[2] + sin(self.verts[vi][2])*self.force
final_pos = Vector([x_sin, y_sin, z_sin])
vertex = mesh.getVertex(mi, vi)
vertex.XYZ = final_pos
def update(self):
self.vetices()
In “def start ()” I create a function that takes the positions of the vertices and saves them in a list along with a random value between 0.0 and pi * 2.
In the def “vetices ()” I take these positions and add the time sine of the random values in the list.