PiCode9560 / Godot-4-Concave-Mesh-Slicer

MIT License
134 stars 7 forks source link

Adding Mass Calculations #7

Closed nhwoodsaa closed 5 days ago

nhwoodsaa commented 5 days ago

Hi, I wanted to add some Mass calculations so when you slice the object the existing body and body2 will have a mass value based on percentage of the slice.

Here is what I came up with;

func calculate_mesh_volume(mesh: ArrayMesh) -> float: var volume = 0.0 for surface in range(mesh.get_surface_count()): var arrays = mesh.surface_get_arrays(surface) var vertices = arrays[Mesh.ARRAY_VERTEX] for i in range(0, vertices.size(), 3): var v1 = vertices[i] var v2 = vertices[i + 1] var v3 = vertices[i + 2] volume += abs(v1.dot(v2.cross(v3))) / 6.0 return volume

Add these to the # Slice Rigidbody section:

var volume1 = calculate_mesh_volume(meshes[0]) var volume2 = calculate_mesh_volume(meshes[1]) var total_volume = volume1 + volume2

var mass1 = body.mass (volume1 / total_volume) var mass2 = body.mass (volume2 / total_volume)

Need to set these variables at the right time, I calculate body.mass after the body.center_of_mass and the same for body2: body.mass = mass1 body2.mass = mass2

Maybe you want to add this in, if not thats cool just thought I'd share.

PiCode9560 commented 5 days ago

What's the point of this? I thought the mass is automatically calculated based on the size.

Edit: maybe it didn't

nhwoodsaa commented 5 days ago

When I was printing the RigidBody3D mass after slicing the mesh it wasn't updating for me, (e.g. a small chunk of an object would still weigh 1000kg and my player unable to move it) so this was my way of implementing that on the player script.

PiCode9560 commented 5 days ago

I just added it. Thanks! b44711e

nhwoodsaa commented 5 days ago

No worries, awesome addon btw having fun with it so far :)