Home Technical Talk

Autodesk 3ds Max Maxscript: Return vertex index of an object touching another object?

polycounter lvl 6
Offline / Send Message
MrQuetch polycounter lvl 6
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

  • monster
    Options
    Offline / Send Message
    monster polycounter
    Two ways I can think of to accomplish this. I’m assuming the spheres are not attached using a wire or controller because querying that would be the fastest way to find the vertex.

    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.
  • MrQuetch
    Options
    Offline / Send Message
    MrQuetch polycounter lvl 6
    monster said:
    Two ways I can think of to accomplish this. I’m assuming the spheres are not attached using a wire or controller because querying that would be the fastest way to find the vertex.

    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.
    Thank you for the ideas, monster.

    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.

    theSpheres = #()
    sphereIndexes = #()
    
    -- Get the vertices, so we know where to place the spheres
    for i = 1 to obj_vertices do
    	(
    	-- Check if the sphere exists, and if it does, add it to the array
    	if (getNodeByName("new_sphere_" + i as string) != undefined) do
    		(
    		new_sphere = getNodeByName ("new_sphere_" + i as string)
    		append theSpheres (new_sphere)
    		
    		obj_spheres += 1
    		)
    	)
    
    -- Check the vertex and sphere distances
    for i = 1 to obj_vertices do
    	(
    	vert = getVert obj_mesh i
    	
    	for j = 1 to obj_spheres do
    		(
    		dist = distance theSpheres[j].pos vert
    		--append sphereIndexes (dist)
    		
    		--if (dist < 1) do
    			--(
    			--)
    		)
    	)

  • RN
  • MrQuetch
    Options
    Offline / Send Message
    MrQuetch polycounter lvl 6
    RN said:
    Hi there, RN.

    Thanks for the link. I will look more into the information.
  • MrQuetch
    Options
    Offline / Send Message
    MrQuetch polycounter lvl 6
    Update: For those of you who are checking back on my progress, I have managed to find a way that works for me.

    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.
Sign In or Register to comment.