Wind direction effecting player

Discussion in 'Programming' started by Captain_ArrowWound, Feb 11, 2019.

  1. Captain_ArrowWound

    Captain_ArrowWound Member

    Joined:
    Jan 19, 2019
    Posts:
    29
    In the game I am making the player can sail around as a ship and if they are going in the direction of the wind their ship sails twice as fast. What can I use to say if the ship is traveling anywhere from a region such as 0 to 45 degrees it will go faster instead of just of specific number? Thanks for any help!
     
  2. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    823
    Do you want the wind to effect them regardless of what direction they're traveling in? - ie boost them when sailing with it, slow them when sailing against and cause drift when sailing perpendicular? If so I'd consider handling it separately from the usual movement. Define wind direction and power. Then after the unit has resolved its own movement, apply wind movement.

    Code:
    x =  length_dirx(windPower, windDir);
    y = length_diry(windPower, windDir);   
    Obviously it'd be a bit more complicated than that as you'd want to check for collisions etc.
     
  3. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    You can use angle_difference(ang1,ang2):
    Code:
    if (angle_difference(sail_dir,wind_dir) <= 45) {
       // Ship moving faster code
    }
    (But JackTurbo's code might be a cooler way of handling the wind)
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,924
    I would use the lengthdirs as it creates a smooth effect transition anywhere from sailing against the wind to sailing with the wind. Of course this could also be derived from angle difference. (If GML had vectors you'd just sum ship's velocity vector with wind's velocity vector to arrive at final velocity and be done with it, but lengthdirs arrive at the same result.)
     
  5. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    Yeah GM lacking true vectors is really annoying in so many different ways. The simplest way I handle it is with an X and a Y macro and using arrays (pos[X], pos[Y]) but still, gotta do all the calculations as though the pos is two separate variables...
     
  6. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,924
    My solution was to write a bunch of vector scripts, but in the end that's A) still having to write VecA = v_sum(VecB, VecC) instead of VecA = VecB + VecC, and B) still about mucking about with silly two-slot arrays, now just hidden from plain sight.
     
    RefresherTowel likes this.
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,461
    Bayesian and RefresherTowel like this.
  8. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,211
    Super useful @FrostyCat, thanks for publicly releasing that.
     
  9. Captain_ArrowWound

    Captain_ArrowWound Member

    Joined:
    Jan 19, 2019
    Posts:
    29
    Thanks for the help. I set this up with the lengthdirs using the wind speed and wind direction, but now the ship is stuck in the top-left corner of the screen. Do you know what I can do to remove this problem?
     
  10. Taddio

    Taddio Member

    Joined:
    Nov 16, 2018
    Posts:
    463
    I did a little top-down pirate ship thing a while ago, and I used a gimmick with gravity, gravity direction and max_speed to simulate wind. From what I remember, it worked pretty good, but it obviously has big flaws, too.
     
  11. Captain_ArrowWound

    Captain_ArrowWound Member

    Joined:
    Jan 19, 2019
    Posts:
    29
    Here is my code by the way.

    obj_wind-create
    Code:
    /// @description
    globalvar windSpeed,windDirection;
    windDirection = choose(0,45,90,135,180,225,270,315);
    windSpeed = random_range(0,2);
    
    faster = 0;
    cooldown = 0;
    obj_wind-step
    Code:
    obj_ship.x =  lengthdir_x(windSpeed, windDirection);
    obj_ship.y = lengthdir_y(windSpeed, windDirection);
    
    if (cooldown <= 0)
    {
        windDirection = choose(0,45,90,135,180,225,270,315);
        windSpeed = random_range(0,4);
        cooldown = 700;
    }
    cooldown = cooldown - 1;
    obj_ship-create
    Code:
    globalvar shipspeed;
    shipspeed = 1;
    globalvar shipDirection;
    shipDirection = direction;
    obj_ship-step
    Code:
    //Turning left
    if keyboard_check_direct(ord("A"))
    {
        image_angle += 0.5;
        direction += 0.5;
    }
    // Turning right
    if keyboard_check_direct(ord("D"))
    {
        image_angle -= 0.5;
        direction -= 0.5;
    }
    //Moving Foward
    if (keyboard_check_direct(ord("W")))
    {
        x = x + shipspeed*cos(direction*pi/180);
        y = y - shipspeed*sin (direction*pi/180);
    
    }
    
    //Moving backward
    if (keyboard_check_direct(ord("S")))
    {
        x = x - shipspeed*cos(direction*pi/180);
        y = y + shipspeed*sin (direction*pi/180);
    }
    Thanks for helping!
     

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