GameMaker How to keep x and y instance create consisted with image angle

M

mastmartelli

Guest
I'm trying to have a gun shoot bullets, by using instance_create_layer(x+bullet_x,y+bullet_y,"guns",oBullet) in the gun's step event, the bullet_y for this gun is set to 0 and the bullet x is set to the tip of the gun. It works fine, but since the gun can be rotated with image_angle, the more it rotates up or down the bullet starts to be created farther from the gun, anyone have any ideas on how to fix this?
 

NightFrost

Member
Use lengthdir_x and lengthdir_y, where length is the distance from gun origin to muzzle, and direction is the angle. The results will be the bullet_x and bullet_y for your create command.
 
M

mastmartelli

Guest
Use lengthdir_x and lengthdir_y, where length is the distance from gun origin to muzzle, and direction is the angle. The results will be the bullet_x and bullet_y for your create command.
I don't think that's what I need, can you give me an example on how to execute it? I tried this:

if (shoot) and (delay_time == 0) and (reload_time == reload) and (ammo > 0)
{
var x_test = lengthdir_x(bullet_x, image_angle);
var bul = instance_create_layer(x+x_test, y+bullet_y, "gun", bullet);
with (bul)
{
direction = other.image_angle + random_range(other.shot_offset, -other.shot_offset);
image_angle = direction;
speed = other.spd;
alarm[0] = other.range;
}
ammo--;
delay_time = delay;
recoil_time = recoil;
if (ammo == 0) reload_time = 0;
scr_camera_screenshake(oCamera_2d, 1, 0.1);
}

bullet_y doesn't matter right now because for this gun it's set to 0
 
L

LV154

Guest
Instead of var x_test = lengthdir_x(bullet_x, image_angle), try var x_test = x+lengthdir_x(bullet_x, image_angle);

The difference here is x+lengthdir_x as opposed to just lengthdir_x
 
M

mastmartelli

Guest
Instead of var x_test = lengthdir_x(bullet_x, image_angle), try var x_test = x+lengthdir_x(bullet_x, image_angle);

The difference here is x+lengthdir_x as opposed to just lengthdir_x
That didn't work I don't even see the bullet when I shoot now, I think it's because I already have x+x_test in the instance create for bullet
 
M

mastmartelli

Guest
That didn't work I don't even see the bullet when I shoot now, I think it's because I already have x+x_test in the instance create for bullet
I tried remove the x+ in the instance_create and now it's doing the same thing as it did before :/
 
C

CROmartin

Guest
Wow. I have no idea what dcos and dsin do even after reading the gamemaker studio help page, but it works perfectly, thank you!
I guess you know what cos and sin does (if not, refresh you knowledge, it wont take you to much), so basically difference between dcos and dsin and cos and sin is that dcos will give you right value if you put degrees in brackets (like dcos(125)) but for cos to give you right value you must put radians in bracket, one radian is about 57,2958 degrees so formula for getting right value should go like this cos(degree / 57,2958), same thing for dsin and sin. Well this is ofc not necessary when we have dcos and dsin, but this might help you understand it more. I am glad to help.
 
Last edited:

NightFrost

Member
The lengthdir commands are wrappers for the trigonometry to simplify thinking angular movement. They do the same as using sines & cosines except you don't need to understand Pythagoras.
 
M

mastmartelli

Guest
The lengthdir commands are wrappers for the trigonometry to simplify thinking angular movement. They do the same as using sines & cosines except you don't need to understand Pythagoras.
Weird they didn't work when I tried it
 
It needs to be x+lengthdir_x(len,dir) and y+lengthdir_y(len,dir), simply using ONLY the lengthdir_x will not set the vector appropriately. Basically, take a measurement of the length in pixels from the sprite origin to the barrel tip in CROmartin's picture and take a measurement of the angle (with 0 degrees being directly right) and plug those values directly into BOTH the lengthdirs without trying to adjust for the x and y separately. For CROmartin's picture, the exact thing you would want is:
Code:
bullet_x = player_x+lengthdir_x(115,player_angle+342); // Alternatively you could use player_angle-18 for the dir section on both
bullet_y = player_y+lengthdir_y(115,player_angle+342);
 
upload_2019-3-18_11-55-57.png

There are two vectors. [cos(angle), -sin(angle)], points in the direction of the parent's x axis. [sin(angle), cos(angle)] points in the direction of the parent's y axis. Both vectors have a length of 1, and they are perpendicular to each other.

The x_axis vector is scaled by the child's x_offset, and the y_axis vector is scaled by the child's y_offset. The results are added together, along with the parent's position, and the result is the child's position.

dcos, and dsin functions take the angle argument in degrees rather than radians.
 
Top