# AttachPoint | Animate 2D Points

Create attach points on your sprites for sticking objects! 路 By Gurpreet Singh Matharoo

# Code for image_angle/x/yscale?

A topic by lorewap3 created Feb 22, 2020 Views: 346 Replies: 3
Viewing posts 1 to 4

Hey Matharoo,

I'm really enjoying the tool, its immensely helpful! I was hoping you could help me out with a little implementation, though.

The attach points work fine with a character with image_x/yscale = 1 and image_angle = 0, but in betweens I'm having trouble figuring out the correct algorithm.

I got the 4 combinations of image_xscale -1 / 1 and image_yscale -1 /1 to work right using 4 if statements, but adding image_angle to the mix has been difficult. I'm thinking I need to remove image_yscale from the mix and just use image_xscale and image_angle. I would appreciate any thoughts you might have on the subject. Or perhaps you wouldn't mind sharing the code that you use?

Here's a sample of the code I'm using. The attached point is a particle emitter, so emitterX/Y/Direction are the values I use after recalculating from the original attach points.

var _curr_attach_point = _attach_points[_curr_index];

var _offset_x = _curr_attach_point[0];
var _offset_y = _curr_attach_point[1];

var _length = sqrt(sqr(_offset_x) + sqr(_offset_y));

// Normal direction
if (image_xscale > 0 && image_yscale > 0) {
emitterDirection =  _curr_attach_point[2];

// Flipped horizontally
} else if (image_xscale < 0 && image_yscale > 0) {
emitterDirection = 180 - _curr_attach_point[2];

// Flipped vertically
} else if (image_xscale > 0 && image_yscale < 0) {
emitterDirection = 180 - _curr_attach_point[2];

// Flipped horizontally and vertically
} else if (image_xscale < 0 && image_yscale < 0) {
emitterDirection = 180 + _curr_attach_point[2];
}

emitterDirection += image_angle;

Will

Developer

Hey Will,

Could you email me your project, to gurpreetsingh793@gmail.com? It would be quicker if I could fix the code in the project itself and then explain what I did.

Cheers

Hey Matharoo,

Sorry I didn't respond until now. It was going to be difficult to send you the project as it currently is, I load alot of json files from the filesystem and the code that calculates the attach_point is buried below a good number of inheritance that would've taken just as long to explain hah. But I figured it out. I just had to think it through procedurally. I have sprites that I flip the image_xscale , image_yscale, and image_angle after that so I just needed to process it in order.

Here's the code in case you are interested!

In essence I get the current image_index, then calculate what the attach_point would be considering ONLY image_xscale and image_yscale, which isn't too bad. Then after that's all done I calculate the image_angle. What was throwing me off is that once you flip the image on the x or y axis, it also flips the attach_point direction the opposite way. So the code I had was already correct, it just wasn't the result I expected.

Thank you for replying so quickly though, I appreciate your willingness to help! I appreciate your videos!

var _curr_index = floor(image_index);

if (_curr_index >= array_length_1d(_attach_points)) {
_curr_index = array_length_1d(_attach_points) - 1;
} else if (_curr_index < 0) {
_curr_index = 0;
}

var _curr_attach_point = _attach_points[_curr_index];

_offset_x = _curr_attach_point[0];
_offset_y = _curr_attach_point[1];
_attach_point_direction = _curr_attach_point[2];

// **************************************
// **************************************
#region Calculates x/y coordinate and direction of attach point assuming image_angle = 0
// Normal direction
if (image_xscale > 0 && image_yscale > 0) {

// Flipped horizontally
} else if (image_xscale < 0 && image_yscale > 0) {

} else if (image_xscale > 0 && image_yscale < 0) {

} else if (image_xscale < 0 && image_yscale < 0) {
}

#endregion
// **************************************
// **************************************

// **************************************
// **************************************
#region Calculate x/y/direction taking image_angle into account
if (image_angle != 0) {
// Gets angle of the straight line drawn from center of player object to attach point

// Once starting direction is determined, calculate adjusted based on image_angle

// Get length of line from center of player object to attach point

// Gets new x/y coordinates of new attach_point angle when you add in image_angle

// Finally, add direction of actual attach point to new calculated direction
} else {
_attach_point_direction_from_center = "N/A";
_length = "N/A";

}
#endregion
// **************************************
// **************************************

Developer

Awesome that you got it to work! 馃憤