I'd like to allow the player to change the direction of gravity in gamemaker (free version). Once the player presses Space I want to add 90 degrees to whatever the current direction of gravity is.
To check if it is something related to my keybinds I have assigned a command to print something to the console once the Space key was pressed. It seemed to work fine.
I am not very skilled yet, and I've tried to find tutorials on changing the gravity. Either they are not clear enough or they just explain how to set normal gravity.
I have tried using the set variable function with gravity_direction
I have also tried using the set gravity function, but that doesn't seem to work for me either.
I have set the gravity at this location: rightclick object_player > properties > physics tab > Gravity X: 0.0 | Gravity Y: 30.0
I have tried setting the gravity manually in a create event with the variables: gravity_direction: 270 gravity: 30
(Removed the settings at the object properties first)
I have tried putting a set gravity function in a create element by itself to set the standard gravity.
For some reason I float upwards, in the last two cases.
If you're not using the Physics Engine (you'd know if you were), then you can use the paired variables gravity
and gravity_direction
.
The gravity
variable controls the magnitude of the gravity. Setting it to 1 is like having gravity be 1px/s2.
Typically I use variables lower than 10 for the gravity
variable.
The gravity_direction
variable controls the direction of the gravity. It ranges from 0 to 360, where the most used values are 0 (Right), 90 (Up), 180 (Left), and 270 (Down).
If you want gravity to change 90 degrees to the right (Clockwise), use gravity_direction += 90
. If you want gravity to change 90 degrees to the left (Counter/Anti-Clockwise), use gravity_direction -= 90
.
Just a word of warning, gravity is applied after the End Step Event, so if you're finding your character stuck in walls or having weird collision issues, you might want to implement your own gravity in the Begin Step or Step Event (I usually use the Step Event).
If you're using the Physics Engine, then the gravity
and gravity_direction
variables won't work. You'll need to use the physics_world_gravity(x, y) function. Here, x
controls the horizontal gravity, and y
controls the vertical gravity.
So, if you wanted normal gravity, you'd use physics_world_gravity(0, 32)
, or whatever value for y
that you wanted.
Unfortunately there's no functions for determining the current gravity, so you'll have to make your own:
global.gravity_x = 0;
global.gravity_y = 32;
physics_world_gravity(global.gravity_x, global.gravity_y);
Typically when I'm using this system, I like to convert to old one, where there's a manitude (gravity
) and direction (gravity_direction
). So instead, I'd do this:
global.phys_gravity_force = 32;
global.phys_gravity_direction = 270; // Down
global.phys_gravity_x = lengthdir_x(global.phys_gravity_force, global.phys_gravity_direction);
global.phys_gravity_y = lengthdir_y(global.phys_gravity_force, global.phys_gravity_direction);
physics_world_gravity(global.phys_gravity_x, global.phys_gravity_x);
Every time you wanted to change the gravity of the world, you'd need to use all of the code in one of the two examples above. In my opinion, the second is easier, and all you would need to change each time would be the global.phys_gravity_direction
variable.