1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GML Creating a direction vector from object direction

Discussion in 'Programming' started by Dr.Spankenstein, Jan 25, 2020.

  1. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    11
    In order to get a vector from a direction I could do the following:

    Code:
    var dirX = dsin(direction)
    var dirY = dcos(direction)
    The problem is that this relies on

    Code:
    0 = Up
    90 = Right
    180 = Down
    270 = Left
    
    and GameMaker uses

    Code:
    0 = Right
    90 = Up
    180 = Left
    270 = Down
    
    How can I translate from gamemaker anticlockwise 90 = Up directions to the clockwise 0 = Up directions?
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,107
    You could experiment with using the correct x/y and sin/cos combination.
     
    Sabnock likes this.
  3. SnotWaffle Studios

    SnotWaffle Studios Member

    Joined:
    Jun 21, 2016
    Posts:
    296
    Make it negative, mod 360, add 90?

    -0 mod 360 +90 = 90
    -90 mod 360 + 90 = 0
    -180 mod 360 + 90 = 270
    -270 mod 360 + 90 = 180

    You might need to do the reverse.

    Edit:
    This is the reverse.
    So taking gamemaker's direction, just do this:
    Code:
    var newDirection = -(direction-90) mod 360;
    var dirX = dsin(direction);
    var dirY = dcos(direction);
    
     
    Dr.Spankenstein likes this.
  4. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    11
  5. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    11
    Actually, that doesn't appear to handle direction = 180 correctly?
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,107
    You do know cos is for calculating x, and sin for y, right?
     
  7. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,250
    If I understand you, it should just be

    x = dsin(angle)
    y = -dcos(angle)

    results in:

    0: 0,-1
    90: 1,0
    180: 0,1
    270: -1,0
     
  8. Dr.Spankenstein

    Dr.Spankenstein Member

    Joined:
    Feb 5, 2019
    Posts:
    11
    Ah. I'm an idiot for getting them the wrong way around! Works as expected now. Thank you, all.
     

Share This Page