Legacy GM Problem using lengthdir_x/y to find the point

I

Ian

Guest
So im trying to do 360 degree turning in a sidescroller and stuck on finding the tip of the gun to shoot.

Code:
///[Step]Gun rotation
gun_angle = point_direction(x,y,mouse_x,mouse_y);
if(gun_angle > 90 && gun_angle < 270) gun_yscale = -1 else gun_yscale = 1;

image_xscale = gun_yscale;
Code:
///Draw event
//Player
draw_self();
//Gun
draw_sprite_ext(spr_uzi,0,x-(3*image_xscale),y+2,1,gun_yscale,gun_angle,c_white,1);

//End point
var xx = x+lengthdir_x(14,gun_angle);
var yy = y+lengthdir_y(14,gun_angle);
draw_sprite_ext(spr_pixel,0,xx,yy,1,gun_yscale,gun_angle,c_white,1);


Found out since lengthdir funcs go off the canter point of the object i could just move the y offset in sprite editor to where i needed it do

Offset before:


Offset Changed to: (moved 2 pixels up, where the barrel is)


Changing the offset fixed it:


But much rather keep the y offset from before since it effects the gun rotation.

So how would i be able to find the guns point since i cannot simple use:
Code:
//End point
var xx = x+lengthdir_x(14,gun_angle);
var yy = y+lengthdir_y(14,gun_angle);
with that offset.
 

HayManMarc

Member
I'd like to tell you, but I can't. It's a secret.

(Just kidding.)

I can tell you, but my hourly fee is $30.

So to begin, I would use the sprite origin of the arm in the first gif -- the one where it's centered on the shoulder. It looks better.

Then, to find the tip of the gun, use the lengthdir functions like you did, but you also need to use it to offset your origin.
Here's what I did when I tested it:

CODE
Code:
// CREATE
// declare vars
gun_x = 0;
gun_y = 0;

gun_angle = 0;

gun_xscale = 1;

gun_x_offset = 0;
gun_y_offset = 0;


// STEP
// gun-arm placement
gun_x = x - (3 * image_xscale);
gun_y = y - 8;
 
//gun rotation
gun_angle = point_direction(gun_x, gun_y, mouse_x, mouse_y);
if (gun_angle > 90 && gun_angle < 270) gun_xscale = -1 else gun_xscale = 1;

image_xscale = gun_xscale;


//DRAW
//Player
draw_self();
//Gun
draw_sprite_ext(spr_uzi, 0, gun_x, gun_y, 1, gun_xscale, gun_angle, c_white, 1);

// origin offset
gun_x_offset = gun_x + lengthdir_x(2, gun_angle + (90 * gun_xscale));
gun_y_offset = gun_y + lengthdir_y(2, gun_angle + (90 * gun_xscale));

//End point
var xx = gun_x_offset + lengthdir_x(14, gun_angle);
var yy = gun_y_offset + lengthdir_y(14, gun_angle);
draw_sprite_ext(spr_pixel, 0, xx, yy, 1, gun_xscale, gun_angle, c_white, 1);

(Note: I changed your "gun_yscale" to "gun_xscale" because that made more sense to me. You'd have to change it back for your code. You may also need to adjust your "gun-arm placement" coords.)

Ok, that took about an hour and a half, so I'll need to collect 45 bucks from ya. Thanks!

(Just kidding.)


*****EDIT*****
I added movement to play around with it and realized that you will need to put the lengthdir code from the DRAW event into the STEP event to make the arm stay in place while the character is moving around. Like so...

Code:
// CREATE
// declare vars
gun_x = 0;
gun_y = 0;

gun_angle = 0;

gun_xscale = 1;

gun_x_offset = 0;
gun_y_offset = 0;


// STEP
// gun-arm placement
gun_x = x - (3 * image_xscale);
gun_y = y - 8;

//gun rotation
gun_angle = point_direction(gun_x, gun_y, mouse_x, mouse_y);
if (gun_angle > 90 && gun_angle < 270) gun_xscale = -1 else gun_xscale = 1;

image_xscale = gun_xscale;

// origin offset
gun_x_offset = gun_x + lengthdir_x(2, gun_angle + (90 * gun_xscale));
gun_y_offset = gun_y + lengthdir_y(2, gun_angle + (90 * gun_xscale));

//End point
var xx = gun_x_offset + lengthdir_x(14, gun_angle);
var yy = gun_y_offset + lengthdir_y(14, gun_angle);
draw_sprite_ext(spr_pixel, 0, xx, yy, 1, gun_xscale, gun_angle, c_white, 1);


//DRAW
//Player
draw_self();
//Gun
draw_sprite_ext(spr_uzi, 0, gun_x, gun_y, 1, gun_xscale, gun_angle, c_white, 1);
 
Last edited:
  • Like
Reactions: Ian
I

Ian

Guest
I'd like to tell you, but I can't. It's a secret.

(Just kidding.)

I can tell you, but my hourly fee is $30.

So to begin, I would use the sprite origin of the arm in the first gif -- the one where it's centered on the shoulder. It looks better.

Then, to find the tip of the gun, use the lengthdir functions like you did, but you also need to use it to offset your origin.
Here's what I did when I tested it:

CODE
Code:
// CREATE
// declare vars
gun_x = 0;
gun_y = 0;

gun_angle = 0;

gun_xscale = 1;

gun_x_offset = 0;
gun_y_offset = 0;


// STEP
// gun-arm placement
gun_x = x - (3 * image_xscale);
gun_y = y - 8;
 
//gun rotation
gun_angle = point_direction(gun_x, gun_y, mouse_x, mouse_y);
if (gun_angle > 90 && gun_angle < 270) gun_xscale = -1 else gun_xscale = 1;

image_xscale = gun_xscale;


//DRAW
//Player
draw_self();
//Gun
draw_sprite_ext(spr_uzi, 0, gun_x, gun_y, 1, gun_xscale, gun_angle, c_white, 1);

// origin offset
gun_x_offset = gun_x + lengthdir_x(2, gun_angle + (90 * gun_xscale));
gun_y_offset = gun_y + lengthdir_y(2, gun_angle + (90 * gun_xscale));

//End point
var xx = gun_x_offset + lengthdir_x(14, gun_angle);
var yy = gun_y_offset + lengthdir_y(14, gun_angle);
draw_sprite_ext(spr_pixel, 0, xx, yy, 1, gun_xscale, gun_angle, c_white, 1);

(Note: I changed your "gun_yscale" to "gun_xscale" because that made more sense to me. You'd have to change it back for your code. You may also need to adjust your "gun-arm placement" coords.)

Ok, that took about an hour and a half, so I'll need to collect 45 bucks from ya. Thanks!

(Just kidding.)


*****EDIT*****
I added movement to play around with it and realized that you will need to put the lengthdir code from the DRAW event into the STEP event to make the arm stay in place while the character is moving around. Like so...

Code:
// CREATE
// declare vars
gun_x = 0;
gun_y = 0;

gun_angle = 0;

gun_xscale = 1;

gun_x_offset = 0;
gun_y_offset = 0;


// STEP
// gun-arm placement
gun_x = x - (3 * image_xscale);
gun_y = y - 8;

//gun rotation
gun_angle = point_direction(gun_x, gun_y, mouse_x, mouse_y);
if (gun_angle > 90 && gun_angle < 270) gun_xscale = -1 else gun_xscale = 1;

image_xscale = gun_xscale;

// origin offset
gun_x_offset = gun_x + lengthdir_x(2, gun_angle + (90 * gun_xscale));
gun_y_offset = gun_y + lengthdir_y(2, gun_angle + (90 * gun_xscale));

//End point
var xx = gun_x_offset + lengthdir_x(14, gun_angle);
var yy = gun_y_offset + lengthdir_y(14, gun_angle);
draw_sprite_ext(spr_pixel, 0, xx, yy, 1, gun_xscale, gun_angle, c_white, 1);


//DRAW
//Player
draw_self();
//Gun
draw_sprite_ext(spr_uzi, 0, gun_x, gun_y, 1, gun_xscale, gun_angle, c_white, 1);
Lengthdir-ception. Thank you, your'e a gentlemen and a scholar. Totally understand it now.
 
Top