• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Trail for a fixed object

P

ProjectGamesInc

Guest
Hello guys,

I'm struggling trying to create a trail on one object that is not moving.
I have this script

Code:
/// @description draw_trail_scripts(length,width,color,sprite,slim,alpha)
/// @param length
/// @param width
/// @param color
/// @param sprite
/// @param slim
/// @param alpha
//Ex. draw_trail(32,32,c_white,-1,1,1)
var Length,Width,Color,Sprite,Slim,Alpha,AlphaT,Texture,Dir,Dim,Min,Height;
//Preparing variables
Length = argument0; //How many previous coordinates will use the trail
Width = argument1; //How wide will the trail be
Color = argument2; //Which color will be used to tint the trail
Sprite = argument3; //Which sprite's texture must be used for the trail. Must have "Used for 3D" marked. -1 for no sprite
Slim = argument4; //Whether the trail will slim down at the end
Alpha = argument5; //The alpha to draw the trail with (0-1), optional
ArrayTrail[0,0] = x;
ArrayTrail[0,1] = y;
Height = array_height_2d(ArrayTrail);
//Getting distance between current and past coordinates
if (Height > 1) ArrayTrail[0,2] = point_distance(ArrayTrail[0,0],ArrayTrail[0,1],ArrayTrail[1,0],ArrayTrail[1,1]) + ArrayTrail[1,2];
else ArrayTrail[0,2] = 0;
//Setting the texture
if (Sprite >= 0) {
Texture = sprite_get_texture(Sprite,0);
Dim = sprite_get_width(Sprite)/sprite_get_width(Sprite)
}
else {
Texture = -1;
Dim = 1
}
texture_set_repeat(1);
//Drawing the primitive
draw_primitive_begin_texture(pr_trianglestrip,Texture);
AlphaT = 1;
Dir = 0;
Min = min(Height - 1,Length);
for(var i = 0; i < Min; i++){
  if (ArrayTrail[i,0] != ArrayTrail[i+1,0] || ArrayTrail[i,1] != ArrayTrail[i+1,1])
    Dir = point_direction(ArrayTrail[i,0],ArrayTrail[i,1],ArrayTrail[i+1,0],ArrayTrail[i+1,1]);
  var Len = Width / 2 - ((i + 1) / Min * Width / 2) * Slim;
  var XX = lengthdir_x(Len,Dir + 90);
  var YY = lengthdir_y(Len,Dir + 90);
  AlphaT = (Min - i) / (Min / 2) * Alpha;
  draw_vertex_texture_color(ArrayTrail[i,0] + XX,ArrayTrail[i,1] + YY,ArrayTrail[i,2] / Width/Dim,0,Color,AlphaT);
  draw_vertex_texture_color(ArrayTrail[i,0] - XX,ArrayTrail[i,1] - YY,ArrayTrail[i,2] / Width/Dim,1,Color,AlphaT);
}
draw_primitive_end();
//Replacing the coordinates positions within the array
Min = min(Height,Length);
for (var i = Min; i > 0; i--){
  ArrayTrail[i,0] = ArrayTrail[i - 1,0];
  ArrayTrail[i,1] = ArrayTrail[i - 1,1];
  ArrayTrail[i,2] = ArrayTrail[i - 1,2];
}

With that i'm good creating trails for object that are actually moving.
But for my game i have a fixed object inside a room with a moving background (so it creates the effect of a moving hero).
I can move the hero only up and down but NOT in his x axis, since all the other objects are moving towards him.
My question is, how can i draw a trail behind the hero even if its not moving? This will help a lot to create the effect of a "continuously moving hero".
Maybe there is a simplier way to code that without that script, but i can't figure it out.

Thanks.
 

Yal

šŸ§ *penguin noises*
GMC Elder
  1. Store a list of the hero's previous coordinates.
  2. Move all the coordinates down with the background speed each step.
  3. Draw the primitive between the points as normal.
 

CloseRange

Member
It might also just be simpler to have some 'trail' object that has the same image as the player.
Make it move left at the same speed as the background.
have the main hero create one every step
when it's off the screen, destroy the 'trail' object

Though I am a sucker for data-structures storing a list of all the coords would be annoying as you'd have to also remember to remove the entries (or I suppose you could reuse the entries by moving them back to the player's position)
However you guys are talking about drawing primitives and drawing them between the points and I neglected to read the code.
So perhaps you just have each 'trail' object have a 'parent' variable that points to the next 'trail' object so that you can draw between each one. At this point though you've just made a linked-list so I guess Yal was right. A list.
 
P

ProjectGamesInc

Guest
Thanks a lot, i will try to use ur tips to make it.
 
Top