Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Grid Framework [scripting and editor plugins]

Discussion in 'Assets and Asset Store' started by hiphish, Jul 24, 2012.

  1. quarzwar

    quarzwar

    Joined:
    Jul 14, 2012
    Posts:
    13
    Hello,
    I have just a quick question about Grid Framework!
    Is it possible to "wrap" the grid around, lets say a sphere?
    For example to make a grid around a planet in a space game?
    Thanks in advance.
    Jesper W. Nelsen.
     
  2. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm afraid that can't be done visually, grids are fixed in their shape. Mathematically though it should be possible to map the rectangular grid's coordinates to the planets surface, but there are so many edge cases (like what coordinates do the poles have?) you would be pretty much writing your own Grid Framework at that point. Spherical grids are something I'm keeping in the back of my head, but it's not something I plan to do anytime soon.

    In unrelated news, the update that adds Playmaker support has just been submitted, it should get approved in a few days.
     
  3. PhilDupont

    PhilDupont

    Joined:
    Nov 24, 2013
    Posts:
    6
    Having a bit of a headache... can't seem to sort this one out... could be (ok, definitely is) something with my setup, but I can't keep the grid centered on one of my objects.

    I have the default GFRect Grid script added to my starship object and other than expanding the grid size a bit and using the offset to center the grid...

    But, when I play and rotate the ship... the grid is no longer centered and attached.

    Any ideas?
     

    Attached Files:

  4. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I assume your grid is a child of the ship? If so, then the grid, like all children, will inherit its parent's rotation. In this case you can either use a script that moves the grid's position to the ships position and don't use parenting, or have third (empty) object that's the parent of both the ship and the grid, making the ship and grid siblings. Then whenever you want to move the ship in your movement code you instead move the parent and when you want to rotate the ship you rotate the ship. That way both the ship and the grid inherit the parent's position, but the ship retains its own rotation.

    EDIT: Oops, I misread your post, you have the grid component on the ship object? In that case I would recommend placing the grid on its own GameObject.
     
    Last edited: Apr 29, 2014
  5. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Actually it has been up for almost a month already, but I have been busy. Anyway, this is the long-awaited release that brings PlayMaker support to Grid Framework, so here is the change log:

    • Introducing Playmaker support: Almost the entire Grid Framework API can no be used as Playmaker actions (some parts of the API are ouside the capabilies of Playmaker for now)
    • Updated the documentation to include a chapter about Playmaker and how to write your own Grid Framework actions.
    • Fixed: the origin offset resetting every time after exiting play mode.
    • Fixed compilation error in one of the Playmaker actions (setter and getter for depth of layered grids)

    If you're wondering why I skipped 1.4.0, it's because I discovered a bug right after submission, so I submitted 1.4.1 right afterwards, thus skipping over the release of 1.4.0. As always you can read about how to use the new PlayMaker actions or write your own ones in the documentation.

    As for future plans for Grid Framework, I still have a place that I want to optimise before considering new features, and it will require a good amount of digging, but the result will be cleaner and easier to maintain code.
     
  6. zipben

    zipben

    Joined:
    Oct 19, 2013
    Posts:
    2
    Hi hiphish

    I love the plugin. It has been a god send, but recently I started having a very strange issue where the grid allign panel wont open. It appears in the window menu and I don't get any errors but for some reason when I click on it is simply doesn't open. Any suggestions would be much appreciated. Thanks again for the awesome plugin.
     
  7. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    It's working on my end. Have you tried re-importing Grid Framework? Also, it won't open if there already is an instance open, have you make sure there is no tab of it already somewhere in your editor? You can reset your editor layout to one of the defaults using Window -> Layouts.
     
  8. zipben

    zipben

    Joined:
    Oct 19, 2013
    Posts:
    2
    I was able to get it working. I think what happened was my layout crashed and it corrupted something. Tried reloading the project and it still wouldnt come up. I ended up just creating a new project and moving things over. It seems to be working properly now.

    Thanks for the quick reply.
     
  9. CraigGraff

    CraigGraff

    Joined:
    May 7, 2013
    Posts:
    44
    Does your grid framework offer any tools for traversing grid vertices?
    I am currently trying to allow the user to paint along between vertices, with the condition that lines may intersect at a point but never have overlapping segments.
     
  10. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Sorry for the late reply. There is nothing built in because it would be too specific, but it would be really simple to write: assuming you are using the mouse pointer use NearestVertexW to get the world coordinates of the nearest vertex. Keep tracks of your line's segments and every time you want to add a new vertex construct a new segment and see if it is already in the list. If it is you have an overlatting segment. Here is a code draft:
    Code (csharp):
    1.  
    2. struct Segment {
    3.     public Vector3 startPoint;
    4.     public Vector3 endPoint;
    5.  
    6.     public Segment(Vector3 start, Vector3 end) {
    7.         startPoint = start;
    8.         endPoint = end;
    9.     }
    10. }
    11.  
    12. List<Segment> segmentList; // store your line segments here
    13. GFGrid grid; // the grid you use
    14.  
    15. // use this in your update function
    16. Vector3 cursorPosition; // position of your cursor
    17. Vector3 vertex = grid.NearestVertexW(cursorPosition);
    18. Vector3 previousVertex = segmentList.Last().endPoint;
    19. if (vertex == previousVertex)
    20.     return; // we are still pointing to the same vertex as last frame
    21.  
    22. Segment newSegment = new Segment(previousVertex, vertex);
    23. Segment inverseSegment = new Segment(vertex, previousVertex);
    24.  
    25. if (segmentList.Contains(newSegment) || segmentList.Contains(inverseSegment) {
    26.     // overlapping segments!
    27. } else {
    28.     segmentList.Add(newSegment);
    29. }
    30.  
    We'll use a new structure to define line segments and keep track of them in a list. Every time we consider a new segment we check if it is already in the list. We could also store the vertices instead of pairs of vertices to save memory, but it will be more complicated because a vertex may appear twice in the list. We must also check the possibility of an inverse segment, because that would be illegal as well. If you're good with C# you could write your own comparison routine instead of creating an inverse segment, but if your lines aren't going to get huge it might not be worth the effort.
     
    Last edited: May 18, 2014
  11. Simmo76

    Simmo76

    Joined:
    Oct 17, 2012
    Posts:
    31
    Hi there,

    Is there an option in the grid framework to draw the red/green/blue transform arrows at runtime? (as per your main screenshot).

    Thanks,
    Simon
     
  12. PhilDupont

    PhilDupont

    Joined:
    Nov 24, 2013
    Posts:
    6
    Sorry for the delay, got tied up with life events...

    I tried moving it to a separate GameObject as a sub of the ship object - same effect (even with the new version).

    Any other ideas?

    i could set it to it's own object not as a sub to the ship object and mirror the position changes to this object as you suggest... but that seems really messy... Plus in the future we'd like to use this AS the ship (really a collection of objects aligned on the grid as the ship)... My concern is that the problem would continue.
     
    Last edited: May 20, 2014
  13. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Sorry, there isn't, but if you want the vectors for the arrows, they can acessed like this:
    Code (csharp):
    1. GFRectGrid grid;
    2. // up arrow
    3. grid.up;
    4. // right arrow
    5. grid.right;
    6. //forward arrow
    7. grid.forward;
    8.  
    It's the same as with Unity's Transform:
    http://docs.unity3d.com/Documentation/ScriptReference/Transform-up.html

    If you make the grid object a child of the ship it will inherit the ship's rotation. The question is if the ship's rotation is really important or just aesthetic. If it is the latter a hierarchy like this would be best:
    Code (csharp):
    1.  
    2. motor      // <--translate this
    3.   |-ship   // <--rotate this
    4.   |-grid
    5. // motor is the parent, ship and grid are the motor's children and each other's siblings
    6.  
    That way both the grid and the ship will inherit the motor's position, but the ship will maintain its own rotation. Don't worry, this is not messy, it is the way 3D games are done. For example, if you have a "person" model it is composed of several smaller models. You move the whole composition to move your character and in addition you move the individual parts to create an interesting animation (like walking or attacking).
     
  14. PhilDupont

    PhilDupont

    Joined:
    Nov 24, 2013
    Posts:
    6
    Sorry, I think I may not have been all that clear with what the issue is and I'm causing some confusion.

    I'm using child/parent hierarchies to nest my objects and inherit it's properties. No problem there.

    The issue I'm having relates to how the grid object handles the origin offset. It appears to rotate on the origin point rather than the origin offset. I made a video that demonstrates what I'm doing and the problem

     
  15. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Now I see. This is going to be a nasty one, the rotation is not handled the grid, but by the Object's Transform. I'll keep you updated once I find a solution.
     
  16. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    Good news, I have managed to isolate and fix the issue:


    There is still some work to be done so that the points always update when needed, but I should have it running in the next few days. If you want I can then send you a "hotfixed" version of the scripts. This required some deep changes, so hexagonal and polar grids will need to be revised as well, that's why I won't be able to publish an update immediately. If you need it for your work and you only need rectangular grids to work the hotfix should be enough to allow you to keep working until the proper update is released. (I cannot send PMs to you, so you will have to PM me if you want me to send you the code)
     
  17. PhilDupont

    PhilDupont

    Joined:
    Nov 24, 2013
    Posts:
    6
    Wow. That's a quick turn around. Yeah I only need rectangular grids so the rest aren't critical to my work.

    I'll PM you my address now.

    Thanks for looking into this.

    PhilD
     
  18. meapps

    meapps

    Joined:
    May 21, 2013
    Posts:
    167
    @hiphish i would need a grid movement for my game that works also with physics 2D etc. For the Player. Would this work with your plugin? I having a hard time to get the grid movement working so far...
     
  19. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I need to know more about how you want to move the player, i.e. what information does your code need to move. Do you need a direction vector? A destination position? Do you need to compute coordinates?
     
  20. Dun1031

    Dun1031

    Joined:
    Mar 22, 2012
    Posts:
    5
    @hiphish

    I am working with your sliding puzzle example and while the code works wonderful for movement I am having an issue with piece pass through. If I move a piece into the corner of another piece I can push that piece through and let it land on top of the piece.

    Do you have any suggestions on how to stop pieces from being pushed onto occupied squares?

    Screenshots attached.

    Thanks,

    Dun
     

    Attached Files:

  21. travillaintim

    travillaintim

    Joined:
    May 28, 2013
    Posts:
    4
    GridinGame.png EditorGameHex.png working with Grid framework.

    Setting up a hexgrid up to match background image in unity editor this is no problem (see the editor image). However when game is run the, grid "resizes" - see game image. I need help to stop it doing this. (ignore my pm btw) Not sure why this is happening

    edit would help to upload image showing the problem... :)
     
    Last edited: Jul 2, 2014
  22. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hello guys, I am really sorry i haven't responded to any posts; the Unity software failed to notify me, it must be because of the update to the forums. I'll get to your issues right away...
     
  23. 99thmonkey

    99thmonkey

    Joined:
    Aug 10, 2012
    Posts:
    525
    With the sliding puzzle example, can you move the pieces via Mobile Touch or Mouse click and drag?
     
  24. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    The problem is that diagonal movement is allowed. This makes it possible for the corner of your block to slightly overlap the corner of a static block. If you keep doing this you can hit an angle where the blocks can overlap to such an extent they pass through each other. Once that has happened the next time you move your block the overlapped part becomes zero in the matrix and counts as not occupied and can be moved through freely at any angle.

    The easiest solution would be to disable diagonal movement: take the vector of the player's movement and see which component is the largest, than cancel the other away. Most of the time players won't be able to do diagonal movement anyway and that's how sliding games usually do it.
     
  25. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    What member of the grid is it exactly that resizes (size, renderFrom, radius, ...)? If you tell me it can probably be solved very easily, that sort of thing can happen if I miss a `[SerializeField]` attribute.
     
  26. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    mouse-drag is the way it is done in the example, but you'll have to implement touch-dragging yourself. The way it works is if the player clicks the block's collider (OnMouseDown) a function is called that sets a bunch of flags and variables, and when the mouse button is released (OnMouseUp) andother set of functions is called and flags are set.
    Code (csharp):
    1. voidOnMouseDown(){
    2. // code here
    3. }
    4.  
    5. voidOnMouseUp(){
    6. // code here }
    7.  
    8. voidFixedUpdate () {
    9. if(beingDragged)
    10. Drag();
    11. }
    You'd have to replace the mouse-specific parts with touch-specific parts. The pussle logic is isolated in the `Drag()` function and doesn't care how you move the block.
     
  27. travillaintim

    travillaintim

    Joined:
    May 28, 2013
    Posts:
    4
    none of them - all the values in GFHexGrid stay the same according to the inspector. Ahh I think it has something to do with NGUI that is a drag...

    Yeah turns out having the grid as a child or attached to a child under UIRoot causes scaling problems as UIRoot scales per the screen or pixel perfect. neither work. Removing UIRoot fixed problem
     
    Last edited: Jul 5, 2014
  28. Dun1031

    Dun1031

    Joined:
    Mar 22, 2012
    Posts:
    5
    We like that you can clip the corner of the piece, it allows for better movement in our puzzle game. I am thinking I will just have to figure out a way to stop the inward movement when clipping the corner of an occupied square (perhaps distance from center of piece to the center of the occupied square?).

    Jason
     
    Last edited: Jul 5, 2014
  29. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Hey all!

    First up, this looks like a great framework, so nice job! I'm think I'm gonna pull the trigger on purchasing this, but have 2 quick questions which have likely been answered before:

    1) I assume you can store data for each grid tile? Take your typical city building game. If I place a building over 4 tiles on the grid, is it possible to store data on those tiles to indicate they are occupied? (obvious I know, but I haven't seen any mention of this and believe the API isn't available unless you purchase it).

    2) What is the performance like for this? Is this suitable for mobile games and/or are there any optimisations you would recommend for use on mobile.

    Thanks, and nice job!
     
  30. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    The API is not public, but I can send you a link if you wish, I just don't like it floating around in public. To answer your first question, no you cannot store any data inside a grid, a grid is just a mathematical construct. This is an intentional design decision: grids are infinitely large, and even if they weren't, what type of data would they store? If I picked any specific type it would be too restraining and if I allowed any type it would be impossible for the compiler to optimise.

    However, you can still store data somewhere else and that's a pattern I make very frequent use of in the examples. Say you want a 20 x 20 x 1 grid and you want to store only if the space is occupied or free. In that case we would use a two-dimensional bool matrix and set the grid's renderFrom to (0,0,0) and renderTo to (20, 20, 0). We could then write code like this:
    Code (csharp):
    1. GFGrid grid; // reference to the grid object
    2. bool[,] occupationMatrix;
    3.  
    4. void BuildMatrix() {
    5.     // get the matrix size from the grid directly
    6.     int width = grid.renderTo.x - grid.renderFrom.x;
    7.     int height = grid.renderTo.y - grid.renderFrom.y;
    8.  
    9.     occupationMatrix = new bool[width, height];
    10. }
    11.  
    You can then use an object's grid coordinates to inspect its position in the matrix:
    Code (csharp):
    1. int[] matrixCoordinates = new int[2] {grid.WorldToGrid(object.transform.position).x, grid.WorldToGrid(object.transform.position).y};
    (of course you should use proper rounding and possibly store the result of `WorldToGrid` in a variable, but that's you choice)

    As for performance there is little to worry about. The grid is infinite in size, but as a mathematical construct it only needs a handful of parameters (rectangular grids for example only need a spacing to be well-defined, that's one Vector3). Where it can get problematic is rendering: to render a grid a bunch of draw points are generated and sent to the GL class of Unity. If your grid is very large the amount of points grows very quickly in the magnitude of O(n^3).

    For regular use-cases this is really only a concern if your grid stretches far beyond the camera and a simple and incredibly powerful solution is to fake a large grid: make your grid only stretch slightly beyond the camera, there should be no noticeable performance impact. Only when the camera moves the grid's `renderFrom` and `renderTo` are updated to maintain the illusion. Also, as long as nothing about the grid changes the points can be recycled, skipping their generation. I have an example included where I use that trick, and it's unnoticeable to the player and has no performance impact. Since the grid itself is infinitely large any calculations done outside the rendering range work as usual. The rendering is only a finite subset of the entire grid.
     
  31. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Thanks for the great answer! I really like your approach for storing the data, thanks for excellent examples!

    I can understand why you want to keep the API private, so that is not a problem. I shall be purchasing this over the next couple of days so I can wait to get it then :)

    Thank you!
     
  32. xabiusa

    xabiusa

    Joined:
    Jul 1, 2014
    Posts:
    4
    I want check my drag object if are in the correct coordinates. I used drag and snap option. How would you do it?

    If I put triggers won't let me put the object

    ty
     
  33. Mwied

    Mwied

    Joined:
    Jul 16, 2014
    Posts:
    2
    I really like framework, but I find it hard to get real info on the Playmaker integration. are all actions working ?, i see them, and stuff like color change of the grid is working fine, but from alot of the actions i get no responce. My goal was getting the Vector3 of the nearest center of a grid stored in a variable in Playmaker.
     
  34. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I'm afraid I don't understand what exactly you mean. Can you please be more articulate about what you are trying to achieve and what the part is that you're struggling with.

    The actions are very barebones intentionally and mimic the API. This means that you have to give the actions the same kind of parameters you would give a corresponding method. To find the nearest face in the grid relative to a certain point you must first give that point to the action. This point, a Vector3, can either be given manually or passed from another action or variable.

    However, there is a problem when it comes to the nearest face action: The face-plane is specified via an enumeration in script, but Playmaker does not support enumerations. Rather than delaying Playmaker support indefinitely because of a few actions I figured it would be better to ship what was possible and then add the missing bits when Playmaker supports them.

    Instead of the nearest face you can use the nearest box, which is similar, but the "face" coordinate won't be on the grid-plane, it will be between (see the manual). Depending on your needs you might have to force that one coordinate into a fixed value then.
     
  35. xabiusa

    xabiusa

    Joined:
    Jul 1, 2014
    Posts:
    4
    Sorry hiphish for my explanation.( I hope you understant )
    I want to make a game that instantiate x objects in the grid, not how to do to instantiate objects and link it to the grid to pick up and move them around the grid (instantiate objects but I can not move them around the grid, because they are not linkeadas and no achievement link it after instantiating objects). After achieving this, move objects by dragging grid (drag and snap option) and place them on the grid positions x, and I check if the positions of the objects are correct. I tried to do it with triggers colliders snap box but not let me put up the objects instantiated (ignoring collision? Have not succeeded)

    The player has to put the objects in the corresponding positions to pass the game.

    PD: sorry for my English
     

    Attached Files:

    Last edited: Jul 30, 2014
  36. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    You have to convert between grid-and world space coordinates. The following two grid methods do that:
    Code (csharp):
    1. GFGrid grid; // this it the grid you use
    2. Vector3 worldPosition; // position in world coordinates
    3. Vector3 gridPosition; // position in grid coordinates
    4.  
    5. worldPosition = grid.GridToWorld(gridPosition);
    6. gridPosition = grid.WorldToGrid(worldPosition);
    There are many ways to mouse-drag objects in Unity, the most common one is to cast a ray through the camera along the mouse cursor and see where it hits a certain collider. That collider can be a simple plane collider that lies on the grid. Then use that hit position as your destination position.

    Snapping an object is simple: when the player releases the mouse button run the following snippet:
    Code (csharp):
    1. Transform object; // the transform of the object that was moved
    2. grid.AlignTransform(object);
    This will snap the object into position. You can also run it during every frame while the player is dragging the object, if you don't want smooth movement.

    To check whether a position is the desired target just compare coordinates:
    Code (csharp):
    1. Vector3 position;
    2. Vector3 target;
    3. if ((position-target).sqrMagnitude < 0.001) {
    4.     // correct position
    5. } else {
    6.     // wrong position
    7. }
    Make sure your points are both in the same coordinate space, i.e. either world space or grid space. World space is preferable, because everything eventually is transferred back to world space, so you don't have to convert the target to world space every time you perform the check.
     
  37. xabiusa

    xabiusa

    Joined:
    Jul 1, 2014
    Posts:
    4
    Thank you hiphish!

    Other question, I can control the position of grid? the rectangle position
     

    Attached Files:

    Last edited: Aug 1, 2014
  38. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    The position of the grid is the position of its gameObject. Click the grid in the editor and move it like any other object. To move it in code use the object's Transform, just as with any other component type in Unity:
    Code (csharp):
    1. GFGrid grid;
    2. // move three units right
    3. grid.transform.position += new Vector3(3.0f, 0, 0);
     
  39. xabiusa

    xabiusa

    Joined:
    Jul 1, 2014
    Posts:
    4
    Thanks so much for all of your help, hiphish! I have achieved :)
     
  40. bizilux1

    bizilux1

    Joined:
    Jun 6, 2013
    Posts:
    151
    i have a question, can this be used to make a 2D game?

    and if so, can it be used to make a game similar to prison architect? http://www.introversion.co.uk/prisonarchitect/

    where you place objects and walls on grid, some objects are 1x1, some are 1x2, some 3x3 and so on. but grid is always the same ofcourse. (each square on grid is always 1x1)

    and then if this is possible, my final question would be, does it work with any pathfinding solution out there? Because i do not need grid-like movement, i only need grid to place objects and walls. but i do need pathfinding to know where obstacles are (which grid slots are taken, so that pathfinding can dynamically change based on which grid slots are free and which are occupied with objects)
     
    Last edited: Aug 9, 2014
  41. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    Do you store the vertex points of a face/box/square anywhere? I can calculate them easily enough but it seems a duplication of effort if they are already accessible somewhere I'm not noticing.
     
  42. NiscyStoy

    NiscyStoy

    Joined:
    May 31, 2013
    Posts:
    1
    Hi hiphish,
    Once again thank you for making this framework, it has helped me achieve in Unity what I once though was un-achievable (for me at least)! I have been working on remaking an Asian game from the early 2000's that I enjoyed very much, just for a learning experience and to see if it could be achieved in Unity.

    I have read through this entire forum thread to see if I could find the answer to my issue, some posts touch on the subject but not in enough detail for me to understand.

    Basically, I am looking to rotate my camera to give an Isometric viewpoint. Currently I have my camera top down onto the grid, I move my character by left clicking and I have coded it so that it finds the nearest grid square to my mouse's x and y co-ords in grid-space (by using your WorldtoGrid method) and moves my transform to the center of that square in the grid. This works perfectly, but if I rotate my camera or even the grid to an isometric viewpoint the movement breaks and my character moves to random positions on the grid when I click in squares. I am guessing it is because I am raycasting from the camera? But I'm not sure.

    In this link: http://gamedevelopment.tutsplus.com...ds-a-primer-for-game-developers--gamedev-6511 - the first two images represent the most what I am trying to achieve - but whether I rotate the camera, the grid or both - the movement breaks.

    Any help would be greatly appreciated and will set me up to achieve what I have my mind set on. I have the latest Unity and the latest GridFramework and I am not doing 2D isometric.

    Also how close are you to finalizing the path-finding? :) That would be most awesome!
     
  43. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Hi,

    Yes, it can be used for 2D games, simply ignore the third dimension as usually when making 2D games in Unity. "Making" a game like prison architect depends on what you mean by "making". Snapping objects of different size is already built into the API, you'll just have to hook up to it. How the player's cursor input is translated to in-game world coordinates is a general case that's outside the scope of Grid Framework, so that's up to you, but it's simple enough in unity (cast a ray through the camera, see where it hits a collider and that's your point, there's an example that does that included).

    Pathfinding is sadly not a part of Grid Framework yet, there is still other work to be done before I can tackle that task and I don't have any ETA. There are other solutions out there and it should be possible to hook them up to Grid Framework's grids, but that's an area I haven't yet looked into, so don't take my word as fact.

    No, there is no data stored. Doing so would break with the constant-time requirement of the grids: every computation has to be doable in constant time. That way my grids are always infinitely large and it doesn't matter for performance how far away from the origin you are.

    computing a set of vertices is simple enough though, just loop through a given range and convert grid coordinates to world coordinates:
    Code (csharp):
    1.  
    2. GFGrid grid;
    3. Vector3[,,](Vector3 start, int xSteps, int ySteps, int zSteps) {
    4.     Vector3[,,] vertices = new Vector3[xSteps, ySteps, zSteps];
    5.     for (int i = 0; i < xSteps; ++i) {
    6.         for (int j = 0; j < ySteps; ++j) {
    7.             for (int k = 0; k < zSteps; ++z) {
    8.                 vertices[i, j, k] = grid.GridToWorld(start + new Vector3(i, j, k));
    9.             }
    10.         }
    11.     }
    12.     return vertices;
    13. }
    14.  
    If your start is in world coordinates already you can forgo the conversion. Instead of adding the offset like this there are direction vector for each grid. For example, the rectangular grid has `right`, `up` and `forward` vectors that always point into the grid's directions (world space) and are scaled to the grid.

    That's hard to answer without knowing how exactly your setup looks like. The best idea is to prototype your game using a regular top-down perspective, not rotation to the grid or camera. Then, when it works, rotate your camera, not the grid. Here is how I do it in the snapping units example:
    Code (csharp):
    1. //shoots a ray, which can only hit the grid plane, from the mouse cursor via the camera and returns the hit position
    2. Vector3ShootRay () {
    3. RaycastHit hit;
    4. gridCollider.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), outhit, Mathf.Infinity);
    5. //this is where the player's cursor is pointing towards (if nothing was hit return the current position => no movement)
    6. return hit.collider != null ? hit.point : cachedTransform.position;
    7. }
    The variable gridCollider is a special collider that's placed to match up with the grid. The result is in world coordinates and there is usually no reason to convert it to grid coordinates if you want to move your objects to it.
     
  44. fabianom

    fabianom

    Joined:
    Aug 17, 2014
    Posts:
    3
    Why I can´t see the grid when I run my game? The grid is only visible during editing mode and the option to hide the grid on play is disabled.
     
  45. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Have you attached the rendering script to your main camera? Select your camera in the editor, then go to Component -> Grid Framework -> Camera -> GFGridRenderCamera.
     
  46. gravyleaves

    gravyleaves

    Joined:
    Mar 19, 2014
    Posts:
    28
    I'm having trouble with "grid scale transform". It appears one of the triangles is too big or out of alignment (randomly guessing).


    I've disabled two cubes to try to illustrate what's going on better. I can just scale the object directly then align the transform but it doesn't look 100% perfect, especially with smaller grids. If I enable 'rotate' on "grid align transform" before (or after) scaling, it covers up the grid lines entirely.
     
    Last edited: Aug 19, 2014
  47. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Looks like Z-fighting to me. When pixels have the same depth it is undefined which one is supposed to cover the other, so the GPU will pick one seemingly at random. Try moving the grid away form the cubes just a tiny bit. To prevent the cubes from snapping right back use two grids, one for snapping and one for display. Make the one for display hidden in the editor and rendered at runtime and the gameplay grid drawn in the editor and not rendered at runtime. A grid that's not visible will not impact performance in any way.
     
  48. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    Shearing

    As I mentioned last time the upcoming release will be a maintenance release with some much-needed cleanup. While I'm still waiting for approval I have been working on a new feature for rectangular grids that I had wanted to do for quite a while now, but couldn't until the cleanup: I'm talking about shearing.

    This is an un-rotated rectangular grid with skewed axes to create the popular 2:1 isometric look. This is quite powerful, because it allows for isometric 2D graphics to be used with Grid Framework. Getting an isometric look in a 3D game is very simple, you just rotate the camera until it looks right. However, in a 2D game the camera has to be perpendicular to the image plane and objects are just drawn as if they were at an angle, so rotation is out of question.

    Shearing is fully implemented, I just need to write the documentation for it. It works with arbitrary numbers and for all axes along any other axis, so you can shear your grids any way you want. It will be a feature for the 1.5 update once the upcoming 1.4.2 release gets approved.
     
  49. andrewow

    andrewow

    Joined:
    Dec 2, 2013
    Posts:
    13
    This framework is great. Do you have any guidance on how you would modify the sliding puzzle example to include pieces that aren't rectangles? (i.e. tetris pieces)?
     
  50. hiphish

    hiphish

    Joined:
    Nov 13, 2010
    Posts:
    626
    I would use the delegation pattern: Have one "delegate" object, that's the one that manages everything. Instead of having Tetris pieces as single objects make them composed from small cubes. Each cube should hold a list of its sibling cubes that form the complete piece. When the player clicks a cube the cube will ask the delegate "hey, the player wants to move me over there, is that OK?", then the delegate would look at the cube and all its siblings (the cube would also pass the list of siblings to the delegate) and sees if their future position is OK. If it is, then the delegate moves the cubes, the cubes don't move themselves. If it isn't OK then the delegate make a sound or does nothing or whatever you want.

    Basically, the idea behind the delegation pattern is that you move the responsibility from the many small objects to one dedicated object. The small objects only catch requests and forward them, they don't execute them on their own.