# Algorithm that cover one mesh by another mesh

Suppose I have two meshes (Let call me first MeshShirt and second - MeshBody) Meshes are aligned (if i render both MeshShirt "covers" MeshBody). But some parts of the body are not "under" the MeshShirt (because MeshShirt shape differs from MeshBody)

How can i "pull up" parts of MeshShirt which are under MeshBody and hold other parts in their places? Of course i want save shape of MeshShirt if it possible

I think about following algorithm

1. mark all points of MeshShire which under MehsBody by tracing intersection of its normals and MeshBody.

But if MeshShirt "hidden" parts have complex shape normal tracing will not work. So some way to detect bad points is needed

2. move every marked point to the plane of its nearest triangle of MeshBody.

But i afraid that these algoritm may produce significant deformation of MeshShirt. So some shape preserved deformation is needed too.

Sorry for my poor English and fuzzy problem definition

• Physics cloth? they are expensive... Or if you mean at modelling time, you better ask on your tool's forums. – Kroltan Dec 3 '14 at 9:54
• Expensive and hard to implement too :) But i not need realistic cloth simulation. It i suppose to run these algo in some initial phase of my program work. – Dark_Daiver Dec 3 '14 at 9:59
• "Initial" as in boot time, or shipped? If your shirt model has a common distance between each vertex, you can try moving the conflicting points, then rearranging the vertices connected to it recursively, until everything is within a threshold of your average distance. This works okay at your asset pipeline, and at load screens, but not very much in realtime. – Kroltan Dec 3 '14 at 10:05
• > "Initial" as in boot time, or shipped. I can't catch difference, sorry. Case of algo usage: user provide some model. Some my procedure preprocess this model, and this algo is final step of preprocess procedure. After preprocess user can view model in viewport and do some other things. I understand that this question is not pure gamedev question but i don't know appropriate resource for it. I will add some additional info to post – Dark_Daiver Dec 3 '14 at 10:16
• "shipped" would be that you have the models before shipping the game to users. But by your description it's the first option. Well, my suggestion while not optimal (I'm not a expert on this) works quite okay, I've used it before with custom colliders, to approximate them to a user-supplied model. – Kroltan Dec 3 '14 at 10:19

First, find the average, minimum and maximum distance all vertices in the shirt mesh. Let those be Da, Di and Dx, respectively.

Also let Dai = Di/Da and Dax = Dx/Da.

Then, you test each point of your shirt mesh Ms to know if it's inside the body mesh Mb, possibly using the Jordan Curve Theorem, which is based on raycasting. Store all vertices which are found to be inside the mesh in a list, then for each vertex Vc:

• Get the nearest vertex Vn of Mb, trace a ray from Vc to Vn, store the contact point Pc
• Move Vc to Pc

After this, you should have a relatively distorted model of your shirt. You then loop again on each Vc:

• For each vertex Vx connected to Vc:
• Get the distance of Vc to Vx
• If it's greater than Dx, move Vx to a point Dax units away from Vc
• If it's smaller than Di, move Vx to a point Dai units away from Vc
• Repeat step 3 for all the vertices connected to Vx

I assume you know how to find the distance between vertices, and how to get a new vertex from a vertex, direction and distance. If not, it would be great to learn that.

This should work for most meshes, but it's quite expensive, so I don't recommend doing this at runtime. If possible, run it once just and save the final mesh somewhere you can keep using it without recalculating.

This works the best for meshes with relatively close edge lengths, that is, a even distribution of vertices along the surface, though it works OK for other meshes too.

I hope this is clearer than the discussion in the comments.

• Thank you for answer! I'll try to implement these algorithm and apply it to my meshes. Also, If some Vx is Vc too what i should do? Just move it like other Vx's? – Dark_Daiver Dec 4 '14 at 11:32
• @Dark_Daiver Yes, that will help reduce distortion produced in the first part of the algorithm. – Kroltan Dec 4 '14 at 13:28
• @Dark_Daiver So, did you get it to work? If this answer is fit, feel free to click the green checkmark to mark this answer as valid. You can always remove it if a better answer is added. :) – Kroltan Dec 8 '14 at 12:18
• I know about green check mark =) But i have limited time to do these task. Also now i am debugging first stage of algo (searching bad vertices) – Dark_Daiver Dec 8 '14 at 12:21
• @Dark_Daiver Okay, I hope you manage to do it! – Kroltan Dec 8 '14 at 12:23