ahmad.karami Posted April 27, 2020 Posted April 27, 2020 (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); } Edited April 28, 2020 by ahmad.karami
karpych11 Posted April 28, 2020 Posted April 28, 2020 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.
ahmad.karami Posted April 28, 2020 Author Posted April 28, 2020 (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); } Edited April 28, 2020 by ahmad.karami
Kireita Posted July 26, 2020 Posted July 26, 2020 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); } 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: and here the physics info: but nothing happens when i simulate or start the game, do you have any suggestions?
fox Posted July 27, 2020 Posted July 27, 2020 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!
Kireita Posted July 27, 2020 Posted July 27, 2020 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() 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: maybe you can help me? i just need: to see the mesh shatter when i press the simulate button and have the material applied to the shattered pieces thank you in advance
fox Posted July 27, 2020 Posted July 27, 2020 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. Thanks!
Kireita Posted July 27, 2020 Posted July 27, 2020 (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! and i understand now that i need to simulate physics in a separate instance because im using c#. thank you! Edited July 27, 2020 by Kireita
TRaffic Posted yesterday at 06:54 AM Posted yesterday at 06:54 AM (edited) Просьба большая, а можно эти разрушения сделать типа базовых, что бы автоматически при выборе пункта разрушения появлялись, а кому надо тот кодом напишет? Всё вроде так же сделал, но ошибка вылазит, не получается внутренний материал. И что бы сразу не разрушалось, это через дельтатайм код делать? Edited yesterday at 06:55 AM by TRaffic
fox Posted yesterday at 03:38 PM Posted yesterday at 03:38 PM Здравствуйте, @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), настроить нужные параметры и проверить, как работает разрушение. Надеюсь, это будет полезным. 1
TRaffic Posted yesterday at 04:24 PM Posted yesterday at 04:24 PM (edited) Спасибо!!! Да круть!!! вообще, надо в базовую версию вам добавить такую функцию, столько времени мне сэкономили. Edited 2 hours ago by TRaffic
Recommended Posts