Jump to content

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


photo

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
Posted

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.

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
  • 2 months later...
Posted
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?

 

Posted

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!

Posted

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

Posted

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!

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
  • 4 years later...
Posted (edited)

Просьба большая, а можно эти разрушения сделать типа базовых, что бы автоматически при выборе пункта разрушения появлялись, а кому надо тот кодом напишет?

Всё вроде так же сделал, но ошибка вылазит, не получается внутренний материал.

И что бы сразу не разрушалось, это через дельтатайм код делать?

37.jpg

200.jpg

Edited by TRaffic
Posted

Здравствуйте, @TRaffic!

С тех пор API немного изменилось (актуальное описание и примеры можно посмотреть в статье про класс BodyFracture).

Вот простенький пример компонента с выбором типа разрушения и таймером:

 

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

[Component(PropertyGuid = "AUTOGEN_GUID")]//<-- Здесь должен быть guid Вашего компонента
public class Fracture : Component
{
    // declare fracture types
    private enum FractureType
    {
        Shatter,
        Slice,
        Crack
    }
	
    [ShowInEditor][ParameterSwitch(Items = "Shatter, Slice, Crack", Tooltip = "Fracture type to be used")]
    private FractureType fracture_type = 0;

    [ShowInEditor]
    [Parameter(Tooltip = "Broken face material")]
    private Material brokenMaterial = null;
	
    [ShowInEditor][Parameter(Tooltip = "Object's life time (in seconds)")]
    private float lifeTime = 1.0f;

    private BodyFracture bf = null;
    private float startTime = 0.0f;
    private void Init()
    {
        if (node.Type != Node.TYPE.OBJECT_MESH_DYNAMIC)
        {
            Log.Error("This component must be assigned to Dynamic Mesh objects only!\n This node's type is: {0}", node.TypeName);
            return;
        }

        bf = new BodyFracture(node as ObjectMeshDynamic);
        bf.Threshold = 0.01f;
        bf.Material = brokenMaterial;
        
        Unigine.Console.Prompt  = bf.Material.FilePath;
        // remember initialization time of an object
        startTime = Game.Time;	
    }

    private void Update()
    {
        // wait until the lifetime ends and break the object
        if (bf && !bf.Broken && Game.Time - startTime > lifeTime)
        {
            // switch fracture type
            switch (fracture_type){
                case FractureType.Shatter: bf.CreateShatterPieces(3); break;
                case FractureType.Slice: bf.CreateSlicePieces(bf.Transform * vec3.ZERO, vec3.ONE); break;
                case FractureType.Crack: bf.CreateCrackPieces(bf.Transform * vec3.ZERO, vec3.ONE, 7, 3, 0.1f); break;
            }
            
            bf.Broken = true;
            Log.Message("The object is broken!");
        }
    }
}

Компонент можно назначить на Dynamic Mesh (ObjectMeshDynamic), настроить нужные параметры и проверить, как работает разрушение.

image.png

Надеюсь, это будет полезным.

  • Thanks 1
Posted (edited)

Спасибо!!! Да круть!!! вообще, надо в базовую версию вам добавить такую функцию, столько времени мне сэкономили.

Edited by TRaffic
×
×
  • Create New...