#Trigonometry math in replicating draw_self() with draw_sprite_general()

6 messages · Page 1 of 1 (latest)

shrewd obsidian
#

Anyone in mood for some trigonometry? I am trying to perfectly replicate draw_self() using draw_sprite_general(), to supply it with part cutout, coloring, modifiable origin point and so on. I achieve it with the following code:

var _origin_transformed_x = (_part_x1 - lerp(_part_x1, (_part_x1 + _part_x2),
                                             ((_origin_x * _scale_x) /
                                              _size_x)));
var _origin_transformed_y = (_part_y1 - lerp(_part_y1, (_part_y1 + _part_y2),
                                             ((_origin_y * _scale_y) /
                                              _size_y)));

_location_x = (_location_x + (_origin_transformed_x * _angle_dcos) +
               (_origin_transformed_y * _angle_dsin));
_location_y = (_location_y - (_origin_transformed_x * _angle_dsin) +
               (_origin_transformed_y * _angle_dcos));

draw_sprite_general(_sprite, _frame, _part_x1, _part_y1, _part_x2, _part_y2,
                    _location_x, _location_y, _scale_x, _scale_y,
                    _angle_value, _color_x1y1, _color_x2y1, _color_x2y2,
                    _color_x1y2, _alpha);

This code works, but I have an edge case I cannot get to fix: It produces an unwanted offset, provided three conditions are met: First is that I have to scale the sprite with scale that is not even in both dimensions (for example: X: 2, Y: 1). Second is that "Automatically Crop" Texture Option must have cropped something from top and/or left of the sprite. Third is that the sprite is being rotated. It results in an offset like this:

#

Where:
Green rectangle is the wanted draw_self().
White rectangle is the above code.
Purple circle is the X/Y location this is drawn at (with origin being set at middle center, unmodified further).

#

How can I remove this offset? My most logical assumption was that while I calculate _origin_transformed, I needed to modify _origin_ variables by of cropped pixels from sprite_get_uv(), but that apparently does not work.

acoustic tinsel
#

#help_1 message

no trig needed, this is point_in_rotated_rectangle, but the bulk of the logic is generating the four corners

shrewd obsidian
#

I know that in the source code, YoYo just does matrix transformations, but I would like to try to just calculate it directly, since I am so close to having getting it right with above setup.