A Rectangle with Bubbles? :O
Today I've been wondering how could I draw something within a specified area like this:
That bubble is sneaky! Be careful!
And of course, there seem to be multiple ways to achieve that, yet I've picked a pretty much basic one. My favorite!
So besides obvious x and y position of the image bubble, I had these variables available:
- left offset (basically cutting the left part)
- top offset (same, but vertically - it cuts the top)
- width (the lower the value, the more cutoff from the right side the image bubble is)
- height (same, but vertically - it cuts the bottom)
After doing some visual representation, maths and silly mistakes, to help me imagine the situation better:
I came up with this code:
///draw_self_rect(rect.x, rect.y, rect.w, rect.h, x, y)
// 0 1 2 3 4 5
var spr_x = round(argument[4]);
var spr_y = round(argument[5]);
//---------------------------------------------
// Don't draw if the sprite is outside the rect
//---------------------------------------------
// left border // x < rect.x - spr.w
if (spr_x < argument[0] - sprite_width) { exit };
// right border // x > rect.x + rect.w
if (spr_x > argument[0] + argument[2]) { exit };
// top border // y < rect.y - spr.h
if (spr_y < argument[1] - sprite_height) { exit };
// bottom border // y > rect.y + rect.h
if (spr_y > argument[1] + argument[3]) { exit };
//---------------------------------------------
// Cut-off the sprite to fit it in the rect
//---------------------------------------------
// left = rect.x - x = horizontal-left cut (0 ~ spr.w)
var left = clamp(argument[0] - spr_x, 0, sprite_width);
// top = rect.y - y = vertical-top cut (0 - spr.h)
var top = clamp(argument[1] - spr_y, 0, sprite_height);
// width = rect.w + rect.x - x = horizontal-right cut (0 - spr.w)
var width = clamp(argument[2] + argument[0] - spr_x, 0, sprite_width);
// height = rect.h + rect.y - y = vertical-bottom cut (0 - spr.h)
var height = clamp(argument[3] + argument[1] - spr_y, 0, sprite_height);
//---------------------------------------------
// Draw!
//---------------------------------------------
draw_sprite_part(sprite_index, image_index, left, top, width, height, spr_x+left, spr_y+top);
Calling this script basically cuts the sprite if it goes beyond the specified borders, eventually doesn't draw at all if there's no point in making further calculations.
And voila! There are some bubbles within a specified area!
You're trapped, ya silly bubbles!
I'm pretty sure, that soon it will turn out, that there was a much better way and I was just unnecessarily over-complicating all of this, just like always.
So YAY! Some progress is finally being made!
Oh it all would be done ages ago if not the job. It goes sooooo slowly.
- 4
7 Comments
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Join the herd!Sign in
Already have an account? Sign in here.
Sign In Now