Hello.
Before I get complex, I want to break this down in simple terms so anyone who reads this will understand what I'm trying to do. I have two objects in my scene. One object is the mesh of a character, and the other object is just a simple sphere. The sphere model's position is set on a vertex of the player model. I'm trying to figure out how I can get the vertex index of the player model that the sphere is touching. Although this may work nicely for one sphere model...
My problem is that if I have multiple spheres, I don't know where to go from there. Let's say I've named my models as "sphere_1", "sphere_2" and so on. How can I check to see what indexes they lay on in the player model? The correlation between the vertex indexes in the player model will not be the same as the spheres, since the amount of spheres will be less than the amount of vertices. So, I need some way to return the vertex indexes of the player model from the spheres touching said vertices.
For example: I could have a sphere named "sphere_1", but, it could be placed on vertex 5 in the player mesh.
Or, I could have a sphere named "sphere_7", but it could be placed on vertex 2 in the player mesh.
I've been adding my spheres to an array, but I have to get the proper indexes from the player model, and I'm unable to do that.
Thanks. I hope you understand.
Replies
So...
1. Get the position of each sphere. Then loop through the verts and get the distance between the verts and the sphere and if the distance is smaller than the previous store that one. You should end with the closest vert per sphere.
2. If your mesh is dense and you only have a few spheres it might be faster to create a mesh select modifier per sphere with a small radius. Move the modifier to the sphere’s position. Then query each mesh select modifier for its selection.
I can't believe I didn't think about distance sooner. It's more likely I'll go with the first option, since I'm not using high poly models.
I will try those and see which works best.
Edit: Here is what I have now, but I'm still unsure how to compare the distances properly.
Thanks for the link. I will look more into the information.
Basically, if I want to place a sphere at a specific vertex on my character mesh, I name the sphere to contain the number containing the vertex index of that mesh. First, I loop through all of the vertices, and then I check if the spheres exist, and if they do, they are appended to an array. In the same loop, I also insert the items into a new array. The vertices that don't have a sphere are marked as "undefined". Then, I have another loop that checks if there are any "undefined" elements, and if there are, it just disregards them, and puts the numbers into another new array. Finally, we can loop through the spheres, and check what these new values are.
I admit - it is a strange way of doing it. But, since my models are low polygon, it isn't that much effort in the end.
The script works great now; thank you for all who have helped.