Trouble with to move toward obj boarding game

Discussion in 'Programming' started by Fabseven, Nov 12, 2019.

  1. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    359
    Hello devs,

    I am trying some experiences and testing how to create a rpg-boardgame
    Maked a big map, take a look :

    map.png

    i am trying to make the player (in red) going through the obj_event (white)
    It's not quite good at the moment, after i press UP => the player is going toward the target (the next event)
    I donot know how to stop it properly, tried with distance_to_object or distance_to_point but it's not clean.
    Either it's continuing up forever because the condition distance <= 1 is not true or when i changing the code i am with distance_to_object (instead of distance_to_point) it's stopping near the target ...

    I think i am missing something, i need my obj_player center's to go to obj_event center's to be clean.

    Source :
    Code:
    Information about object: obj_player
    Sprite: spr_player
    Solid: false
    Visible: true
    Depth: -2000
    Persistent: false
    Parent: 
    Children: 
    Mask: 
    No Physics Object
    Create Event:
    execute code:
    
    going_next_ev = false
    going_next_ev_targetid = noone
    current_event_id = noone
    event_goto[EST] = noone
    event_goto[SOUTH] = noone
    event_goto[NORTH] = noone
    event_goto[WEST] = noone
    d= noone
    
    speed = 0
    speed_going = 2
    move_launched = false
    
    Step Event:
    execute code:
    
       ///eva of col with event
    if( going_next_ev == false)
    {
    
       if( place_meeting(x,y,obj_event))
       {
           current_event_id = instance_nearest(x,y,obj_event)
           event_goto[EST] = current_event_id.event_goto[EST]
           event_goto[SOUTH] = current_event_id.event_goto[SOUTH]
           event_goto[NORTH] = current_event_id.event_goto[NORTH]
           event_goto[WEST] = current_event_id.event_goto[WEST]
       }
    
    }
    
    execute code:
    
    ///moving if having a target
    if(  going_next_ev = true and  instance_exists( going_next_ev_targetid ))
    {
       if(move_launched == false)
       {
           move_towards_point( going_next_ev_targetid.x , going_next_ev_targetid.y, speed_going)
           move_launched = true
       }
           t = instance_nearest(x,y,obj_event)
           if (t == going_next_ev_targetid)
           {
               centerx = x + sprite_width/2
               centery = y + sprite_height/2
               t_centerx = t.x + sprite_get_width(t.sprite_index)
               t_centery = t.y + sprite_get_height(t.sprite_index)           
           
               d = distance_to_point(t_centerx,t_centery)
               
               if( d <= 1)
               {
               
                   going_next_ev = false
                   speed = 0
                   going_next_ev_targetid = noone
                   //x = t.x
                   //y = t.y
                   move_launched =false
               }
                 
           }
       
       
       
    }
    
    Draw GUI Event:
    execute code:
    
    draw_set_font(font_gui)
    draw_set_color(c_red)
    draw_text(100,100,"going next ev ="+string(going_next_ev))
    draw_text(100,120,"going targetid ="+string(going_next_ev_targetid))
    draw_text(100,140,"current_event_id ="+string(current_event_id))
    draw_text(100,160,"d ="+string(d))
    
    
    Key Release Event for <Up> Key:
    execute code:
    
    if( instance_exists(current_event_id))
    {
       var target = event_goto[NORTH]
       
       with(obj_event)
       {
           if(event_id == target)
           {
               target = id   
           }
       }
       
       
       if (instance_exists(target))
       {
           going_next_ev = true
           going_next_ev_targetid = target
           
           current_event_id = noone //reset
           
       }
    
    }
    
    
    
    
    Code:
    Information about object: obj_event
    Sprite: spr_event
    Solid: false
    Visible: true
    Depth: -1000
    Persistent: false
    Parent: 
    Children: 
    Mask: 
    No Physics Object
    Create Event:
    execute code:
    
    event_id = noone
    event_goto[EST] = noone
    event_goto[SOUTH] = noone
    event_goto[NORTH] = noone
    event_goto[WEST] = noone
    
    
    
    Any idea ?
     
  2. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,077
    Change the distance_to_point check from "<= 1" to "<= speed_going". If the speed is faster than the distance being checked, then you can over-shoot the position and the distance check will never return true (which is what's happening here).
     
    Fabseven likes this.
  3. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    359
    Ty for your advice, i changed the code like this (targetx and targety in creation code as well)
    Code:
    ///moving if having a target
    if(  going_next_ev = true and  instance_exists( going_next_ev_targetid ))
    {
             targetx = going_next_ev_targetid.x //+ sprite_get_width(going_next_ev_targetid.sprite_index)
             targety = going_next_ev_targetid.y //- sprite_get_height(going_next_ev_targetid.sprite_index)/2           
           
             d = distance_to_point(targetx,targety) 
             //d = distance_to_object(going_next_ev_targetid)
       
               if( d <= speed_going)
               {
                   show_debug_message("stop")
                   going_next_ev = false
                   speed = 0
                   going_next_ev_targetid = noone
                  // x = t_centerx
                  // y = t_centery
                   move_launched =false
                   targetx = noone
                    targety = noone
               }
               else
               {
                 
                  move_towards_point(targetx,targety,speed_going)
                  show_debug_message("step")
               }
         
       
       
    }
    
    it's moving ... then reaching the target and stopping, not bad. But the position of the obj_player is not good at 100%, check this :
    map.PNG

    ps : changed sprite origin to 16/16 (from 0/0 , height and width = 32)

    Second problem : if the target is not strictly a cardinal direction the player is going straight to the target (maybe going through the forest)
    in the end will it be easier to store the pixels to move on a direction and only have next event North,south,est,west ?
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    4,178
    If you want complex movement, you could use Path resources for the movement instead. To avoid having hundreds of paths, you could have a system where you only have path movements when not moving in a cardinal direction, and assign paths on a panel-to-panel basis using Instance Creation Code to set "path_up", "path_right" (etc) variables.
     
  5. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    359
    ok, used https://docs.yoyogames.com/source/d... collisions/motion planning/mp_grid_path.html system and it's working fine !
    my "solid" cell are all setted at depth 1000 so i added cell looping through tiles with depth = 1000

    Only "bug" : i have a bridge on my water and the water is at depth 1000 so we path will no go through this bridge, but i already have to solution = make the water under the bridge not in tile 1000
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice