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

SOLVED Making a grid with sprites and rotate them based on an anchor point

mbeytekin

Member
I didn't think this is difficult but I'm stuck and need an advice.
My goal is making a grid with sprites based on given parameters like horizontal and vertical sprite numbers. This is easy. But I want rotate them all like group from a given origin point with a given angle. All parameters will be change everytime.
Another problem is sprites' scales can be change by given two parameters (startscale,endscale). Start scale is near to rotation origin and end scale is far one.
Basically I have this parameters rows,columns,distanceX,distanceY,originpointX,originpointY,StartScale,EndScale
Fig1.pngFig2.pngFig3.png
As you can see figure one is a grid with 5 columns and 4 rows. Total width is distanceX * columns and height is distY*rows.
Figure2 shows that scale's of sprites from startscale to endscale based on distance to rotation origin.
Figure3 shows rotation of grid.

Can anybody give an idea? Because I tried something but when rotating them I can't get a good result.
 

Binsk

Member
What have you tried? What worked and what didn't?

Do you remember learning about different coordinate systems in school? We have Cartesian (aka, [x, y]) which defines a location with a value along the x axis and a value along the y axis. We also have Polar (aka, [theta, length]) which defines a location with a value theta degrees/radians from the right and length units from the center. If you convert all your sprite's coordinates to Polar shouldn't it be easy to rotate them by simply modifying the theta value? At the same time, you can only render things in Cartesian so you will need to effectively do the following:
  • Convert [x, y] -> [theta, length]
  • Rotate in Polar as needed where it is easy to do so
  • Convert [theta, length] -> [x, y]
  • Render in Cartesian as needed where it is easy to do so
So if you have your origin location, you want to find each sprite's Cartesian location relative to your origin point before converting to Polar so that, once in polar, that origin point will effectively be point [0, 0].

Some example code.
Code:
// First, convert relative to our rotation origin:
// Let x/y_origin be the rotation origin
// Let x/y/_current be the current global coordinates of the sprite in Cartesian
var x_relative = x_origin - x_current;
var y_relative = y_origin - y_current;

// Convert the values into polar coordinates:
var theta = point_direction(0, 0, x_relative, y_relative);
var length = point_distance(0, 0, x_relative, y_relative);

// Rotate however much we need in polar coordinates:
theta += 5; // 5 degrees

// Convert back to Cartesian coordinates:
x_relative = lengthdir_x(length, theta);
y_relative = lengthdir_y(length, theta);

// Convert back to global origin:
x_current = x_origin + x_relative;
y_current = y_origin + y_relative;
Rotate the sprite to match and it should translate just fine given your sprite origin is in the center.
 

mbeytekin

Member
Thank you for your kind explanation. You are right. I tried giving coordinates and rotation same time. Now first I create a ds_grid for initial positions.After that I calculate rotation and scales with Cartesian coordinate system like you advice. I think this solved my problem for now. Thank you again
 
Top