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

Take a chunk out of a sprite?

T

teamrocketboyz

Guest
Hello all im wondering how to do this or if its even possible.

Is it possible to have a bullet object take a chunk off of a sprite? kind of like what happens in the worms games when a grenade hits the land?
 
D

dannyjenn

Guest
What you're talking about is called "destructible terrain", and yes, it's possible. I've never done it before though, so I'm not sure what the best way to go about it would be. I personally would probably just use surfaces, though this doesn't allow for collision detection (as far as I'm aware).
 
So, danny's right that you need a surface. Basically, what you do is you draw your sprite onto a surface, you cut the holes in it with bm_subtract and the appropriate mask for the weapon/explosion/whatever, and then, if you wanna make a scorch effect around the hole, you'd put it on with bm_dest_alpha, bm_inv_src_alpha; then you set the surface as the new sprite for the terrain (with precise collisions). Relatively simple affair tbh, but it's not necessarily something you'd realise just from reading the manual or whatever.
 

Joe Ellis

Member
@RefresherTowel then you set the surface as the new sprite for the terrain (with precise collisions)
This isn't needed, In my thing, I made an array as the mask and made a separate collision system,
cus when you blow holes in stuff, making a new collision mask out of that causes huge lag, so I just made it so it cuts a hole in the image, then a synced array with all the precise collision per pixel also gets processed, I made the collision mask processing async so it causes no lag

https://forum.yoyogames.com/index.php?threads/2d-destructible-terrain-starter-kit.47954/
 
T

teamrocketboyz

Guest
@RefresherTowel then you set the surface as the new sprite for the terrain (with precise collisions)
This isn't needed, In my thing, I made an array as the mask and made a separate collision system,
cus when you blow holes in stuff, making a new collision mask out of that causes huge lag, so I just made it so it cuts a hole in the image, then a synced array with all the precise collision per pixel also gets processed, I made the collision mask processing async so it causes no lag

https://forum.yoyogames.com/index.php?threads/2d-destructible-terrain-starter-kit.47954/
ive been following a tutorial on youtube and im unsure if his works because he isnt using gms2 or not, ive copied it exactly. i can make holes where i click on a sprite but the first initial click quarters the size of the sprite and then i can put holes in it. a circle becomes like a pizza slice exactly a quarter of it.

heres my code

Code:
create event

hit = false

damage_x = 0;
damage_y = 0;
Code:
left pressed

var mx = mouse_x - x;
var my = mouse_y - y;

damage_x = mx;
damage_y = my;

hit = true;
Code:
draw begin

if hit = true {
    
    var temp_surf = surface_create(sprite_width,sprite_height)
    
    surface_set_target(temp_surf)
    
    draw_sprite(sprite_index,0,0,0)
    
    gpu_set_blendmode(bm_subtract)
    
    draw_set_color(c_black)
    
    draw_circle(damage_x,damage_y,12,false)
    
    gpu_set_blendmode(bm_normal)
    
    sprite_index = sprite_create_from_surface(temp_surf,0,0,sprite_width,sprite_height,false,false,0,0)
    
    surface_free(temp_surf)
    
    sprite_collision_mask(sprite_index,false,0,0,0,0,0,0,0)
    
    
    
    hit = false
}
 

Joe Ellis

Member
That looks fine except there's no draw_clear_alpha(0, 0), for the transparent background, but I don't know if gms2 does this automatically with a new surface.

It could be the view\projection needs to be set for drawing on the surface, I always use d3d_set_projection_ortho(0, 0, surface_width, surface_height, 0) and it stops any problems,
I don't know what the equivalent of that is in gms2, I just tried to look but couldn't find it, you've probably got more idea than me.

If the sprite has scaling and rotation when it's drawn, that'd make it more complicated for the bullet to take the chunk out in the right place, cus you'd have to transform the bullet's position into the non-transformed image of the sprite, with the inverse of the sprite's transform.
 
Last edited:
Top