u/RoGlassDev

The function draw_rectangle has some issues and can't be rotated, so I made 2 functions that are more flexible. Hopefully, they'll help you if you have a similar problem!

The function draw_rectangle has some issues and can't be rotated, so I made 2 functions that are more flexible. Hopefully, they'll help you if you have a similar problem!

Hey all! I'm pretty new to Gamemaker (trying it out for my newest game instead of Unreal) and I'm working on a game that uses pixel art. The function draw_rectangle was having issues with adding an extra 1 pixel line on two of the edges and doesn't have a built in way to rotate the rectangle being drawn.

I'm drawing tooltips by drawing a series of rectangles and text and this was the solution I came up with to allow rotation. It's certainly not the cleanest way of doing it, but I figured I'd share anyways.

Edit: I was adjusting angles and realized that there was a slight error with RotatePoint. I adjusted it in the code block var _angle_radians = _angle*pi/180; --> var _angle_radians = (_angle + 180)*pi/180;

//Draws a rectangle with color, alpha, and angle using a primative
function DrawRectangle(_origin_x, _origin_y, _x1, _y1, _x2, _y2, _color = c_white, _alpha = 1, _angle = 0)
{
    var _previous_color = draw_get_colour();
    var _previous_alpha = draw_get_alpha();
    draw_set_colour(_color);
    draw_set_alpha(_alpha);

    draw_primitive_begin(pr_trianglestrip);
    draw_vertex(RotatePoint(_origin_x, _origin_y, _x1, _y1, _angle)[0], RotatePoint(_origin_x, _origin_y, _x1, _y1, _angle)[1]);
    draw_vertex(RotatePoint(_origin_x, _origin_y, _x1, _y2, _angle)[0], RotatePoint(_origin_x, _origin_y, _x1, _y2, _angle)[1]);
    draw_vertex(RotatePoint(_origin_x, _origin_y, _x2, _y1, _angle)[0], RotatePoint(_origin_x, _origin_y, _x2, _y1, _angle)[1]);
    draw_vertex(RotatePoint(_origin_x, _origin_y, _x2, _y2, _angle)[0], RotatePoint(_origin_x, _origin_y, _x2, _y2, _angle)[1]);
    draw_primitive_end();

    draw_set_colour(_previous_color);
    draw_set_alpha(_previous_alpha);
}

//Rotates a point around an origin by the given angle in degrees
function RotatePoint(_x_origin, _y_origin, _x_point, _y_point, _angle)
{
    var _angle_radians = (_angle + 180)*pi/180;
    var _base_x = _x_origin - _x_point;
    var _base_y = _y_origin - _y_point;
    var _normal_x = _base_x*cos(_angle_radians) - _base_y*sin(_angle_radians);
    var _normal_y = _base_x*sin(_angle_radians) + _base_y*cos(_angle_radians);
    return [_x_origin + _normal_x, _y_origin + _normal_y];
}
u/RoGlassDev — 1 day ago