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

Draw rotate (SOLVED)

N

NoFontNL

Guest
Hello, I want to draw 2 sprites above one another and draw them rotated.
upload_2019-6-27_15-44-31.pngupload_2019-6-27_15-44-43.png

Image angle only changes the object's sprite rotation.

And when using draw_sprite_ext, the sprites only get rotated around their own center point,
which results into this:
upload_2019-6-27_15-43-56.png

Any idea how to rotate them both around one specified center point, while not having to change the sprites' center point?
 

Attachments

YoSniper

Member
Define the center point, then the offsets. The rest is trig.

Code:
var x_offset_1, y_offset_1, x_offset_2, y_offset_2;

x_offset_1 = <sprite1 initial x offset> * dcos(angle) + <sprite1 initial y offset> * dsin(angle);
y_offset_1 = <sprite1 initial y offset> * dcos(angle) - <sprite1 initial x offset> * dsin(angle);
x_offset_2 = <sprite2 initial x offset> * dcos(angle) + <sprite2 initial y offset> * dsin(angle);
y_offset_2 = <sprite2 initial y offset> * dcos(angle) - <sprite2 initial x offset> * dsin(angle);

draw_sprite_ext(sprite1, index1, center_x + x_offset_1, center_y + y_offset_1, xscale, yscale, angle, c_white, 1);
draw_sprite_ext(sprite2, index2, center_x + x_offset_2, center_y + y_offset_2, xscale, yscale, angle, c_white, 1);
 
N

NoFontNL

Guest
Code:
/// draw_sprites_angle(cx,cy,angle,si_1,ii_1,xo_1,yo_1,si_2, ii_2, xo_2, yo_2 ...)
// cx: Center X : real
// cy: Center Y : real
// angle: angle : real
// si_n: Sprite Index : real
// ii_n: Image Index : real
// xo_n: x offset : real
// yo_n: y offset : real
// Example: draw_sprites_angle(x,y,90,sp_tile1,0,0,64,sp_tile2,0,0,128,sp_tile3,0,0,192);

var center_x = argument0;
var center_y = argument1;
var angle = argument2;
var si, ii, x_offset, y_offset;

for (var i = 3; i<argument_count; i+=2){
        if(i+3>=argument_count){
                continue;
        }
        si[i] = argument[i];
        ii[i] = argument[i+1];
     
        x_offset[i] = argument[i+2] * dcos(angle) + argument[i+3] * dsin(angle);
        y_offset[i] = argument[i+3] * dcos(angle) - argument[i+2] * dsin(angle);
       draw_sprite_ext(si[i], ii[i], center_x + x_offset[i], center_y + y_offset[i], xscale, yscale, angle, draw_get_color(), draw_get_alpha());
}
Tried making a script out of this, would this work?
And another question. Does this work with different center points of the sprites themselves?
 

YoSniper

Member
Looks good at first glance. And yes, a script would help in such a case.

And yes, this should work fine with sprites having different origins. It's just the x and y offsets that need to be accounted for.
 
N

NoFontNL

Guest
Still having trouble. Just added the script, ran it from a test object. Got this:
upload_2019-6-27_17-2-13.png
 

chamaeleon

Member
Still having trouble. Just added the script, ran it from a test object. Got this:
View attachment 25443
GMS (current version 2 anyway), does not like mixing the use of argument0, argument1, etc., with argument[0], argument[1], and so on. Pick one style or the other. In this case, presumably the array version is better given a loop over the set provided arguments.
 
N

NoFontNL

Guest
Above script isn't working right, I made some minor changes that broke it.
Here is the full working script thanks to all people above that helped me especially YoSniper for the base with those mathematical expressions stuff and chamaeleon to finish it. (didn't know about the difference between argument0 and argument[0])
Code:
/// draw_sprites_angle(cx,cy,angle,si_1,ii_1,xo_1,yo_1,si_2, ii_2, xo_2, yo_2 ...)
// cx: Center X : real
// cy: Center Y : real
// angle: angle : real
// si_n: Sprite Index : real
// ii_n: Image Index : real
// xo_n: x offset : real
// yo_n: y offset : real
// Example: draw_sprites_angle(x,y,90,sp_tile1,0,0,64,sp_tile2,0,0,128,sp_tile3,0,0,192);
// Thread: https://forum.yoyogames.com/index.php?threads/draw-rotate.64780/

var center_x = argument[0];
var center_y = argument[1];
var angle = argument[2];
var si, ii, x_offset, y_offset;

for (var i = 3; i<argument_count; i+=4){
        if(i+3>=argument_count){
                continue;
        }
        si[i] = argument[i];
        ii[i] = argument[i+1];
     
        x_offset[i] = argument[i+2] * dcos(angle) + argument[i+3] * dsin(angle);
        y_offset[i] = argument[i+3] * dcos(angle) - argument[i+2] * dsin(angle);
       draw_sprite_ext(si[i], ii[i], center_x + x_offset[i], center_y + y_offset[i], image_xscale, image_yscale, angle, c_white, draw_get_alpha());
}
 
Top