GameMaker Drawing a rotated rectangle from object position

Shavv

Member
Heey guys!

I found a neat little script here to draw a rotated rectangle, wich is really fun but I can't seem to solve the issue im having with it.

its drawing the rectangle in the exact middle of the object, and i want it to draw from the object forward to the direction i want it to.

Screenshots:

Whats happening now:


What I initially want:




Code:
Code:
/// @description draw_rectangle_rotated(x,y,width,height,rotation,outline)
/// @param x
/// @param y
/// @param width
/// @param height
/// @param rotation
/// @param outline
var cx,cy,w,h,rot,sxx,sxy,syx,syy,x1,x2,x3,x4,y1,y2,y3,y4;
cx=argument0;
cy=argument1;
w=argument2/2;
h=argument3/2;
rot=argument4;

sxx = cos(degtorad(rot));
sxy = -sin(degtorad(rot));
syx = -sin(degtorad(rot));
syy = -cos(degtorad(rot));

x1=cx-sxx*w-syx*h;
x2=cx+sxx*w-syx*h;
x3=cx+sxx*w+syx*h;
x4=cx-sxx*w+syx*h;

y1=cy-sxy*w-syy*h;
y2=cy+sxy*w-syy*h;
y3=cy+sxy*w+syy*h;
y4=cy-sxy*w+syy*h;

if argument5=true
{
    draw_line(x1,y1,x4,y4);
    draw_line(x2,y2,x1,y1);
    draw_line(x3,y3,x2,y2);
    draw_line(x4,y4,x3,y3);
}
else
{
    draw_triangle(x1,y1,x2,y2,x3,y3,false);
    draw_triangle(x1,y1,x3,y3,x4,y4,false);
}

I can't seem to wrap my head around how i could change the initial position without centering it in the middle of the object.


Thanks for your kind help!
 
To use the script without having to change it you can add the position.

Code:
tx = lenghtdir_x(width / 2, rotation)
ty = lenghtdir_y(width / 2, rotation)
Where width is the same value as you put in width and rotation is the same value as you put in rotation.
tx and ty should then be added to what you already put in x and y e.g. x + tx and y + ty
 

Shavv

Member
To use the script without having to change it you can add the position.

Code:
tx = lenghtdir_x(width / 2, rotation)
ty = lenghtdir_y(width / 2, rotation)
Where width is the same value as you put in width and rotation is the same value as you put in rotation.
tx and ty should then be added to what you already put in x and y e.g. x + tx and y + ty
Thanks for the reply, i did this and nothing much changed sadly:





Code:

Code:
/// @description draw_rectangle_rotated(x,y,width,height,rotation,outline)
/// @param x
/// @param y
/// @param width
/// @param height
/// @param rotation
/// @param outline
var cx,cy,w,h,rot,sxx,sxy,syx,syy,x1,x2,x3,x4,y1,y2,y3,y4,tx,ty;
cx=argument0;
cy=argument1;
w=argument2/2;
h=argument3/2;
rot=argument4;

sxx = cos(degtorad(rot));
sxy = -sin(degtorad(rot));
syx = -sin(degtorad(rot));
syy = -cos(degtorad(rot));

tx = lengthdir_x(w/2,rot)
ty = lengthdir_y(w/2,rot)

x1=cx-sxx*w-syx*h;
x2=cx+sxx*w-syx*h;
x3=cx+sxx*w+syx*h;
x4=cx-sxx*w+syx*h;

y1=cy-sxy*w-syy*h;
y2=cy+sxy*w-syy*h;
y3=cy+sxy*w+syy*h;
y4=cy-sxy*w+syy*h;

if argument5=true
{
    draw_line(x1+tx,y1+ty,x4+tx,y4+ty);
    draw_line(x2+tx,y2+ty,x1+tx,y1+ty);
    draw_line(x3+tx,y3+ty,x2+tx,y2+ty);
    draw_line(x4+tx,y4+ty,x3+tx,y3+ty);
}
else
{
    draw_triangle(x1,y1,x2,y2,x3,y3,false);
    draw_triangle(x1,y1,x3,y3,x4,y4,false);
}

EDIT: Note that i didn't change the argument5=false drawing, because im only testing with an outline :)
 

DesArts

Member
You don't need to /w by 2 inside the script because that's already done at the start. Also, which is the width and which is the height? Looks like the rectangle shifted along the shorter direction and not the long one, so maybe height is what you want to use for that.

Maybe show the code where you use the script?
 

Shavv

Member
You don't need to /w by 2 inside the script because that's already done at the start. Also, which is the width and which is the height? Looks like the rectangle shifted along the shorter direction and not the long one, so maybe height is what you want to use for that.

Maybe show the code where you use the script?
I already tried swapping width for height and came out with this:


code im using:

Code:
draw_rectangle_rotated(x,y,10,80,leaderdir,1)

EDIT:

I noticed that i swapped around the width and height and corrected it to:
Code:
draw_rectangle_rotated(x,y,80,10,leaderdir,1)
and now have this (almost there):

 
I

ItsRealGaming

Guest
Ok, now is the real issue. Your case is the outline of this script is true, now I want the the outline is false. If the outline is false (fill the rectangle), the first issue is back but with filled rectangle. How do we solve this?
 
Last edited by a moderator:
Modified the script to act more like the draw_rectangle function. With the x, y as the origin of rotation & draw position, and the x1, y1, x2, y2 params are offsets without rotation.

example of use in OP case, draw_rectangle_rotated(x, y, 0, -5, 80, 5, leaderdir, 1);

Code:
/// @desc draw rectangle primitive rotated
/// @func draw_rectangle_rotated(x,y,x1,y1,x2,y2,rotation,outline)
/// @param x
/// @param y
/// @param x1
/// @param y1
/// @param x2
/// @param y2
/// @param rotation
/// @param outline
/// @returns n/a
var cx,cy,xx1,yy1,xx2,yy2,rot,sxx,sxy,syx,syy,x1,x2,x3,x4,y1,y2,y3,y4;

cx=argument0;    // Origin position x
cy=argument1;    // Origin position y
xx1=argument2;    // Offset x1
yy1=argument3;  // Offset y1
xx2=argument4;  // Offset x2
yy2=argument5;  // Offset y2
rot=argument6;    // Angle

sxx = cos(degtorad(rot));
sxy = -sin(degtorad(rot));
syx = sin(degtorad(rot));
syy = cos(degtorad(rot));

x1=cx+sxx*xx1+syx*yy1;
x2=cx+sxx*xx2+syx*yy1;
x3=cx+sxx*xx2+syx*yy2;
x4=cx+sxx*xx1+syx*yy2;

y1=cy+sxy*xx1+syy*yy1;
y2=cy+sxy*xx2+syy*yy1;
y3=cy+sxy*xx2+syy*yy2;
y4=cy+sxy*xx1+syy*yy2;

if (argument7)
{
    draw_line(x1,y1,x4,y4);
    draw_line(x2,y2,x1,y1);
    draw_line(x3,y3,x2,y2);
    draw_line(x4,y4,x3,y3);
}
else
{
    draw_triangle(x1,y1,x2,y2,x3,y3,false);
    draw_triangle(x1,y1,x3,y3,x4,y4,false);
}
 

TibiSoft

Member
It might be obvious, but to use the above function draw_rectangle_rotated() in 'normal' condition (eg. as a bounding box draw function) then it is like that:
draw_rectangle_rotated(x,y, -sprite_xoffset, -sprite_yoffset, sprite_width-sprite_xoffset, sprite_height-sprite_yoffset, image_angle, true);
 
Top