Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by joseques · Dec 05, 2014 at 07:14 PM · c#collisionmeshmesh-deformation

Mesh deformation with collisions

Ok, this is my case:

I have this script to make a mesh react with collisions and deform the vertices of the mesh depending on the contact points and the force of the collision. The problem is the script, it's not working at all. When I search the index of the vertex that it's equal to the contact point it doesn't found anything. Can you guys help me a bit?

Edit: I think the getNearestVertex it's not needed at this time. The collider it's based on the mesh.

This is my script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class HitDeformation : MonoBehaviour {
     public MeshFilter meshFilter;
     public float forceDetectionThreshold = 5;
     private Mesh mesh;
     private Material mat;
     private List<Vector3> Points;
     private List<Vector3> Verts;
     void Start(){
         Verts = new List<Vector3>();
     }
     void OnCollisionEnter(Collision col){
         if(col.relativeVelocity.magnitude > forceDetectionThreshold)
             deformMesh(col);
     }
     void deformMesh(Collision collision){
 
         mesh = meshFilter.mesh;
         ContactPoint[] contacts = collision.contacts;
         float force = collision.relativeVelocity.magnitude;
         Verts.Clear ();
         Verts.AddRange(mesh.vertices);
         Debug.Log("Colision and mesh data stored in deformMesh function variables. Comparing Contact points with mesh");
         foreach(ContactPoint c in contacts){
             int i = Verts.IndexOf(c.point);
             if(i != -1){
                 Debug.Log("vertex found. Index nº "+i);
                 Verts[i] = Verts[i] + (-transform.forward*force);
             }
         }
         Debug.Log("dumping new verts to mesh vertices");
         mesh.vertices = Verts.ToArray();
         Debug.Log("dumping new mesh to MeshFilter mesh");
         meshFilter.mesh = mesh;
         //meshFilter.mesh.Optimize (); I don't know if this it's really neccesary
     }
     /*Vector3 getNearestVertex(Vector3 point){
         float minDistanceSqr = Mathf.Infinity;
         Vector3 nearestVertex = Vector3.zero;
         Debug.Log("Finding nearestVertex of given point");
         // scan all vertices to find nearest
         foreach (Vector3 vertex in Verts){
             Vector3 diff = point-vertex;
             float distSqr = diff.sqrMagnitude;
             if (distSqr < minDistanceSqr){
                 minDistanceSqr = distSqr;
                 nearestVertex = vertex;
             }
         }
         
         // convert nearest vertex back to world space
         return transform.TransformPoint(nearestVertex);
     }*/
 
     /* get collision
      * Get the contact points
      * Push the points where the contact points are
      * update the mesh
      * voila?
      */
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joseques · Dec 06, 2014 at 05:48 AM 0
Share

Sorry for this, I don't know if bumping a question it's allowed but I really need an answer

avatar image quansatthu · Dec 06, 2014 at 08:14 AM 0
Share

The contact point is NOT the vertex of your collider, it's just the contacting position on your collider. Nearest vertex is needed this time.

But deformating like this is not a good idea at ALL, you should just resizing the whole object

avatar image joseques · Dec 06, 2014 at 08:57 AM 0
Share

That's why it doesn't find the contact point? I tried with finding the nearest point but still not working

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by EvilTak · Dec 06, 2014 at 09:20 AM

Try this: http://forum.unity3d.com/threads/now-with-source-mesh-deformation-on-impact.50214/ You can also add some noise to the deformation to add realism.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joseques · Dec 06, 2014 at 10:30 PM 0
Share

I've been searching this everywhere. How I didn't see that thread?

Thank you very much. Works like a charm.

avatar image
0

Answer by KingDolphin · Dec 06, 2014 at 08:52 AM

I'm not entirely sure what the problem is, but here is a probably really messy script that I made awhile back that deforms another mesh.

this goes on the object hitting the deformable object

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class deformer : MonoBehaviour {
 
     public float speed = 2f;
     public Mesh mesh;
     public Vector3[] verts;
     public float maxDistance;
     public GameObject explosion;
     
     void Start() {
         mesh = this.GetComponent<MeshFilter>().mesh;
         verts = mesh.vertices;
         this.GetComponent<MeshFilter>().mesh = mesh;
     }
     
     void OnCollisionEnter(Collision other) {
         Debug.Log("Collided with " + other.gameObject.name);
         if (other.gameObject.GetComponent<deformable>() != null) {
             Vector3 colPosition = transform.InverseTransformPoint(other.contacts[0].point);
             movePoints(other.gameObject);
         }
     }
     
     public void movePoints(GameObject other) {
         Vector3[] otherVerts = other.GetComponent<deformable>().verts;
         float distance;
         for (int i=0; i<otherVerts.Length; i+=1) {
             distance = GetDistance((rigidbody.position), other.transform.TransformPoint(otherVerts[i]));
             if (distance <= maxDistance) {
                  //edit the vertices
             }
         }
         other.GetComponent<deformable>().UpdateMesh(otherVerts);
     }
 
 }





this goes on the object being deformed

using UnityEngine; using System.Collections;

public class deformable : MonoBehaviour {

 public Mesh mesh;
 public Vector3[] verts;
 public Mesh oldMesh;
 
 void Start () {
     if (mesh == null) {
         oldMesh = mesh = this.GetComponent<MeshFilter>().mesh;
     }
     verts = mesh.vertices;
     this.GetComponent<MeshFilter>().mesh = mesh;
 }

 public void UpdateMesh() {
     mesh.vertices = verts;
     this.GetComponent<MeshFilter>().mesh.vertices = mesh.vertices;
 }
 
 public void UpdateMesh(Vector3[] points) {
     mesh.vertices = points;
     this.GetComponent<MeshFilter>().mesh.vertices = mesh.vertices;
     this.GetComponent<MeshCollider>().sharedMesh = null;
     this.GetComponent<MeshCollider>().sharedMesh = mesh;
 }

 void OnApplicationQuit() {
     this.GetComponent<MeshFilter>().mesh.vertices = oldMesh.vertices;
     this.GetComponent<MeshCollider>().sharedMesh = null;
     this.GetComponent<MeshCollider>().sharedMesh = oldMesh;
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joseques · Dec 06, 2014 at 09:00 AM 0
Share

I'll give it a try and I'll let you know if this works for me.

Another question: Why you are using the shared$$anonymous$$esh? I've readed that it's recommended to use the shared$$anonymous$$esh only for reading the mesh data.

avatar image KingDolphin · Dec 06, 2014 at 09:07 AM 1
Share

Oh, when I first made this, it was my first time ever doing anything with meshes and I was doing it for a hackathon, so when $$anonymous$$onodevelop recommended things when I put the '.' after get component, I didn't see just mesh only shared mesh and it worked for my scenario so yeah, probably shouldn't use that if you have multiple objects being deformed.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to render a lot of the same small objects 2 Answers

Mesh Collider on a 2D mesh creating a 3D box? 0 Answers

new Mesh() works outside of array, but not in array. Huh? 3 Answers

Realtime mesh deformation collisions in Unity 5 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges