• 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!

Tiling a Chain-Like Object While Moving

P

Pyro

Guest
Hey guys,

I know my title was a bit cryptic, but I couldn't think of a better way to word it. I know there's probably a few ways I could go about doing this, but I'd like some thoughts. What do you think is the best way to go about drawing an object like a chain to the screen, but having it extend without stretching or anything weird, like a grappling hook coming out of a gun or something. Essentially, I'd want to draw a small tileable sprite a number of times at an angle between two points so that it looks like one continuous chain.

Any ideas?

Thanks,
-Pyro.
 
Basically, make a loop for as many links as you need, then within the loop, use the lengthdir_x/y functions to find the correct position along the line, and use draw_sprite/_ext at that position.
 
P

Pyro

Guest
I think that would definitely work on objects that weren't moving, but say for the example of a grapling hook where the hook moves out and then potentially back, using the 'for loop' method, wouldn't that just draw the sprites in set positions thus making the end of the hook move, but not the actual links?
 

jo-thijs

Member
As an alternative for what stainedofmind suggested,
when editing the sprite, check "Tile: Horizontal".
You can now use draw_primitive_begin_texture to with 4 vertices to do this.

EDIT: And no, using a for loop, you have complete control over where the chains are drawn and how they move.
 
P

Pyro

Guest
Does "Tile:Horizontal" within a primitive texture allow it to be rotated? I've never tried it, nor have I ever actually used texturing like that, but my first thought is that Horizontal tiling only applies for the X-axis rather than a manipulated and rotated primitive shape.

And yes, I know that I have complete control. I should have phrased my previous reply better. I guess I just have to check the distance between the hook and the source, divide that by the length of the individual link sprite, and update it as the hook moves to not have the sprites be static for the duration of the hook.
 

jo-thijs

Member
"Tile Horizontal" means horizontally for the sprite.
However, through primitives, you can deform the texture by using any affine transformation on any triangle in the texture to a triangle on screen.
Using 2 triangles, you can use any affine transformation on rectangles.
Primitives are fun and useful things, I advice reading up on them.

As for the for loop, yes, but you would usually recalculate everything every frame in the draw event.
Things vary too much over multiple steps to really keep track of previous calculations.
 
P

Pyro

Guest
Got it working perfectly on my first try. For anyone else who may be interested, here's the code to draw the "chains" of a grappling hook like I want to do it:

Code:
var dist = point_distance(x, y, obj_player.x, obj_player.y);
var spr_width = sprite_get_width(spr_chain);
var angle_player = point_direction(x, y, obj_player.x, obj_player.y);

var num = dist/spr_width;
var gap = dist/num;

for(var i=0;i<num;i+=1)
{
    draw_sprite_ext(spr_chain, -1, x + lengthdir_x(i*gap, angle_player), y + lengthdir_y(i*gap, angle_player), image_xscale, image_yscale, angle_player, c_white, image_alpha);
}
Here's a gif of it working:
 
Top