playMaker

Author Topic: Converting hex offset (duel axis) coordinates to cube axis (tri axis) [SOLVED]  (Read 2397 times)

simon

  • Playmaker Newbie
  • *
  • Posts: 17
I've been reading THE source of information of hex grid coordinates by Red blob https://www.redblobgames.com/grids/hexagons/

I understand that in order to have cube based coordinates, I have to convert from the standard method of generating hex grids. When generating the hex grid, the odd row is indented, causing a pattern of offsets that makes pathfinding more complicated in the long run.

Based on Redblob's guide, i'm supposed to use the below algerithm to convert from offset to cube:

function oddr_to_cube(hex):
      x = hex.col - (hex.row - (hex.row&1)) / 2
      z = hex.row
      y = -x-z
      return Cube(x, y, z)

The part that says '(hex.row&1)' has what looks like an 'AND' operator followed by 1. I understand that it's used to check whether there is an odd number (indented) or otherwise and how do I replicate this without diving into C#?


Ultimate question; how do I get 'X' using playmaker?

I might try seeing if I can get the state of the row I'm up to with a Boolean switch. Perhaps I can use that to determine whether it's an odd number. I know for sure that every other number will be odd after all..

Still, feels like I'm hacking it together.

Warm regards,

Simon.
« Last Edit: January 25, 2018, 06:58:26 AM by simon »

simon

  • Playmaker Newbie
  • *
  • Posts: 17
Re: Converting hex offset (duel axis) coordinates to cube axis (tri axis)
« Reply #1 on: January 20, 2018, 06:37:00 AM »
Hey guys.. So I tried creating my own custom action by copying an existing custom script and changing the regular operation in bitwise operations involving all axis represented by the cube coordinates:

switch (operation)
            {
                case Operation.OddRowToCube:
                    storeResultX.Value = v2 - (v1 - (v1 & 1)) / 2;
                    storeResultZ.Value = v1;
                    storeResultY.Value = -storeResultX.Value -storeResultZ.Value;
                    break;

                case Operation.EvenRowToCube:
                    storeResultX.Value = v2 - (v1 + (v1 & 1)) / 2;
                    storeResultZ.Value = v1;
                    storeResultY.Value = -storeResultX.Value -storeResultZ.Value;
                    break;

             
            }

It kind of works, but only really on the Z axis (left to right). It doesn't quite line up properly with the Y and X. It's repeating a number every few numbers and causing weird duplicates here and there as a result. Have I got the syntax wrong? I don't have any errors..

Any thoughts?

Warm regards,

Simon.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Converting hex offset (duel axis) coordinates to cube axis (tri axis)
« Reply #2 on: January 22, 2018, 02:10:22 AM »
Hi,

 Indeed for these kind of features, a custom action is a very good thing to develop, and it will be reusable as well.

I think your copy paste needs to be fixed, as I look at both Operation, I think StoreResultX and StoreResultY should be swapped.

 So make sure you logically go over your code, comment it fully, each and every line until you are in complete control over the logical math and what's happening. Else debug information in between each line or run the c# debugger to check values at runtime.

 Usually, when I fail all the above, I do a routine that works in itself without input, I verify the I get the expected result and then I start swapping values with the ones coming from the scene or the fsm variables.

Bye,

 Jean

simon

  • Playmaker Newbie
  • *
  • Posts: 17
Re: Converting hex offset (duel axis) coordinates to cube axis (tri axis)
« Reply #3 on: January 25, 2018, 06:57:56 AM »
I managed to solve this by ensuring I had generated my rows in the same direction as indicated by Redblob. Everything works fine now :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Hi,

 great :)

 Bye,

 Jean

PatternTree

  • Playmaker Newbie
  • *
  • Posts: 5
Hello and sorry for posting in such an old thread but is it possible to get this action you made or the code it was made up of? :)
I'm looking around for good methods for working with a hex grid in Playmaker and if you managed to make an action that successfully converts world position to grid position then I would be more than interested in using it as well! :D