GML [1.4.9999] gravity well /solar flare

woods

Member
starting to add some fluff to my space shooter..
i have three "sun"sprites i want to use 1 static and safe, 2 warning flare imminent, 3, warning critical ... then create a flare ,(still havnt made the flare yet)
that part is pretty basic.
set a timer and change the sprite or object
create flare to damage players within range
(works the same as creating a bullet)

(sidenote: these three "suns" are shown side by side for visual reference. they will be created and destroyed on timer)
1594547612363.png


here i go babbling..

the issue i am about to tackle is the gravity well... i want an increasing pull the closer the player gets to the sun. And also increasing pull as the stages of the flare's warning changes. (resetting the pull to minimal after the flare)


not exactly sure where to start.. with the gravity well.
 

Roleybob

Member
Something like this called from the player object:

GML:
var distance_to_sun;
var distance_to_move;
var angle_to_sun;

distance_to_sun = point_distance(x, y, obj_sun.x, obj_sun.y);    //the distance between obj_sun's origin and the player's origin//
if (distance_to_sun < move_speed)   //if the player is already near obj_sun's origin, go to that position and exit. This is to stop the player from jittering around the centre of the sun , and also to avoid a divide by 0 error//
{
    x = obj_sun.x;
    y = obj_sun.y;
    exit;
}

distance_to_move = move_speed / distance_to_sun;    //this is how many pixels we want the player to move towards the sun based on how far away it is//
angle_to_sun = point_direction(x, y, obj_sun.x, obj_sun.y);    //this is the direction towards obj_sun's origin//

x += lengthdir_x(distance_to_move, angle_to_sun);
y += lengthdir_y(distance_to_move, angle_to_sun);    //these 2 lines actually move the player//

Where move_speed is a variable which stores how many pixels the player moves at in a single step.

You'll need to reduce the value of distance_to_move for the suns with a lower gravity value, and also you should cap the maximum distance that the sun can pull the player in below the player's move speed, otherwise the player won't be able to escape when it gets near to centre of the obj_sun.

You probably want a cut-off distance so that if they player is far away there won't be any gravity at all, and simply dividing by the distance gives a very slow movement towards the sun so you'll want to play around with the numbers used to get what you want
 
Last edited:

woods

Member
thanks for the jumpstart.
ive been on this project all day and kinda ran into one of those... uuuuuuhhhh blank walls ;o)

not very used to using lengthdir.. i still get muddled with it pretty easily
 

Sabnock

Member
if you are using GMS's built in speed and direction functionality you could use motion_add() in each of the objects that have mass to gradually change the speed and direction of the affected instances. you would need to calculate a fall off so that the gravitational affect is weaker / stronger dependant on the mass of each instance and the distance your affected object is away from it.

you would then only need to use point_direction() to determine the direction that motion_add() needs.
 
Last edited:

woods

Member
Roley solution is working right nice for me so far ;o)


player create
Code:
sprite_index = spr_ship_0;
HP = 500;
countdown = room_speed * 1;
turn_spd = 2; // rotation
thrust = 0.05; // acceleration
spd = 6;  // max speed
image_xscale = 0.5;
image_yscale = 0.5;
image_angle = 0;
friction = 0.01
can_shoot = true; // to slow down rate of fire
countdown_current = room_speed * 1;
kill_count = 0; // score
player step -movement
Code:
/// movment


if(keyboard_check(ord("A"))){
    image_angle = image_angle + obj_P1.turn_spd;
}

if(keyboard_check(ord("D"))){
    image_angle = image_angle - obj_P1.turn_spd;
}


if(keyboard_check(ord("W"))){
    motion_add (image_angle, + obj_P1.thrust)
    with (obj_thruster_1)
    {
        visible = true;
    }
}
else
{
    with obj_thruster_1
    {
        visible = false;
    }
}

if(keyboard_check(ord("S"))){
    {
        motion_add (image_angle, - obj_P1.thrust);
    }
}

speed = clamp(speed, -obj_P1.spd, obj_P1.spd);
player step -gravity well
Code:
/// gravity well


if (instance_exists(obj_sun_1))
{
var distance_to_sun;
var distance_to_move;
var angle_to_sun;

distance_to_sun = point_distance(x, y, obj_sun_1.x, obj_sun_1.y);    //the distance between obj_sun's origin and the player's origin//
if (distance_to_sun < spd)   //if the player is already near obj_sun's origin, go to that position and exit. This is to stop the player from jittering around the centre of the sun , and also to avoid a divide by 0 error//
{
    x = obj_sun_1.x;
    y = obj_sun_1.y;
    exit;
}

distance_to_move = spd / distance_to_sun*100;    //this is how many pixels we want the player to move towards the sun based on how far away it is//
angle_to_sun = point_direction(x, y, obj_sun_1.x, obj_sun_1.y);    //this is the direction towards obj_sun's origin//

x += lengthdir_x(distance_to_move, angle_to_sun); 
y += lengthdir_y(distance_to_move, angle_to_sun);    //these 2 lines actually move the player//
}


ive been fiddling with distance_to_move for a good while... changing up the multiplier.. getting different rates and whatnot. it is rather difficult due to each ship having different stats..


ship 0
Code:
sprite_index = spr_ship_0;
HP = 500;
countdown = room_speed * 1;
turn_spd = 2;
thrust = 0.05;
spd = 6;
the dreadnought ...big bulky and slow
the light fighter ...highly maneuverable but no armor

ship 3
Code:
sprite_index = spr_ship_3;
HP = 200;
countdown = room_speed * 0.5;
turn_spd = 6;
thrust = 0.08;
spd = 15;
 
Top