3
\$\begingroup\$

I'm having difficulties trying to create a procedural mesh that deforms on impact. The deformation is meant to look and feel like metal when it is being forged. Since this is less of an aesthetic feature and more of something that would affect gameplay directly, it needs to be somewhat accurate. This means that volume of the mesh should be preserved (at least visually) such that the mesh does not gradually get too much smaller.

What I have tried:

1) Applying force on every vertex, based on the force and distance from impact point

foreach (Vector3 vertexPos in vertexList)
{
    vertexPos += (force / distanceToImpactPoint.sqrMagnitude) * impactDir;
}

This led to the entire mesh gradually becoming smaller (as mentioned earlier)

2) Take into account the dot product between the impact direction and vertex normal for each vertex, as well as lerp between impact direction and normal (to simulate sides of objects bulging out when compressed

foreach (Vector3 vertexPos in vertexList)
{
    float attenuatedForce = (force / distanceToImpactPoint.sqrMagnitude);
    float nDotD = Vector3.Dot(impactDir, currVertexNormal);
    vertexPos = attenuatedForce * nDotD * Vector3.Lerp(impactDir, currVertexNormal, nDotD);
}

This led to mesh completely splitting apart for anything that has hard shading (as there are duplicate vertices where the edges are, and also artifacts on smooth shaded objects.

Hard Shading: enter image description here

Soft Shading (sort of works, but lots of artefacts and not volume preserving) enter image description here

Also, both methods break completely if force is applied to an edge.

Does anyone know of a more elegant solution to do this?

\$\endgroup\$
  • 3
    \$\begingroup\$ Hitting metal works like hitting a liquid with a very very high viscosity (blacksmith lower this viscisity by heating metals up). You could try some sort of fluid simulator algorithm (like Navier-Stokes) and apply force to the "liquid" where you hit it. \$\endgroup\$ – Bálint Mar 28 '17 at 18:22
  • \$\begingroup\$ @Bálint Fluid simulation using Navier-Stokes would not be able to have physics interactions with external colliders as far as I can tell :( (NVIDIA's implementation on the fragment shader), and a particle based approach I worry might be too computationally demanding, but I will look into. I was hoping for something that affects the mesh directly, such that I can still throw a mesh collider on it \$\endgroup\$ – eclmist Mar 29 '17 at 9:51
  • \$\begingroup\$ You could look into procedural mesh generation, like marching cubes. That makes you able to throw a meshcollider on iz. \$\endgroup\$ – Bálint Mar 29 '17 at 11:01
  • \$\begingroup\$ I can post a complete answer if you want. \$\endgroup\$ – Bálint Mar 29 '17 at 11:01
  • \$\begingroup\$ @Bálint If you can post a complete answer, please do!! That would be much appreciated! \$\endgroup\$ – eclmist Mar 29 '17 at 11:12
2
\$\begingroup\$

Due to the relatively low melting temperature of metals, they act like a fluid (sort of) when you hit them (it' closer to a gel, but gels are also fluids technically). The higher something's temperature is, the more viscose it gets (that's why metal workers heat up metals bars before using them, try heating up honey to test this).

So, you can technically simulate the properties of them with a fluid simulation algorithm (like the Navier-Stokes equations) by using a very very high viscosity.

Most fluid simulators allow you to apply a force at a specific position. You can use this to apply the hit force to the material, which makes a part of the fluid more dense, thus the algorithm dissipates it to the sides. Because fluids are uncompressable (they are, but a higjer compression would create a nuclear reaction), the material won't ever disappear.

Creating a mesh out of the data is very easy too. Most fluid simulators use a grid to store the water. You can just take this grid and process it using an algorithm like marching cubes to produce a mesh out of it.

This mesh can then get rendered and used in a mesh collider.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.