Legacy GM help image_angle gun problem

D

dawson2223

Guest
Hey,

I am trying to add a fire animation at the tip of my gun. for this I am using an instance_create in the step event when the gun fires and used length_dir so I could measure the tip of the gun from the origin... (Gun origin was 8,14. Gun tip was 26,5... Difference brings me to 18,9).
My code was:
Code:
 instance_create(x + lengthdir_x(18,image_angle),y + lengthdir_y(9,image_angle),obj_testing);
It works fine when the gun is facing a horizontal direction. the instance is created right at the tip of the gun wherever it is pointing... However, when the gun is facing up or down, the object gets created in the center of the gun instead of the tip... I don't understand why. Can anyone help?

Thanks!
 
P

Panlizak

Guest
Maybe, its because you are using different distance values for lengthdir_x and lengthdir_y. They need to have the same value to act as a circle.
 
Last edited by a moderator:

hippyman

Member
When you need it to be at different offsets you need to go one more step.

Code:
var dx = 18;
var dy = 9;

var len = point_distance(0,0,dx,dy);
var dir = point_direction(0,0,dx,dy);

instance_create(x + lengthdir_x(len,dir+image_angle),y + lengthdir_y(len,dir+image_angle),obj_testing);
There is a tutorial on the old GMC that better explains this but I can't find it. This should do what you want though.
 
D

dawson2223

Guest
When you need it to be at different offsets you need to go one more step.

Code:
var dx = 18;
var dy = 9;

var len = point_distance(0,0,dx,dy);
var dir = point_direction(0,0,dx,dy);

instance_create(x + lengthdir_x(len,dir+image_angle),y + lengthdir_y(len,dir+image_angle),obj_testing);
There is a tutorial on the old GMC that better explains this but I can't find it. This should do what you want though.
That worked perfectly for the entire left half of the character, but on the right the object is being created on the bottom of the gun instead of the tip. Do you know why?
 
D

dawson2223

Guest
That worked perfectly for the entire left half of the character, but on the right the object is being created on the bottom of the gun instead of the tip. Do you know why?
It happens when the gun sprite flips.
 
so if you know your x and y displacements, let's call them dx and dy:

var _c = dcos(image_angle);
var _s = dsin(image_angle);

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

(_x, _y) is where the displacement (dx, dy) would be located when rotated at the angle "image_angle"

Im assuming you are making a side-view game and are using image_yscale to mirror your gun sprite when the gun is facing toward the left, if you are using a different variable, just substitute that one for image_yscale. Though image_yscale is what people usually use in this case.

EDITED
 
Last edited:
D

dawson2223

Guest
so if you know your x and y displacements, let's call them dx and dy:

var _c = dcos(image_angle);
var _s = dsin(image_angle);

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

(_x, _y) is where the displacement (dx, dy) would be located when rotated at the angle "image_angle"

Im assuming you are making a side-view game and are using image_yscale to mirror your gun sprite when the gun is facing toward the left, if you are using a different variable, just substitute that one for image_yscale. Though image_yscale is what people usually use in this case.

EDITED
it's 4 directional but yeah I'm using image_yscale!!! I'm still trying to understand your coding right now lol.
 
D

dawson2223

Guest
so if you know your x and y displacements, let's call them dx and dy:

var _c = dcos(image_angle);
var _s = dsin(image_angle);

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

(_x, _y) is where the displacement (dx, dy) would be located when rotated at the angle "image_angle"

Im assuming you are making a side-view game and are using image_yscale to mirror your gun sprite when the gun is facing toward the left, if you are using a different variable, just substitute that one for image_yscale. Though image_yscale is what people usually use in this case.

EDITED
Do you want me to add this coding on what hippyman said or is this by itself?
 
so it's top-down? Then I don't quite understand why your sprites need to flip at all. Maybe you mean something different by "4-directional". Maybe some drawings would help.

The code I posted doesn't build on hippyman's.
 
D

dawson2223

Guest
so it's top-down? Then I don't quite understand why your sprites need to flip at all. Maybe you mean something different by "4-directional". Maybe some drawings would help.

The code I posted doesn't build on hippyman's.
Ill record it for you to get you an idea!
 
D

dawson2223

Guest
so it's top-down? Then I don't quite understand why your sprites need to flip at all. Maybe you mean something different by "4-directional". Maybe some drawings would help.

The code I posted doesn't build on hippyman's.
Here is a video of what is happening with hippyman's code

I flip the gun sprite so it is always looking at the site object the right way. Do I not even need this code?
Code:
//Set Direction Towards Sight
image_angle = point_direction(x,y,obj_sight.x,obj_sight.y);
if (obj_sight.x < x) image_yscale = -1
else image_yscale = 1
 
P

Panlizak

Guest
I don't really know how your game looks like but from your post I really do think its because you are using different distance values for lengthdir_x and lengthdir_x.
 
D

dawson2223

Guest
I don't really know how your game looks like but from your post I really do think its because you are using different distance values for lengthdir_x and lengthdir_x.
I think that lengthdir_x and lengthdir_y can be different values.
 
It's kind of hard for me to see what's going on in that video. I can't really tell where the bullets are coming from relative to the gun. However, I think my code will work fine in your situation. Btw, my code should be located in the gun object, x and y should be the position of the gun, dx, and dy, should be the location of the barrel relative to the gun's origin, as measured when the gun is at zero rotation.
 
D

dawson2223

Guest
It's kind of hard for me to see what's going on in that video. I can't really tell where the bullets are coming from relative to the gun. However, I think my code will work fine in your situation. Btw, my code should be located in the gun object, x and y should be the position of the gun, dx, and dy, should be the location of the barrel relative to the gun's origin, as measured when the gun is at zero rotation.
The bullets are coming from the middle of the gun I think right now, haven't changed that yet. Yeah the code is in the gun object. Do I have to put numbers in for the dcos and dsin? Not sure what the d stands for. And what should my instance_create code be?
 
D

dawson2223

Guest
It's kind of hard for me to see what's going on in that video. I can't really tell where the bullets are coming from relative to the gun. However, I think my code will work fine in your situation. Btw, my code should be located in the gun object, x and y should be the position of the gun, dx, and dy, should be the location of the barrel relative to the gun's origin, as measured when the gun is at zero rotation.
Sorry if I'm asking easy questions.... I'm a noob :(
 
D

dawson2223

Guest
dcos and dsin are cosine and sine functions that take angle arguments in degrees, (rather than radians). The angle you put in there should be the angle that the gun is pointing.
Oh gotcha, thanks! What should my instance_create look like ???
 
you just need to define xd and yd which are just the number of pixels (along both axes) that seperate the barrel tip from the gun's origin. Count them in the sprite editor. By the way, if either xd and yd is zero, then that part of the computation can be left out.

For example if yd is zero then

this...

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

becomes this...

_x = x + _c * dx;
_y = y - _s * dx;
 
D

dawson2223

Guest
So could should be
Code:
dx = 18
dy = 9

var _c = dcos(image_angle);
var _s = dsin(image_angle);

_x = x + _c *dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;
instance_create(_x,_y,obj_testing);
depth = -y;
right?

I put it in and it kinda worked. Except the object came out of the bottom of the gun instead of the top of the gun.. but I can just change the dy value to fix that right?
 
D

dawson2223

Guest
you just need to define xd and yd which are just the number of pixels (along both axes) that seperate the barrel tip from the gun's origin. Count them in the sprite editor. By the way, if either xd and yd is zero, then that part of the computation can be left out.

For example if yd is zero then

this...

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

becomes this...

_x = x + _c * dx;
_y = y - _s * dx;
I'm sorry I'm an idiot I subtracted the dy value wrong the code worked thank you SO MUCH!!!! That saved a lot of time!
 
D

dawson2223

Guest
you just need to define xd and yd which are just the number of pixels (along both axes) that seperate the barrel tip from the gun's origin. Count them in the sprite editor. By the way, if either xd and yd is zero, then that part of the computation can be left out.

For example if yd is zero then

this...

_x = x + _c * dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

becomes this...

_x = x + _c * dx;
_y = y - _s * dx;

Quick Question!
My code when hitting the global left button is this (what you gave me)
Code:
 var dx = 16;
var dy = -2;

var _c = dcos(image_angle);
var _s = dsin(image_angle);

_x = x + _c *dx + _s * dy * image_yscale;
_y = y + _c * dy * image_yscale - _s * dx;

instance_create(_x,_y,obj_shotgunanimation);
depth = -y;
    }
How do I get it to follow the player when I'm moving?
i tried using with(obj_gunanimation)
x = _x;
y = _y;
In the end step but that didn't work.
 
Top