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

Legacy GM Anchor an Object To the End of Another And Rotate

K

Konzash

Guest
So, I have a rotating arm that is a seperate object from the main player sprite. The arm rotates based on the mouse cooridnates and flips the player sprite and arm if it goes behind the current facing direction.

So now, I want to place a weapon object in the hand at the end of the arm, which has it's pivot point at the shoulder, at the opposite end. I also need to rotate the gun based on the direction the arm is pointing, wether it be at the ceiling, straight ahead, or down.

As of right now I can have it the player arm but is sits in place based on the pivot point at the shoulder.

Here is what code I have for the gun following the characters arm object:


if (PlayerOwned=true)
{
if (oPlayer.image_xscale >=0)
{
image_xscale=1;
x=oPlayer_default_arm.x+64;
y=oPlayer_default_arm.y;
} else {
if (oPlayer.image_xscale <=0)
{
image_xscale=-1;
x=oPlayer_default_arm.x-64;
y=oPlayer_default_arm.y-6;
}
}
}
 
You're going to want to looking using the 'lengthdir_x' and 'lengthdir_y' functions. Roughly as follows:

The below assumes the 'arm' is default facing to the right (or 0 for image_angle), and that it's 16 pixels long. All code below would be in the Arm object.

Create Event:
Code:
gun_offset = 16;
gun_x = 0;
gun_y = 0;
Step Event:
Code:
// Change image angle here, however you need/want to do it

gun_x = x + lengthdir_x(gun_offset, image_angle);
gun_y = y + lengthdir_y(gun_offset, image_angle);
Draw Event:
Code:
draw_self();

draw_sprite_ext(spr_gun_sprite, gun_sub_image, gun_x, gun_y, 1, 1, image_angle, c_white, 1);
This currently doesn't handle flipping sprites with image_xscale, but it's a start.
 
B

bojack29

Guest
dcos and dsin work much the same way

if you need the x value in a 45 degree angle at a length of 10 you can do:
Code:
var xx = x + dcos(45) * 10;
Or y value of something at a length of 15 and at an angle of 225
Code:
var yy = y + -dsin(225) * 15;
 
Top