0

I have an object with its own inventory, for which I use a list of maps. Each item has a name and x and y offsets.

I want to draw each item with its offset on top of my other object, and have them turn along with the original object, so inside the draw function I have this:

draw_sprite_ext(spr_cart2, 0, x, y,1,1,direction, c_white, 1)
for(i = 0; i < ds_list_size(products); i++){
    product = products[| i]
    rad_dir = degtorad(direction)
    x_rot = cos(rad_dir)*(product[? "x"]-x) - sin(rad_dir)*(product[? "y"]-y)
    y_rot = cos(rad_dir)*(product[? "y"]-y) - sin(rad_dir)*(product[? "x"]-x)
    draw_sprite_ext(asset_get_index("spr_"+product[? "product"]), 0, x_rot, y_rot, 1, 1, direction, c_white, 1)
}

However, it doesn't work at all. I tried logging product[? "x"]-x, and it gave a very large negative number, whereas logging just product[? "x"] did give the expected result.

b9s
  • 517
  • 4
  • 13
  • always when I see `sin`+`cos` I think that someone does not know about `lengthdir_...` fuinctions. – Dmi7ry Mar 17 '18 at 04:34
  • Didn't know about the `lengthdir_` functions, however, those only work when the `x` and `y` offsets are the same. They also don't seem to behave well with map values. – b9s Mar 17 '18 at 13:29
  • Yes, because they _should_ be same (read about polar coordinate system). map - there is no difference with any other variables/etc. – Dmi7ry Mar 17 '18 at 19:05

1 Answers1

0

What worked in the end was not directly subtracting from the map There were also some errors in the rotational logic.
Working code:

draw_sprite_ext(spr_cart2, 0, x, y,1,1,direction, c_white, 1)
for(i = 0; i < ds_list_size(products); i++){
    product = products[| i]
    x_rot = (cos(rad_dir)*(product[? "x"])) - (sin(rad_dir)*(product[? "y"]))
    y_rot = (cos(rad_dir)*(product[? "y"])) + (sin(rad_dir)*(product[? "x"]))
    draw_sprite_ext(asset_get_index("spr_"+product[? "product"]), 0, x-x_rot, y+y_rot, 0.5, 0.5, direction, c_white, 1)
}
b9s
  • 517
  • 4
  • 13