photo

[SOLVED] C# Code of Fracture Body & Complex tringles mesh or simple tringles mesh?

Recommended Posts

Posted (edited)

 

Hi Unigine Team,

I wrote a simple C# Code of Fracture Body and attached to ObjectMeshDynamic_1 , but does not work in editor(both I tested with C# code and Without C# code) , plus,  I tested a lot parameters of Physics-> Fracture but does not work & this hidden with Broken flag!

Is it possible , Fracture feature does work without code? or I must use C# code? Fracture feature does work in editor or build file (*.exe) ? I must Complex tringles mesh or simple mesh? 

private void Init()
{
Node n = World.GetNodeByName("ObjectMeshDynamic_1");
ObjectMeshDynamic mesh = n as ObjectMeshDynamic;
BodyFracture bf =new BodyFracture(mesh);
bf.CreateShatterPieces(10);
}

image.thumb.png.8398722a0b0e3b178db3f2c4c3f03782.png

image.thumb.png.ef13576f09b1c3b0604992b77b3e62f4.png

Edited by ahmad.karami

Share this post


Link to post

Hello,

Your code is correct, but you need to change the threshold for a fracture body. For example, bf.Threshold = 0.05f. With C# components it only works when you press Run button to run the separate instance of the application.

Share this post


Link to post
Posted (edited)
5 hours ago, karpych11 said:

Hello,

Your code is correct, but you need to change the threshold for a fracture body. For example, bf.Threshold = 0.05f. With C# components it only works when you press Run button to run the separate instance of the application.

thank you so much @karpych11

this is new my code ( does work! )

private void Init()
{
Node n = World.GetNodeByName("ObjectMeshDynamic_1");
ObjectMeshDynamic mesh = n as ObjectMeshDynamic;
BodyFracture bf =new BodyFracture(mesh);
bf.Threshold = 0.02F;
bf.CreateShatterPieces(50);
}

 

image.thumb.png.e1e84a7ce8fa25a1ac0314b778b10a64.png

image.thumb.png.ee6fc9c99189cf8d2b1ec3d9ab331880.png

 

Edited by ahmad.karami

Share this post


Link to post
On 4/28/2020 at 1:00 PM, ahmad.karami said:

thank you so much @karpych11

this is new my code ( does work! )


private void Init()
{
Node n = World.GetNodeByName("ObjectMeshDynamic_1");
ObjectMeshDynamic mesh = n as ObjectMeshDynamic;
BodyFracture bf =new BodyFracture(mesh);
bf.Threshold = 0.02F;
bf.CreateShatterPieces(50);
}

 

image.thumb.png.e1e84a7ce8fa25a1ac0314b778b10a64.png

image.thumb.png.ee6fc9c99189cf8d2b1ec3d9ab331880.png

 

hey @ahmad.karami, im trying to use your code to shatter an object as well but nothing happens i tried this:

using System;
using System.Collections;
using System.Collections.Generic;
using Unigine;

[Component(PropertyGuid = "c94040114fe1fa3cc478906b5a8beace8335f3d5")]
public class Fracture : Component
{
    [ShowInEditor]
    [Parameter(Tooltip = "Object for shattering")]
    private ObjectMeshDynamic mesh = null;

    private void Init()
    {
        // write here code to be called on component initialization
        BodyFracture bf = new BodyFracture(mesh);
        bf.Threshold = 0.02f;
        bf.CreateShatterPieces(50);
    }

    private void Update()
    {
        // write here code to be called before updating each render frame

    }
}

here is the node info:

image.thumb.png.b7baa2eb254008b44775f086fd814224.png

 

and here the physics info:

image.png.6fba7d10b0d1329369b0b2a084a56d80.png

but nothing happens when i simulate or start the game, do you have any suggestions?

 

Share this post


Link to post

Hi Kireita,

To achieve the desired effect for the volume of your box you can try reducing either the number of pieces or the threshold value. This should help.

Thanks!

Share this post


Link to post

Hey @fox

I tried setting the trheshold to 0.01 and the shatter pieces to 2 but nothing happens when i press simulate.

 

but i did manage to shater it when running by adding bf.broken = true to the init()

image.png.f4c4a8a2c96a2460d8bc890567f8fa25.png

but unfortunately the broken pieces don't have the material applied. for some reason.

here is the code:

[Component(PropertyGuid = "c94040114fe1fa3cc478906b5a8beace8335f3d5")]
public class Fracture : Component
{
    [ShowInEditor]
    [Parameter(Tooltip = "Object for shattering")]
    private ObjectMeshDynamic mesh = null;

    [ShowInEditor]
    [Parameter(Tooltip = "Shattered object Material")]
    private Material shatteredMaterial = null;

    BodyFracture bf;
    private void Init()
    {
        // write here code to be called on component initialization
        bf = new BodyFracture(mesh);
        bf.Threshold = 0.01f;
        bf.CreateShatterPieces(2);
        bf.SetMaterial(shatteredMaterial.Name);
        bf.SetSurfaceProperty(shatteredMaterial.Name);
        Unigine.Console.SetPrompt(bf.GetMaterialName());
        bf.Broken = true;
    }

    private void Update()
    {

    }
}

 i made the class so that you can decide the material from the UI:
image.png.28d66a278beede657de9662e872eb840.png

maybe you can help me?

i just need:

  1. to see the mesh shatter when i press the  simulate button image.png.b945d3a9eea900713b70afab4bd3bd54.png
  2. and have the material applied to the shattered pieces

thank you in advance

Share this post


Link to post

Hi Kireita,

Try changing your Init() method as follows:

private void Init()
{
    // write here code to be called on component initialization
    bf = new BodyFracture(mesh);
    bf.Threshold = 0.01f;
    bf.SetMaterial(shatteredMaterial.Name);
    bf.CreateShatterPieces(2);
    bf.Broken = true;

}

Should work, but please note that with C# components it works on clicking the Run button to run the separate instance of the application.

image.png

Thanks!

Share this post


Link to post
Posted (edited)

@fox

Hey i see now what the problem was, i needed to apply the material BEFORE shattering it, instead of after it, thank you!, i did not know the order of the functions actually affected its behavior, thanks!

image.png.07bd479d28ec9621cf8c927fb9027996.png

and i understand now that i need to simulate physics in a separate instance because im using c#.

thank you!

Edited by Kireita

Share this post


Link to post