0

i made a sim for the Mandelbrot Set function, zn + 1 = power(zn) + c and it work but when i get to the point were the function is unstable it crashes, now i have a boolen that when true makes a wire that connects all the circles, when its false its fine(dosent crash) but when its on it does, the code works like this:

start: building a list of circles and making there pos by the equation, and then crating a wire between the circle and the last circle, update: then when you move the circle it uses the already made list of gameobj to update there pos.

you can try it here: build github: git

but it crashes:(, heres the code:

 private void updateCircles()
{
    StartUpdateCircles();
}


private void StartCircles()
{
    float x = BlackCircle.anchoredPosition.x;
    float y = BlackCircle.anchoredPosition.y;
    AllCircles.Add(BlackCircle.gameObject);

    for (int i = 1; i < itarations; i++)
    {
        Vector2 RedCircleVec2 = RedCircle.anchoredPosition;
        Vector2 LastCircleVec2 = AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition;
        GameObject Circle = Instantiate(BlackCircle.gameObject, Vector3.zero, Quaternion.identity);

        Circle.transform.SetParent(CanvasPerent);
        AllCircles.Add(Circle);

        x = Mathf.Pow(x, 2);
        x -= Mathf.Pow(LastCircleVec2.y, 2);
        x += RedCircleVec2.x;

        y = (2 * LastCircleVec2.x
            * LastCircleVec2.y) + RedCircleVec2.y;

        Circle.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);

        if (HasWire)
        {
             GameObject wire = GenrateWireStart(LastCircleVec2
           , Circle.GetComponent<RectTransform>().anchoredPosition);

            AllWires.Add(wire);
        }

    }

}

private void StartUpdateCircles()
{
    float x = BlackCircle.anchoredPosition.x;
    float y = BlackCircle.anchoredPosition.y;
    for (int i = 1; i < itarations; i++)
    {
        Vector2 RedCircleVec2 = RedCircle.anchoredPosition;
        Vector2 LastCircleVec2 = AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition;
        RectTransform ICircle = AllCircles[i].GetComponent<RectTransform>();


        x = Mathf.Pow(x, 2);
        x -= Mathf.Pow(LastCircleVec2.y, 2);
        x += RedCircleVec2.x;

        y = (2 * LastCircleVec2.x
            * LastCircleVec2.y) + RedCircleVec2.y;

        ICircle.anchoredPosition = new Vector2(x, y);
        if (HasWire)
        {
            
              GenrateWireUpdate(LastCircleVec2
                ,ICircle.anchoredPosition, i);
            
            
        }
    }
}

public GameObject GenrateWireStart(Vector2 NodeA, Vector2 NodeB)
{
    GameObject Connector = new GameObject("connector", typeof(Image));
    Connector.transform.SetParent(CanvasPerent);
    RectTransform ConnectorRT = Connector.GetComponent<RectTransform>();

    ConnectorRT.anchorMin = new Vector2(0, 0);
    ConnectorRT.anchorMax = new Vector2(0, 0);

    Connector.GetComponent<Image>().color = new Color(0f, 0f, 0f, 0.25f);

    Vector2 dir = (NodeB - NodeA).normalized;
    float distance = Vector2.Distance(NodeA, NodeB);

    ConnectorRT.sizeDelta = new Vector2(distance, 0.005f);

    ConnectorRT.position = NodeA + dir * distance * .5f;

    ConnectorRT.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir));

    return Connector;
}

public void GenrateWireUpdate(Vector2 NodeA, Vector2 NodeB, int i)
{
    RectTransform ConnectorRT = AllWires[i - 1].GetComponent<RectTransform>();
    Vector2 dir = (NodeB - NodeA).normalized;
    float distance = Vector2.Distance(NodeA, NodeB);
    ConnectorRT.sizeDelta = new Vector2(distance, 0.005f);
    ConnectorRT.position = NodeA + dir * distance * .5f;
    ConnectorRT.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir));
}

pls help, thank you.

the project

12
  • Could you add how exactly you call these methods? They are all private and no Unity messages so I guess it might be relevant to see how you call them. Also by "crashing" you mean it freezes or do you get any errors? Commented Feb 28, 2022 at 7:55
  • If you post a link to your build .. could you explain what we are looking at, how we are supposed to interact with it and how exactly your issue is reproduced? Commented Feb 28, 2022 at 7:57
  • 1
    What I can say for now you are getting an error An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was: RuntimeError: integer overflow which sounds pretty self-explanatory: Some int is growing bigger than 2147483647 .. I think you should rather debug your code with breakpoints and check when exactly this happens in the editor where you can still fully debug ;) Commented Feb 28, 2022 at 8:18
  • mmm, okay thanks for the idea, i will chack to see if i can fix the issue Commented Feb 28, 2022 at 9:18
  • 1
    In general assuming this is a Unity project you are not allowed to rename the Assets folder to something else ;) Commented Feb 28, 2022 at 9:53

1 Answer 1

1

I looked briefly into your code and you seem to get some invalid positions like infinite / undefined from your calculations or just some positions too far away for Unity.

I could remove these by simply limiting positions to e.g.

 x = Mathf.Clamp(Mathf.Pow(x, 2), -Screen.width, Screen.width);
 x = Mathf.Clamp(x - Mathf.Pow(LastCircleVec2.y, 2), -Screen.width, Screen.width);
 x = Mathf.Clamp(x + RedCircleVec2.x, -Screen.width, Screen.width);

 y = Mathf.Clamp((2 * LastCircleVec2.x * LastCircleVec2.y) + RedCircleVec2.y, -Screen.width, Screen.width);

which simply limits all positions to some off-screen max positions

Sign up to request clarification or add additional context in comments.

6 Comments

thanks man, it works now!, much apricated!
so you basically made it go only to a certain range? before it was going infinite and it caused the overflow?
@billthecomputerwiz exactly .. I clamped it at an arbitrary value .. just making sure for now it is somewhere out of the screen to not look awkward ;)
@billthecomputerwiz I would have some refactoring suggestions and fixes for the drag and animator behavior btw ;) I created a PR you can take it or ignore it ^^
thanks, i will implement some of your code, just curious, have you ever programed this function or the Mandelbrot set?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.