• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [1.4.9999] Hex grid movement

woods

Member
been at this for a couple days...

working on an olskool battletech inspired, time/turn based, squad tactics game.

i have a hex grid map and am trying to get my mech to move around properly..
60^ turns, walk front/back no problem..
what i am trying to do is get the mech to walk to the nearest hex in front of him and stop(effectively snapping to the center of the new hex)

i have a target hex "following the player" in front
step event:
GML:
x=obj_mech_1.x+lengthdir_x(64,obj_mech_1.direction);
y=obj_mech_1.y+lengthdir_y(64,obj_mech_1.direction);
i want to use this to target the nearest front facing hex when i move my player..
player step event:
Code:
/// movement

//facing
if direction =   0{sprite_index = spr_mech_1_east;}
if direction =  60{sprite_index = spr_mech_1_north_east;}
if direction = 120{sprite_index = spr_mech_1_north_west;}
if direction = 180{sprite_index = spr_mech_1_west;}
if direction = 240{sprite_index = spr_mech_1_south_west;}
if direction = 300{sprite_index = spr_mech_1_south_east;}
if direction = 360{direction = 0;}


//rotate left and right
if keyboard_check_pressed(vk_left) {direction += 60;}
if keyboard_check_pressed(vk_right) {direction -= 60;}


var hx = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).x; //want hex in front of mech, not nearest
var hy = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).y; // target_hex? and point_direction? length_dir?


if keyboard_check_pressed(ord("W"))
{  
    move_towards_point(hx,hy,1);// want to move to next hex in front of mech
    image_speed = 0.25;
}

/*
else
{
image_speed = 0;
speed = 0;
}
with the else speed = 0 at the end of the code, my mech moves just a notch and stops
(i commented out the else speed=0 )
my mech will walk forward but wont stop


what i think is happening is the target "hx,hy" is changing as i move out of my current hex(i want to move 1 hex forward on keypress)



end game result is going to be multiple mechs with move and shoot on two initiative timers... (if skillcheck is passed, then move)


sidenote:
am i coming at this project completely backwards?
 
Last edited by a moderator:

TailBit

Member
If you just want to move til you are aligned with the next tile, then I would just get the coordinate of the hex you stood at and lerp it with the one you are moving to, and increase the lerp by "speed / hex distance" til you are there
 

woods

Member
i just want the mech to walk to the center of the next hex and stop there when i push the forward button..

played around with the lerp function for about 4 hours... im just not getting how it works ;/
heres the current step event of the player

GML:
/// movement

//facing
if direction =   0{sprite_index = spr_mech_1_east;}
if direction =  60{sprite_index = spr_mech_1_north_east;}
if direction = 120{sprite_index = spr_mech_1_north_west;}
if direction = 180{sprite_index = spr_mech_1_west;}
if direction = 240{sprite_index = spr_mech_1_south_west;}
if direction = 300{sprite_index = spr_mech_1_south_east;}
if direction = 360{direction = 0;}


//rotate left and right
if keyboard_check_pressed(vk_left) || keyboard_check_pressed(ord("A")){direction += 60;}
if keyboard_check_pressed(vk_right) || keyboard_check_pressed(ord("D")) {direction -= 60;}


var hx = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).x;
var hy = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).y;

if keyboard_check_pressed(ord("W"))
    {      
        image_speed = 0.25;  
        x = lerp(x, hx, spd);
        y = lerp(y, hy, spd);  
    }

if keyboard_check_pressed(ord("S"))
    {      
        image_speed = 0.25;  
        x = lerp(x, hx, -spd);
        y = lerp(y, hy, -spd);  
    }

as it sits, the player will teleport to the next hex where he is supposed to be
(defeats the whole purpose of lerp doesnt it ;o))
when i do something like:
x = lerp(x, hx, spd/64);
the player moves across the hex a minimal amount per keypress..

obviously i dont understand how this function is supposed to work ;o)



edit:
if i go like this:
GML:
var hx = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).x;
var hy = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent).y;

var xx = lerp(x,hx,spd/64);
var yy = lerp(y,hy,spd/64);


if keyboard_check_pressed(ord("W"))
    {       
        image_speed = 0.25;
        move_towards_point(xx,yy,spd);   
//        x = lerp(x, hx, spd);
//        y = lerp(y, hy, spd);   
    }
i get fluid movement but he doesnt stop... also my target jumps to the next hex more like i imagined it would
 
Last edited:

TheouAegis

Member
Clearly you're grasping at straws here. What is the value of spd? The lerp() function just calculates things for you. If you want to move over a certain number of steps, the value of spd should be a fraction with the number of steps as the denominator (e.g., 1/32 if you want it to take 32 steps). Since lerp() needs to be run every step, you need to set up grid-based movement, which means hx and hy should not be temporary variables and they should only be set when a new cell is picked. If you want it to move over 32 steps, for example, you should set an alarm or timer to 32 and set hx and hy when the keyboard is pressed; then check if the alarm is greater than 0 and move using
Code:
x=lerp(x,hx,1/32);
y=lerp(y,hy,1/32);
It's the same with move_towards_point(); you'd need a timer to tell the function when to stop running. Whereas lerp() simply calculates the distance between two points and then returns a multiple of that distance, move_towards_point() calculates the direction of the calling instance toward two points and then updates the speed vectors.

And in both cases, there is a strong possibility that (x,y) will never equal (hx,hy) due to rounding errors. That's why I like to use the timer and then when the timer is done counting just set x and y to hx and hy directly, since rounding issues aren't a concern when moving, only when stopping. And if you designed your hex grid right, the time it should take to travel to any adjacent cell should be the same.
 

TailBit

Member
create event:
GML:
last = instance_nearest(x,y,, obj_hex_parent);


Code:
if keyboard_check_pressed(ord("W")){

    if next = noone{
        next = instance_nearest(obj_hex_target.x,obj_hex_target.y, obj_hex_parent);
        pos = 0;
    }
    hex_spd = spd / point_distance(last.x,last.y,next.x,next.y);
}

if next != noone{
    pos += hex_spd;
    x = lerp(last.x,next.x, pos);
    y = lerp(last.y,next.y, pos);
    if pos >= 1 { last = next; x = last.x; y = last.y;  next = noone; }
    if pos <= 0 { x = last.x; y = last.y;  next = noone; }
}
I guess something like this is what I had in mind, but yeah, just counting steps might be easier :3
 

woods

Member
grasping at straws is clearly an understatement.. i freely admit that ;o) but thats why i am here.. to learn.

thanks guys for the help
 
Top