GM:S 1.4 How to recreate asteroids movement using GML alone?

Discussion in 'Programming' started by gacl, Jun 25, 2016.

  1. gacl

    gacl Guest

    I'm trying to avoid the use of 'speed'.

    my whole ship movement script looks as follows:

    if keyboard_check(vk_space)
    {
    obj_ship.x += cos(radtodeg(obj_ship.image_angle)) * 3;
    obj_ship.y += sin(radtodeg(obj_ship.image_angle)) * 3;
    }
    if keyboard_check(vk_left)
    {
    obj_ship.image_angle += 3;
    }
    if keyboard_check(vk_right)
    {
    obj_ship.image_angle -= 3;
    }

    However, the ship is going all over the place, moving in any direction it wants to, seemingly randomly.

    Can anyone tell me where I'm going wrong? I have the tip of my ship facing upwards, if it helps.
     
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Hi and welcome to the GMC!

    You've used radtodeg instead of degtorad, which is causing you trouble.
    You'll probably also be interested in learning about the lengthdir_x and lengthdir_y functions.
     
  3. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,116
    you don't need degtorad because the trig functions have couterparts that take degrees as an argument
    dcos, dsin. dsin should be negated in your calculations.

    dcos
    -dsin

    or use lengthdir functions
     
  4. bojack29

    bojack29 Member

    Joined:
    Jun 21, 2016
    Posts:
    407
    You can use dsin and dcos too

    move in 45 degrees at length of 10
    Code:
    x += dcos(45) * 10;
    y += -dsin(45) * 10;
    
     
  5. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,837
    sprite must point right

    I dont know why you dont want to use speed. are you planning to do delta time movement?

    step:
    image_angle+=(keyboard_check(vk_left) - keyboard_check(vk_right)) * 3;
    motion_add(image_angle,keyboard_check(vk_up)*.01);
    friction = speed/20;

    using your own system
    create

    spdx= 0;
    spdy = 0;


    step
    image_angle+=(keyboard_check(vk_left) - keyboard_check(vk_right)) * 3;
    var thrust = keyboard_check(vk_up)*.01;
    var dx = dcos(image_angle) * thrust;
    var dy = dsin(image_angle) * thrust;
    spdx+=dx;
    spdy+=dy;
    spdx*=.99;
    spdy*=.99;
    x+=spdx;
    y+=spdy;
     
  6. GenjiBlackwood

    GenjiBlackwood Member

    Joined:
    Apr 1, 2018
    Posts:
    1
    I used this code, but my ship always goes in the opposite direction. Horizontally it works fine, but vertically it goes backwards. Like, when Im faced down, it goes up.
     
  7. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,837
    try using -image_angle, if you are using the physics engine and the physics functions
     

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