GMS 2 How to keep x and y instance create consisted with image angle

Discussion in 'Programming' started by mastmartelli, Mar 17, 2019.

  1. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    I'm trying to have a gun shoot bullets, by using instance_create_layer(x+bullet_x,y+bullet_y,"guns",oBullet) in the gun's step event, the bullet_y for this gun is set to 0 and the bullet x is set to the tip of the gun. It works fine, but since the gun can be rotated with image_angle, the more it rotates up or down the bullet starts to be created farther from the gun, anyone have any ideas on how to fix this?
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,628
    Use lengthdir_x and lengthdir_y, where length is the distance from gun origin to muzzle, and direction is the angle. The results will be the bullet_x and bullet_y for your create command.
     
  3. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    I don't think that's what I need, can you give me an example on how to execute it? I tried this:

    if (shoot) and (delay_time == 0) and (reload_time == reload) and (ammo > 0)
    {
    var x_test = lengthdir_x(bullet_x, image_angle);
    var bul = instance_create_layer(x+x_test, y+bullet_y, "gun", bullet);
    with (bul)
    {
    direction = other.image_angle + random_range(other.shot_offset, -other.shot_offset);
    image_angle = direction;
    speed = other.spd;
    alarm[0] = other.range;
    }
    ammo--;
    delay_time = delay;
    recoil_time = recoil;
    if (ammo == 0) reload_time = 0;
    scr_camera_screenshake(oCamera_2d, 1, 0.1);
    }

    bullet_y doesn't matter right now because for this gun it's set to 0
     
  4. LV154

    LV154 Member

    Joined:
    Sep 9, 2016
    Posts:
    39
    Instead of var x_test = lengthdir_x(bullet_x, image_angle), try var x_test = x+lengthdir_x(bullet_x, image_angle);

    The difference here is x+lengthdir_x as opposed to just lengthdir_x
     
  5. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    That didn't work I don't even see the bullet when I shoot now, I think it's because I already have x+x_test in the instance create for bullet
     
  6. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    I tried remove the x+ in the instance_create and now it's doing the same thing as it did before :/
     
  7. CROmartin

    CROmartin Member

    Joined:
    Jul 17, 2016
    Posts:
    80
    This should help:
    explantion for rotation and shoting.png
     
    LV154 likes this.
  8. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    Wow. I have no idea what dcos and dsin do even after reading the gamemaker studio help page, but it works perfectly, thank you!
     
    CROmartin likes this.
  9. CROmartin

    CROmartin Member

    Joined:
    Jul 17, 2016
    Posts:
    80
    I guess you know what cos and sin does (if not, refresh you knowledge, it wont take you to much), so basically difference between dcos and dsin and cos and sin is that dcos will give you right value if you put degrees in brackets (like dcos(125)) but for cos to give you right value you must put radians in bracket, one radian is about 57,2958 degrees so formula for getting right value should go like this cos(degree / 57,2958), same thing for dsin and sin. Well this is ofc not necessary when we have dcos and dsin, but this might help you understand it more. I am glad to help.
     
    Last edited: Mar 17, 2019
    mastmartelli likes this.
  10. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,628
    The lengthdir commands are wrappers for the trigonometry to simplify thinking angular movement. They do the same as using sines & cosines except you don't need to understand Pythagoras.
     
  11. mastmartelli

    mastmartelli Member

    Joined:
    Jan 17, 2019
    Posts:
    50
    Weird they didn't work when I tried it
     
  12. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    990
    It needs to be x+lengthdir_x(len,dir) and y+lengthdir_y(len,dir), simply using ONLY the lengthdir_x will not set the vector appropriately. Basically, take a measurement of the length in pixels from the sprite origin to the barrel tip in CROmartin's picture and take a measurement of the angle (with 0 degrees being directly right) and plug those values directly into BOTH the lengthdirs without trying to adjust for the x and y separately. For CROmartin's picture, the exact thing you would want is:
    Code:
    bullet_x = player_x+lengthdir_x(115,player_angle+342); // Alternatively you could use player_angle-18 for the dir section on both
    bullet_y = player_y+lengthdir_y(115,player_angle+342);
     
    CROmartin likes this.
  13. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    1,902
    upload_2019-3-18_11-55-57.png

    There are two vectors. [cos(angle), -sin(angle)], points in the direction of the parent's x axis. [sin(angle), cos(angle)] points in the direction of the parent's y axis. Both vectors have a length of 1, and they are perpendicular to each other.

    The x_axis vector is scaled by the child's x_offset, and the y_axis vector is scaled by the child's y_offset. The results are added together, along with the parent's position, and the result is the child's position.

    dcos, and dsin functions take the angle argument in degrees rather than radians.
     

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