Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question superposition of two or more 3D rectangles

Discussion in 'Scripting' started by julien83franceschi, May 20, 2024.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    249
    Hi there,

    I would like the grid to update when 2 or more rectangles are superimposed.
    This works perfectly.
    The problem is this: if we do an OnMouseDown on a rectangle then if the 2 rectangles are superimposed, there is a 0 where the rectangles are superimposed (instead of putting a 1).

    Here is my code for the OnMouseDown:

    Code (CSharp):
    1.     void OnMouseDown()
    2.     {
    3.         var changedFigures = Figures.Where(i => i.hasChanged);
    4.  
    5.         if (changedFigures.Count() != 0)
    6.         {
    7.             foreach (var figure in changedFigures)
    8.             {
    9.                 foreach (Transform box in figure)
    10.                 {
    11.                     Vector3 firstGridSquarePosition;
    12.                     firstGridSquarePosition = new Vector3(grilleRouge.objArray[0].transform.position.x, grilleRouge.objArray[0].transform.position.y, grilleRouge.objArray[0].transform.position.z);
    13.                     int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
    14.                     int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
    15.  
    16.                     if (grid[z, x] == 1)
    17.                     {
    18.                         grid[z, x] = 0;
    19.                     }
    20.                 }
    21.             }
    22.         }
    23.     }
    I tried putting the code below in the blue rectangle (to reactivate the 1 in the blue rectangle):
    blueRect.SetActive(true) (with public variable: public GameObject blueRect);
    But this doesn't work.

    Here's a video to help you understand



    If you have any ideas to replace the 0 with a 1,

    thanks for your help,

    A+
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,214
    This answer won't offer a direct response to your question, but I believe that the top-down logic of your system is not really up to the task.

    You have to consider that what you're building consist of two separate realities (let's call 'em that). The first one is the visual, interactive representation with which the user is actually engaged (let's call that the User Interface). And the other is what you use as an invisible ground-level truth (let's call it the Model) which lets you quickly correlate advanced logic without having to ascertain the status of the User Interface every time.

    So what is the benefit here? Well the Model, having just 0s and 1s, is much faster to work with, is robust in how the reality is represented, allows detection of complex states, allows for quick and compressed data load/save etc. However the game's interface is intended to look and feel richer than just 0s and 1s, perhaps you want 3D models, flashy effects, perhaps you want physics, smooth animation etc. And this is why we typically split our game worlds into two "realities".

    However, by doing this, we inevitably run into this problem of synchronization. Because that's what you're doing and what you're failing at. You're keeping the two realities synchronized, and the error in your example is a situation where this synchronization seems to be lost.

    So if you think about this as abstractly as possible, what do you think which approach would generate the least amount of synchronization errors:

    1) by reacting to every possible incremental change, in all kinds of different scenarios, or
    2) by doing one big swoop when the system is ready to be synchronized, which scans and copies a state from one reality onto another?

    In other words, by doing model synchronization from the domain of your User Interface reality, and reacting to punctual events, you're mixing tasks so badly that the synchronization issues simply become inevitable. Your type of solution reminds me of the old cartoons when the wooden ship is sinking, with Duffy Duck sticking his fingers into holes trying to stop the water from flowing, but he has a finite amount of fingers, yet the holes appear from everywhere, so he tries to use his bill as well, ending up in a comic situation.

    I believe this is a better answer than to help you fix this one issue, because you will run again in those issues on the very next corner.
     
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    249
    Thanks for you answer.

    Here's my question: how can I keep the 1 in the blue 3D rectangle if I do an OnMouseDown and remove it from the white 3D rectangle on top of it (in the yellow number array) ?

    Thanks for your help.

    A+
     
  4. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    249
    Here's my new question: how do I find out the coordinates (grid[x, z]) of the cube where the 3D rectangles are superimposed (see image below)?



    Here is my code:
    Code (CSharp):
    1.  
    2.     void OnMouseUp()              //   the pentamino is deposited
    3.     {
    4.         var changedFigures = Figures.Where(i => i.hasChanged);
    5.  
    6.         if (changedFigures.Count() != 0)
    7.         {
    8.             foreach (var figure in changedFigures)
    9.             {
    10.                 foreach (Transform box in figure)
    11.                 {
    12.                     Vector3 firstGridSquarePosition;
    13.                     firstGridSquarePosition = new Vector3(grilleRouge.objArray[0].transform.position.x, grilleRouge.objArray[0].transform.position.y, grilleRouge.objArray[0].transform.position.z);
    14.                     int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
    15.                     int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
    16.  
    17.                     if (grid[z, x] == 0)
    18.                     {
    19.                         grid[z, x] = 1;
    20.                     }
    21.                 }
    22.             }
    23.          }    
    24.       }
    25.  
    26. void OnMouseDown()            //  the pentamino is removed
    27.     {
    28.         var changedFigures = Figures.Where(i => i.hasChanged);
    29.  
    30.         if (changedFigures.Count() != 0)
    31.         {
    32.             foreach (var figure in changedFigures)
    33.             {
    34.                 foreach (Transform box in figure)
    35.                 {
    36.                     Vector3 firstGridSquarePosition;
    37.                     firstGridSquarePosition = new Vector3(grilleRouge.objArray[0].transform.position.x, grilleRouge.objArray[0].transform.position.y, grilleRouge.objArray[0].transform.position.z);
    38.                     int x = Mathf.FloorToInt((box.position.x - (firstGridSquarePosition.x - 0.8f)) / 1.6f);
    39.                     int z = Mathf.FloorToInt((box.position.z - (firstGridSquarePosition.z - 0.8f)) / 1.6f);
    40.  
    41.                     if (grid[z, x] == 1)
    42.                     {
    43.                         grid[z, x] = 0;
    44.                     }
    45.                 }
    46.             }
    47.         }
    48.     }
    49.  
    Thanks in advance for your help,

    A+
     
    Last edited: May 22, 2024
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,657
    I know this isn't a direct answer to your question but I'm hoping you'll take my advice and it'll help. As others have attempted to tell you in some of your posts, you'll continue to suffer from these kinds of problems until you start abstracting things. For instance, you should NOT have these kinds of logical grid position / world position calculations all over your code.

    You should, in the very least, have a few methods that convert between the logical grid positions (presumably always 2D) and their respective (visual) world positions. Having "magic" values like "0.8f" sprinkled around your code is making it even worse and is very confusing. For instance, most devs here don't know what "0.8" means and if it's related to the problem you're having which is likely why you don't always get the answers you need. None of your game logic should know anything about that kind of stuff.

    You've ask lots of questions for your project over the last few months which is perfectly fine however most that I've seen are caused by this lack of abstraction.

    You should have:
    • A "model" of your board (game) state which knows nothing about visuals i.e. position on the screen etc.
    • Conversion methods to convert between the board position and the rendering position and vice versa.
    With those you can build upon them and make your logic much easier.

    Code (CSharp):
    1. firstGridSquarePosition = new Vector3(grilleRouge.objArray[0].transform.position.x, grilleRouge.objArray[0].transform.position.y, grilleRouge.objArray[0].transform.position.z);
    With all due respect, lines like this are an abomination and you need to make things simpler to understand. You're grabbing "objArray[0]" three times, grabbing the transform three times, grabbing the position three times then grabbing the X, Y or Z. All you need to do here is grab the Transform position once. That means:
    Code (CSharp):
    1. firstGridSquarePosition = objArray[0].transform.position;
    As I mentioned above, I'd go further and say you don't even need to be doing my simplified version.

    Unfortunately I have no idea how to answer your question because your code above doesn't make much sense to me. It's a copy of the question you asked here.
     
    spiney199 and orionsyndrome like this.