GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 GML Code Help » triple parallax starfield » 2019-04-26 05:11:53

Heavybrush
Replies: 1

Hi guys,
I have a new project, and I would like to figure out how to make a triple parallax starfield

for now i made this:

start event

Expand/// @description create starfield

//inputs
starField = 3;
starDepth[0] = 100;
starDepth[1] = 110;
starDepth[2] = 120;
stars = 0;
maxStars = 1000;
spd[0] = 0.3;
spd[1] = 0.2;
spd[2] = 0.1;
minSpd = 0;
maxSpd = 1;
dir = 90;
posX[0] = 0;
posX[1] = 0;
posX[2] = 0;
posY[0] = 0;
posY[1] = 0;
posY[2] = 0;
posW = room_width;
posH = room_height;
starColor[0] = global.ega_white;
starColor[1] = global.ega_light_grey;
starColor[2] = global.ega_dark_grey;

//stars init
for(var s = 0;s < starField;s++) {
    for(var i = maxStars;i > stars;i--) {
        //depth
        starDepth[i] = starDepth[s];
    
        //position
        posX[i] = irandom(posW);
        posY[i] = irandom(posH);
    
        //speed
        spd[i] = spd[s];
    
        //color
        starColor[i] = starColor[s];
    }
}

step event

Expand/// @description move stars

//movement
for(var s = 0;s < starField;s++) {
    //direction
    var ax = dcos(dir);
    var ay = dsin(dir);
 
    for(var i = maxStars;i > stars;i--) {
        //position
        var xx = posX[i];
        var yy = posY[i];
    
        //speed
        xx += spd[i] * ax;
        yy += spd[i] * ay;
    
        //wrap
        if(xx < 0) xx += posW;
        else if(xx > posW) xx -= posW;
        if(yy < 0) yy += posH;
        else if(yy > posH) yy -= posH;
    
        //new position
        posX[i] = xx;
        posY[i] = yy;
    }
}

draw event

Expand/// @description draw stars

//stars drawing
for(var s = 0;s < starField;s++) {
    for(var i = maxStars;i > stars;i--) {
        draw_point_colour(posX[i],posY[i],starColor[i]);
    }
}

it works but just one layer
i could put all the step event in the draw event but i made this way to have everything more clear

and i already tried using 2d arrays but i can't manage them yet
so i would like to understand how to do and talk about it with you guys

currently the direction of the stars is opposite to the direction, for one reason, it could be enhanced using the direction of my ship to have a little change

and i decided to make it with code instead animating a sprite, because you could take a powerup boost and have also a stars feedback having more speed also for them, and opefully a little stroke about them when you are very fast

Board footer

Powered by FluxBB