GameMaker [Unsolved/Closed] Rotate Around Center Using draw_sprite_general

S

Sahibjot Cheema

Guest
I want to draw a gun sprite and have it look towards the mouse. So far the rotation is based around where the drawing is made which is the top left. How can I make it rotate around the center of the drawn image using
draw_sprite_general. I know using draw_sprite_ext fixes this but I would like to use draw_sprite_general.

I decided to record it and post the code and its much simpler. Here is the video.

The gun on the right:
Code:
var dir        = point_direction(x, y, mouse_x, mouse_y);
var facing    = 1;
if (dir > 90 && dir < 270) facing = -1;
draw_sprite_ext(spr_gun, 0, x, y, 1, facing,dir, image_blend, image_alpha);
The gun on the left:
Code:
var dir        = point_direction(x, y, mouse_x, mouse_y);
var facing    = 1;
if (dir > 90 && dir < 270) facing = -1;
draw_sprite_general(spr_gun, 0, 0, 0, 48, 48, x, y, 1, facing, dir, image_blend, image_blend, image_blend, image_blend, image_alpha);
The sprites origin point is middle center and its 48 by 48.

EDIT: Closed. Staff said its possible but I shouldn't do it.
 
Last edited by a moderator:
Code:
/// PLAYER DRAW EVENT
draw_self();

var dir = point_direction(x, y, mouse_x, mouse_y);
var gw = sprite_get_width(sGun);
var gh = sprite_get_height(sGun);
var xoff = sprite_get_width(sPlayer) / 2;
var yoff = sprite_get_height(sPlayer) / 2;
draw_sprite_general(sGun, 0, 0, 0, gw, gh, x + xoff, y + yoff, image_xscale, image_yscale, dir, image_blend, image_blend, image_blend, image_blend, image_alpha);
Also make sure the Gun sprite is facing to the right.
 
S

Sahibjot Cheema

Guest
Code:
/// PLAYER DRAW EVENT
draw_self();

var dir = point_direction(x, y, mouse_x, mouse_y);
var gw = sprite_get_width(sGun);
var gh = sprite_get_height(sGun);
var xoff = sprite_get_width(sPlayer) / 2;
var yoff = sprite_get_height(sPlayer) / 2;
draw_sprite_general(sGun, 0, 0, 0, gw, gh, x + xoff, y + yoff, image_xscale, image_yscale, dir, image_blend, image_blend, image_blend, image_blend, image_alpha);
Also make sure the Gun sprite is facing to the right.
Nope, nothing really changed besides it x and y locations. It still rotates around its top left axis.
 
You're shackling yourself by trying to make it rotate around the center when it's rotation point is not around the center. Whatever is preventing you from being able to center the origin on your gun is the real problem you need to fix.
 
With draw_sprite_general, draw your sprite at (x1,y1):

x1 = x + c * -w/2 + s * -h/2;
y1 = y + -s * -w/2 + c * -h/2;

(x,y) is the location of your pivot point in the world. (c,s) are dcos(angle) and dsin(angle), where of angle is the measure in degrees of the rotation. (w,h) are the width and height of the whole sprite.

I just want to be sure though that we are trying to solve the correct problem. I have a hunch that your real problem can be addressed more directly. Let us know if that is the case.

IMPORTANT NOTE: If your sprite image is surrounded by transparent area, and you have cropping turned on for your texture group, then you will see jittery placement of your sprite when drawing with draw_sprite_general.
 
Last edited:
S

Sahibjot Cheema

Guest
Here is a quick copy of my project, the gun on the right side uses ext and is what I want, the gun on the left uses general and is bugged. Any why to make the gun on the left act like the one on the right.
With draw_sprite_general, draw your sprite at (x1,y1):

x1 = x + c * -w/2 + s * -h/2;
y1 = y + -s * -w/2 + c * -h/2;

(x,y) is the location of your pivot point in the world. (c,s) are dcos(angle) and dsin(angle), where of angle is the measure in degrees of the rotation. (w,h) are the width and height of the whole sprite.

I just want to be sure though that we are trying to solve the correct problem. I have a hunch that your real problem can be addressed more directly. Let us know if that is the case.

IMPORTANT NOTE: If your sprite image is surrounded by transparent area, and you have cropping turned on for your texture group, then you will see jittery placement of your sprite when drawing with draw_sprite_general.
While this note has nothing to do with my current project, this might actually be a fix to a bug I was experiencing on another one. Thx
 
Last edited by a moderator:
Here is a quick copy of my project, the gun on the right side uses ext and is what I want, the gun on the left uses general and is bugged. Any why to make the gun on the left act like the one on the right.

While this note has nothing to do with my current project, this might actually be a fix to a bug I was experiencing on another one. Thx
Your file is damaged when I try to download it, it can't be opened.
 
S

Sahibjot Cheema

Guest
Your file is damaged when I try to download it, it can't be opened.
Oh my bad. I decided to record it and post the code and its much simpler. Here is the video.
The gun on the right:
Code:
var dir        = point_direction(x, y, mouse_x, mouse_y);
var facing    = 1;
if (dir > 90 && dir < 270) facing = -1;
draw_sprite_ext(spr_gun, 0, x, y, 1, facing,dir, image_blend, image_alpha);
The gun on the left:
Code:
var dir        = point_direction(x, y, mouse_x, mouse_y);
var facing    = 1;
if (dir > 90 && dir < 270) facing = -1;
draw_sprite_general(spr_gun, 0, 0, 0, 48, 48, x, y, 1, facing, dir, image_blend, image_blend, image_blend, image_blend, image_alpha);
The sprites origin point is middle center and its 48 by 48.
 
Top