# Wind direction effecting player

C

#### Captain_ArrowWound

##### Guest
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!

#### JackTurbo

##### Member
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.

#### RefresherTowel

##### Member
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)

#### NightFrost

##### Member
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.)

#### RefresherTowel

##### Member
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...

#### NightFrost

##### Member
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...
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

##### Member
Super useful @FrostyCat, thanks for publicly releasing that.

C

#### Captain_ArrowWound

##### Guest
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?

T

##### Guest
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.

C

#### Captain_ArrowWound

##### Guest
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!