Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Metal Like Deforms on Meshes #36

Open
razzraziel opened this issue Nov 9, 2018 · 3 comments
Open

More Metal Like Deforms on Meshes #36

razzraziel opened this issue Nov 9, 2018 · 3 comments

Comments

@razzraziel
Copy link

razzraziel commented Nov 9, 2018

This is a really great project and i personally thank all who contributed.

What really improves this project is that having more metal like deformations. Like in Truss Physics:
https://www.youtube.com/watch?v=fNfChjsYLXc

I'm working on this project for my more damage based game. I'm planning to have not only crashes, also other flying objects' impacts.

Deformation takes a lot of CPU power (have 200ish mesh colliders & no collider deform) and deformation itself can be improved with more realistic way. Instead of checking all the vertices, can we handle vertices as groups with their neighbour vertices and bend object itself with predefined specific points. Displacement parts is just avoiding this realism so i put that off topic.

For example, instead of making deforming the hood with impact, we can bend the whole hood with predefined vertex points. Here is a basic bending code:

       vertices = mesh.vertices;
       //We're bending around the x axis for example but it depends on force and direction.
        if (axis == BendAxis.X)
        {
            float meshWidth = mesh.bounds.size.z;
            for (var i = 0; i < vertices.Length; i++)
            {
                float formPos = Mathf.Lerp(meshWidth / 2, -meshWidth / 2, fromPosition);
                float zeroPos = vertices[i].z + formPos;
                float rotateValue = (-rotate / 2) * (zeroPos / meshWidth);

                zeroPos -= 2 * vertices[i].x * Mathf.Cos((90 - rotateValue) * Mathf.Deg2Rad);

                vertices[i].x += zeroPos * Mathf.Sin(rotateValue * Mathf.Deg2Rad);
                vertices[i].z = zeroPos * Mathf.Cos(rotateValue * Mathf.Deg2Rad) - formPos;
            }
        }
        mesh.vertices = vertices;
        mesh.RecalculateBounds();

So maybe we can improve this and integrate with predefined point inputs per object for more pleasing damage physics with metal feel. What do you think?

@razzraziel razzraziel changed the title More Physically Correct Deform on Meshes More Metal Like Deforms on Meshes Nov 9, 2018
@JustInvoke
Copy link
Owner

JustInvoke commented Nov 10, 2018

You are free to implement this if you wish, but I'm not going to commit to extending the damage system like this. One thing you might want to try is using quaternions to deal with twisting or rotating vertices around a point or axis. This example is derived from my recent Convex Collider Creator asset featuring mesh deformation:

Vector3 center; //Rotation origin in local space
Quaternion twist; //Twist rotation in local space
float deformRadius; //Radius of vertices twisted around center
float deformStrength; //Strength of twisting
float deformFalloff; //Falloff for twisting based on distance to origin from each vertex
Vector3[] verts = mesh.vertices;
for (int i = 0; i < verts.Length; i++)
{
    float deformAmount = Mathf.Pow(Mathf.Max(0.0f, curHook.radius - Vector3.Distance(verts[i], center)), falloff);
    Vector3 newVert = center + twist * (vertices[i] - center);
    verts[i] += (newVert - verts[i]) * deformAmount * strength;
}
mesh.vertices = verts;
mesh.RecalculateNormals();
mesh.RecalculateBounds();

This is untested but gives a general idea of how to twist vertices with an arbitrary rotation. Assume the variables before the loop excluding verts are set based on the situation.

@TheBricktop
Copy link

Well at first I was thinking that deformations are handled using bone rigging for simplification and was suprised that it actually modifies the mesh.

@razzraziel
Copy link
Author

Well at first I was thinking that deformations are handled using bone rigging for simplification and was suprised that it actually modifies the mesh.

Do you have any reference for that kind of example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants