accessing vertices of a mesh from within BGE

In my last thread, the topic was destructible meshes(choppable trees) I have a section in my trees that is comprised of chunks of wood. The chunks are separate objects parented to the tree. They are uv mapped so that the seams are invisible on the trunk. When you chop at the choppable section, the chunk object the player’s ray detects is deleted. This is great however having loads of wood chunk objects per tree slows down performance. With Solarlune and agoose’s help we came to the conclusion that the best solution is keeping the chunks and tree all in one object, one mesh. This would allow me to easily take advantage of blender’s level of detail system(presumably, hopefuly) there still may be a little trickery involved in getting the right lod set up for a chopped tree but I think having one mesh is a step in the right direction. Now with the chunks being a part of the trees mesh, we need to locate the chopped chunk and be able to remove it by scaling it down to 0. Solarlune has a system that allows you to access and make changes to a meshes vertices in game. I had a look at an example blend, the system looks promising although I was getting an error because of a missing import module. This is most likely my fault and Solar if you’re reading this I still could use some help with the example file. In any case I’m looking for a concrete solution that I can implement asap. My python knowledge is very mechanical and slightly rigid, I’m not a wiz but I do know how to get things done once I have the right ingredients. You’re help is GREATLY appreciated. Thanks. Agoose and Solar I know you guys are busy but if you could jump back in that would be great.

Also, if it isn’t possible to locate the chunk of wood the player chopped at via ray sensor, I can also use a hit box parented to the tree. The tree is very much a representation of a real tree. If you think about a tree trunk in terms of geometry, it has 4 sides. so the hit cube’s right side represents the player standing at the right side of the tree and so on for all sides. I use ray.hitNormal. It returns the face of the cube that was hit by the player’s ray. So if it returns [0, -1, 0] that means the player was standing at the back of the tree. Knowing this, I can then remove a plug of the tree from the back side, creating the illusion that he player chopped a chunk of the tree. You may not be familiar with how the ray.hitNormal’s output works so just take my word for it. So yeah let’s say the player was indeed standing at the back of the tree when he chopped, I would have some way of labeling the chunks within the mesh, be it vertex groups or something, and then for example remove(scale it to 0) “chunk_back_1”

Give your wall of text some structure…

You need to know beforehand which face has which vertices.
Meaning, write manually a dictionary of face:[verts].

Also: make the tree meshes no colision.
Only spawn the hit boxes when character is near them.
Physics don’t update when you modify vertices…

I did a simple version:
As of blocky nature i can calculate the blocks of the tree by simple means.

press space to delete, and change in python which part it should delete.
(too lazy to implement mouse over ray…)


delete_tree_parts.blend (483 KB)


@VegetableJuiceF you are awesome. Just what I was looking for. Sorry for my post looking like the great wall of china :P. I really appreciate you taking time out of your day to make an example blend that is straight forward and easy for me to comprehend. This system has all sorts of possibilities and will be added to my toolbox. I’m keeping tabs on everyone who’s helped me out, thanks again.

The system works great for simple meshes like the one in the example you provided. Is there a way to easier pinpoint vertices? my mesh is a little too complex to just iterate through every vertex. Do you know of any possible way to get a vertex group? that way I could name each chunk. wishful thinking?

^ There’s no way to grab vertex groups or vertices that belong to a group in the BGE. I made a function to allow you to find vertices or polygons in a fixed manner (i.e. it’s the same regardless of how or if you change the mesh, as long as the polygon positions themselves don’t change much). OK, so I see you had an issue with my module. Did you check the files all out via SVN?

EDIT: In your picture, it seems like the chunk doesn’t need the sides, right? Because it slots into the tree, and then gets removed when you chop the tree…?

EDIT 2: A simpler way to go about this would be to just have the chunks be separate objects that get spawned in when you get close enough to a tree (like, a few BU away). It wouldn’t be too much of a pain on resources in that case, and you wouldn’t have to worry about altering the mesh.

Here are some ideas:
You can make a dictionary with the general chunk position (can be given by an object.

  1. Vertex paint each chunk with an unique color. print it’s vertex color . All vertices must have the exact same color.

mesh = object.meshes[0]
for v in range(1):    
    vertex = mesh.getVertex(0,0); 
  1. type the print result into the empty’s property.

  2. Place your chunk into it’s position, and if it’s not done, join it to the tree mesh.

  3. Run a script that collects all vertices with the color and either append them to an empty’s property or into a tree’s general dictionary.

To access the vertices, access the empty’s property, or the key you use in the tree dictionary (preferably) the empty’s position.

Thanks for the replies torakunsama and solar when I get home I’ll go over your replies. Torak your idea seems very promising I was hoping something existed that would allow me to easily identify an entire chunk, in this case via its color value. and solar I’ll try to get your blend working. I downloaded the files inside the folder that contained the example.blend file. It didn’t help with the error I was getting about not having the bghelper module. I’m assuming since it’s your module I can’t just import it without having some sort of script or some recourse? I’m not sure how third party imports work. It’s not your fault solar more so my lack of knowledge in that area, perhaps you could fill me in on how the bghelper module works. I’ll have a look at the link you left for your module when I get home

Yeah solar if nothing else I’ll have spawning the chunks in to fall back on but it still won’t be without a certain level of complexity. I mean I’m sure I could get it figured out but I’m in favor of the single mesh idea. Speculating at this point because I’m away from my pc. Just gathering ideas and resources. By the way do guys get notifications when a thread is replied to?

@torakunsama the value returned by .getRGBA is a one channel integer. Is there a way to convert this into a 4 channel tupple?

Well, that’s why I recommended the printout and manual input of the value, otherwise it would be automatic.
Unfortunately, I don’t know how to format in tuple format. However, setRGBA() accepts tupple values.
Besides, if you look for a vertex having an rgb value other than an integer, you wont find it… I guess. I’ll check a few files, to see if I was able to in the past.