***SOLVED*** Adding rotational movement

Discussion in 'Programming' started by KrimzonDestiny, Jun 10, 2019.

  1. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    Hey guys. Taking another crack at programming a different project. I always hit a brick wall when it comes to applying trig to my projects. The similar movement to A Link to the Past with a mouse and keyboard. 'W' moves the player toward whatever target is locked on to, and 'S' moves them backward away from them. I want to use 'A' and 'D' to orbit around the target (whether it be an enemy or the mouse) clockwise and counter-clockwise respectively, but and unsure as to where or how to implement that logic. Thanks for your consideration.

    Code:
    //switch targets between mouse and actor
        var targx,targy;
        if(global.target<>noone){
            targx=global.target.x;
            targy=global.target.y;
        }else{
            targx=mouse_x;
            targy=mouse_y;
        }
        
        //set direction
        var pdir = point_direction(x,y,targx,targy);
        
        // apply walk movement
        if( walk_dir == 1 || walk_dir == -1){
            //get distance/length
            var len = move_spd;   
                
            //get xspd and yspd
            xspd = lengthdir_x(len, pdir) ;
            yspd = lengthdir_y(len, pdir) ;
            
        }
            
        //move player
        x += xspd*walk_dir;
        y += yspd*walk_dir;
     
  2. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    473
    Point directon + 90 and - 90 using your pdir? Then just include that when you change the X/Y like you're doing already. Not sure how to do this on top of moving forwards or backwards though.
     
    KrimzonDestiny likes this.
  3. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    No, I don't think that work. It may just snap or subtract a rigid 90 degrees to the vector. I want to add or subtract a gradual amount. It's frustrating because I can make an object orbit another, and i can make one move away or towards another, I'm just not knowledgeable as to how to combine them to create an arc between the two.
     
    trentallain likes this.
  4. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    Adding 90 to the angle is starting to get somewhere. Thing is, however, it seems to negate the forward or backward movement; the instance just rotates around at the same distance.
     
  5. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    473
    Maybe try adding another point direction between the moving forwards/backwards Xspd/Yspd and the orbiting Xspd/Yspd and then using lengthdir again to find the final trig? Not sure how well that would work haha.
     
  6. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    473
    Oh also for the length argument in lengthdir for the orbit, that should be point distance of you and the target I think
     
  7. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    536
    This will move them forwards and backwards, and rotate them around. Not sure if that's totally what you want - as I can't recall how you move in A Link To The Past.

    create event:
    Code:
    spd = whatever;
    step event:
    Code:
     
    var targx,targy;
    if(global.target<>noone)
    {
    targx=global.target.x;
    targy=global.target.y;
    }
    else
    {
    targx=mouse_x;
    targy=mouse_y;
    }
     
    
    var pdir = point_direction(targx, targy, x, y);
    var pdist = point_distance(x, y, targx, targy);
    var forward = keyboard_check(ord("W"))
    var back = keyboard_check(ord("S"))
     
    if forward
    {
    walkdir = -1
    }
     
    if back
    {
    walkdir = 1;
    }
    
    if ((!forward) && (!back))
    {
    walkdir = 0;
    }
     
    if walkdir != 0
    {
    pdist += walkdir * spd; 
    }
        
    if keyboard_check(ord("A"))
    {
    pdir += -1
    }
     
    if keyboard_check(ord("D"))
    {
    pdir += 1;
    }
     
    x = targx + lengthdir_x(pdist, pdir);
    y = targy + lengthdir_y(pdist, pdir);
    image_angle = pdir + 180;
     
  8. trentallain

    trentallain Member

    Joined:
    Aug 6, 2016
    Posts:
    473
    In a link to the past you use a d-pad to move. This is more like ocarina of time 2D I think?
     
  9. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    You know what, you're right! I guess i'm actually looking for the movement to be a hybrid of the 2. Either that, or the closest to OoT you can get in 2D.
     
    Last edited: Jun 11, 2019
    trentallain likes this.
  10. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    Well, the rotation works, but there is an unforseen issue. Basically, the further away the player instance is from the target, the greater the change in position. I know it's because to rotate 1degree, the change is greater the further away from the origin of the vector you are. So, I want to alter the number I add to 'pDir' so that the rotational change feels the same no matter how close or far away you are. I'm guessing this is called "normalizing" the value, but I'm not sure that's exactly correct.
     
  11. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,984
    If I understand correctly what you're trying to do, I think this will work well enough for you:

    var _mx = move_speed * (keyboard_check(vk_up) - keyboard_check(vk_down));
    var _my = move_speed * (keyboard_check(vk_right) - keyboard_check(vk_left));
    var _dir = point_direction(x,y,mouse_x,mouse_y);
    var _c = dcos(_dir);
    var _s = dsin(_dir);
    hspeed = _mx * _c + _my * _s;
    vspeed = _mx * -_s + _my * _c;

    Presuming the mouse is the target point. You can have seperate "move_speed" variables for _mx and _my if you'd like.
     
    KrimzonDestiny likes this.
  12. KrimzonDestiny

    KrimzonDestiny Member

    Joined:
    Aug 2, 2016
    Posts:
    38
    You guys are the greatest! Thank you so much. Seriously, almost every project that I abandoned I did so because of my lack of knowledge of trig. You wouldn't happen to know of any tutorials or youtube demonstrations of trigonometry for games for dumb dumbs would ya?
     
  13. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    536
  14. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,984
    If you have a pretty firm grasp of basic algebra, learning basic trig in 2d is actually pretty straightforward. You could probably get it done in a couple of afternoons. A math textbook is where I would personally look. But I'm sure there must be tons of videos on youtube and other resources online.

    Now, the huge majority of questions I see that involve some kind of trig basically revolve around rotating a vector. Which is all that is going on above, just rotating the vector (_mx,_my) by the angle _dir. And if you want to understand why what I posted works, I drew up this diagram a while back that I hope makes it somewhat clear. Although here, a vector (x_offset,y_offset) is being rotated and then added to another vector (parent_x,parent_y), but the rotation is done in the same way.

    [​IMG]

    Notice that the y axis points downward here, and that is just the convention that we're using in gamemaker's default orthographic projection. In textbooks, you will see the y axis point upwards, and positive angles of rotation still going anti-clockwise. In which case the sign before the sine functions will be reversed in most calculations.
     
    Last edited: Jun 14, 2019 at 4:58 AM
    the_dude_abides likes this.

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