Hexagon, Square or Circle

2»

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    @Summation said:
    Or does the pattern not follow?

    From what I can see sqrt(X) would effect the radius rather than the number of sides, changing the modulus value will determine the number of sides, you'd simply divide 360° but how many sides you want (and then offset the angle by half that amount (which is the -30 bit)).

    So (and don't quote me on this because I've not tested anything yet !! :smile: ) an octagon would be described by . . .

    radius/cos((self.Rotation %45)-22.5)

    . . . and a decagon would be . . .

    radius/cos((self.Rotation %36)-18)

    . . . and a triangle would be . . .

    radius/cos((self.Rotation %120)-60)

    etc.

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    @Summation said:

    Lol, I've made you a demo project - open the 'plot' actor and switch on and off the various move behaviours (only have one on at a time) to see how the maths works.

  • SummationSummation Member, PRO Posts: 476

    @Socks said:
    Lol, I've made you a demo project - open the 'plot' actor and switch on an off the various move behaviours (only have one on at a time) to see how the maths works.

    Learning in progress, please wait. :D

  • SummationSummation Member, PRO Posts: 476

    @Socks

    I wonder if you can use this pattern to spawn a bunch of spawners which will ultimately form a hex grid?

    By the way, I tried to place 4 on screen at the same time and the 4th one wouldn't spawn for some odd reason... I figure it's just the windows creator acting up.

  • SocksSocks London, UK.Member Posts: 12,822

    @Summation said:
    Socks

    I wonder if you can use this pattern to spawn a bunch of spawners which will ultimately form a hex grid?

    Yes, you could, although there are probably easier / quicker ways to set up a hex grid as the spacing is essentailly just two square grids (one offset from the other).

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    Hex Star:

  • SummationSummation Member, PRO Posts: 476

    @Socks said:
    Hex Star:

    Added a very simple color change.

  • SummationSummation Member, PRO Posts: 476

    @Summation said:

    Is it me or are the exports unusable?
    I tried to download my own project and for some reason can't open it because, even after I unzipped everything, there was no "project_name.gsproj" file...

  • SocksSocks London, UK.Member Posts: 12,822

    @Summation said:
    I tried to download my own project and for some reason can't open it because, even after I unzipped everything, there was no "project_name.gsproj" file...

    Might be something to with the Nightly build.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited May 2015

    @Summation They all worked for me but some were made using the Nightly version of Creator and require it to open.

    Edit: I only tried the Mac project files.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SummationSummation Member, PRO Posts: 476
    edited May 2015

    @tatiang said:
    Summation They all worked for me but some were made using the Nightly version of Creator and require it to open.

    Edit: I only tried the Mac project files.

    I'm using 0.13.4, filed bug 999 just in case.

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    edited May 2015

    I used to be a programmer. And sometimes I do get to do programming when I don't have templates or games I need to work on.

    WARNING!!! MATH!!!!!

    But as for the reasoning behind the math:
    floor( self.Size.Width /4) *sqrt(3)/cos(( self.Angle %60)-30)

    In a regular hexagon, where all the edges are the same size and all the angles are the same, it is made up of 6 equilateral triangles.

    So at 1 vertex starting at 0 degrees, every 60 degrees, you get to another vertex. (That self.Angle%60) ((Just noticed how old that expression is that I haven't switched to the mod(x,y) expression))

    So we break down determining a point on the edge of the hexagon that way because the calculations are relatively the same for each 60 degrees.

    So from the image attached, you have 1/6th of the hexagon. If the actor/image has the width,w, from the far right point of the green triangle to what would be a point mirrored on the Y axis, we have determined that the length of a side of the equilateral triangle is width/2, or w/2.

    With that we can determine with that blue line that splits the equilateral triangle exactly in half, it makes a 30/60/90 triangle, so the length of the blue line is known to be the sqrt(3)*w/4

    Since we know that the angle of the equilateral triangle is 60 degrees, if we make a right triangle(the lower right purple line) from the point where the blue line hits crosses the green triangle, that angle is 30 degrees. And thus the angle formed by the blue and purple line is 60 degrees.

    That means the θ+θ' angle is 30 degrees in the triangle made by the blue, purple, and green lines. (Basic Math Knowledge: The sum of the angles in a triangle is always 180 degrees)

    So we want to know θ' to figure out x. θ' = 30-θ

    That grey Theta, θ is the known angle from that (self.Angle%60) from above.

    So the right triangle made by the blue line and with the maroon line,'x', and the small pat of the green line, we know the length of the blue line and an angle, θ'.

    So we can figure out the length of the maroon line with the equation, cos(θ') = (length of the blue line)/x. So x = (sqrt(3)*w/4)/cos(θ')

    See: http://www.mathsisfun.com/sine-cosine-tangent.html

    cos(θ) = cos(-θ) so we use the expression

    x=(sqrt(3)w/4)/cos(30-θ)
    =(sqrt(3)
    w/4)/cos(θ-30)
    =(sqrt(3)*self.width/4)/cos(self.Angle-30)

    EDIT:
    The function assumes the hexagon is as pictured. If you rotate the actor/hexagon, you will need to subtract the rotation to normalize the angle.

    floor( self.Size.Width /4) *sqrt(3)/cos(( self.Angle %60)-30)
    becomes
    floor( self.Size.Width /4) *sqrt(3)/cos(( (self.Angle-self.Rotation) %60)-30)

  • DocJDocJ Member Posts: 66

    @Socks @tatiang @Summation @code_monkey I know this started out with a "board game" question, but all I can say after all the code examples you guys just whipped up is.......Holy Crap!!

    The math involved and the way it comes together is incredible. I'm actually very good at math myself (3 college years of calculus), but I still had to dissect the way it was put together in GS. Great job!!

    I'm sure there is use for these examples in someone's upcoming games. In fact, I bet you've probably spawned some new ideas with these examples.

    I thank you all once again for the extended dialogue. That's what I love about these forums.
    Doc

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772

    Threads like this make me wish I had paid more attention in math class! :)

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    @CodeMonkey -- thanks for sharing this great tip!

    @Socks -- those demos are definitely keepers. Thanks for sharing them.

  • SocksSocks London, UK.Member Posts: 12,822

    @CodeMonkey said:
    I used to be a programmer. And sometimes I do get to do programming when I don't have templates or games I need to work on.

    WARNING!!! MATH!!!!!

    But as for the reasoning behind the math: . . .

    Cheers for that, great information, useful too !

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    @DocJ said:
    Socks tatiang Summation code_monkey I know this started out with a "board game" question . . .

    In the new and upcoming GameSalad 14.1 there is going to be a pathfinding function that includes hexagonal grids, which should be really useful for certain sorts of hex grid games.

    https://help.gamesalad.com/hc/en-us/articles/206039217

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    Socks -- those demos are definitely keepers. Thanks for sharing them.

    Cheers. :)

  • SummationSummation Member, PRO Posts: 476
    edited May 2015

    @CodeMonkey said:

    WARNING!!! MATH!!!!!

    You know, I think there should be an "Educational" reaction alongside the existing ones.
    Great job.

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803

    More Math!!!

    Here is the expression for octagons.
    floor( self.Size.Width /2)/sin((( self.Angle +22.5- self.Rotation )%45)+67.5)

    Adding in the self.Rotation will allow for rotation of the tile/actor and still get the collision area correct. The 22.5 offset is to get an octagon vertex on the x-axis.

    I borrowed an image of a regular octagon from mathcaptain because it had angle values already on it.

    self.Size.Width/2 is the length from the center of the octagon to the center of a side and forms one of the non-hypotenuse sides of the right triangle.

Sign In or Register to comment.