(Math) Placing sprites along a 2D geometric shape

Smacktard

Member
Specifically, I'm wondering how to place sprites along a 2D geometric shape so that they're equidistant from one another. Sounds fairly easy, right? But then again, I'm a mathlet.

I've used the following code:

for (var i = 0; i < 360; i += 40) // 40 = degrees between each one
Code:
{
    draw_sprite_ext(spr_minidiorama_vex,vexflash,display_get_gui_width()/3+display_get_gui_height()/2+cos(i)*600, display_get_gui_height()/2+sin(i)*600,2,2,1,c_white,1)
}
However, the objects are not equidistant, nor is the first one located in the top center, as I was hoping for. The spites' origins are mid center.
Also, I want the clock hands to be in the middle of the clock.

The following code achieves this:
GML:
draw_sprite_ext(spr_clockhand,0,(display_get_gui_width()/3+display_get_gui_height()/2-16)*cos(270),display_get_gui_height()/2,20,1,clockdegrees,c_white,1)
draw_sprite_ext(spr_clockhand,0,(display_get_gui_width()/3+display_get_gui_height()/2-16)*cos(270),display_get_gui_height()/2,10,1,clockdegreesH,c_white,1)
These sprites' origins are mid-right, so I've subtracted 16 pixels to account for that.

As you can see in the file below, the code doesn't quite work:

edit: (imagine that the clock hands ARE displayed correctly in this image, because I did get this part of the code working ... the only part that's not working is displaying that same 3x3 sprite of the dude running on the block with the spike character at the correct intervals.
 
Last edited:

gnysek

Member
Assign center of circle to temp variables (in my case cx and cy), then select length in which your sprites should be drawn, and use lengthdir functions:

GML:
var cx = display_get_gui_width()/3, cy = display_get_gui_height()/2-16; // center x and y points
var len = 100; // distance from center

for(var i=0; i<360; i+=45) { // i - angle
    draw_sprite_ext(spr_clockhand,0, cx + lengthdir_x(len, i), cy + lengthdir_y(len, i), 20,1,clockdegrees,c_white,1);
}
Very similar to your code, but maybe there was some issue with cos/sin results, hard to tell without testing it. Lengthdirs does all that logarithmic magic for you :)
 

Smacktard

Member
Assign center of circle to temp variables (in my case cx and cy), then select length in which your sprites should be drawn, and use lengthdir functions:

GML:
var cx = display_get_gui_width()/3, cy = display_get_gui_height()/2-16; // center x and y points
var len = 100; // distance from center

for(var i=0; i<360; i+=45) { // i - angle
    draw_sprite_ext(spr_clockhand,0, cx + lengthdir_x(len, i), cy + lengthdir_y(len, i), 20,1,clockdegrees,c_white,1);
}
Very similar to your code, but maybe there was some issue with cos/sin results, hard to tell without testing it. Lengthdirs does all that logarithmic magic for you :)
Just had to change the angle and it worked like a charm. Thanks a million!
 
Top