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

Rotate objects around 1 point

E

eldinhelja

Guest
Hello, I've had a long day. My brain is not working.

I have created this :


I have 8 objects called P1, P2, P3...
I Want to rotate every point at once for some angle "degRotate"

Code:
//Variables
degRotate=240

//Find Center
CenterX = (x1+x2)/2
CenterY = (y1+y2)/2

//Get Radius
radius = point_distance(x1, y1, CenterX, CenterY)

//Create points after rotation
p[0] = instance_create(....) //p[0] is P1 (Point 1)
So eg. if I rotate it by 30 deg. I need to get this :


I am really tired.

EDIT :

Forgot to mention : p[0] = instance_create(x1,y1,x2,y2) //Should look like thise

Just need a formula, not code :p
 
Last edited by a moderator:

Nux

GameMaker Staff
GameMaker Dev.
You're going to have to get the vector of each object from the main object's origin, the longitudinal and lateral components.

From there you can use trigonometric functions to calculate where the objects will be when turned θ degrees about a central point (the centre of your main object).
Alternatively, you are able to use length_dir_*.

I might dump some code i've done with this before to help better explain.

EDIT: Alrighty! General formulae using trigonometric functions:
Code:
    x = xCentre + xComponent*cos(θ) + yComponent*sin(θ)
    y = yCentre + xComponent*cos(θ+90) + yComponent*sin(θ+90)
EDIT#2: I will add: xComponent and yComponent are not coordinates within the room; they are Δx and Δy when θ = 0 degrees (has no rotation).

If each point has an equal spacing of 20 in the x direction, and 40 in the y direction, the x and y component of the 'p1' will be:
Code:
    xComponent = xCentre - (20*3)*0.5  // this is because there are 3 separations between the points on the x-axis
    yComponent = yCentre - (40*1)*0.5  // *1 because there is only 1 separation between two points along the y-axis
I'm sure you can extrapolate this or find exact relative points for each point of yours with a bit of tweaking.
 
Last edited:
E

eldinhelja

Guest
@Nux thank you ! I will try it tomorrow.

When theta is 0 degrees I know coordinates of all points, but I dont understand what are x and y components.
 

Nux

GameMaker Staff
GameMaker Dev.
coordinates of all points, but I dont understand what are x and y components.
Ohh right! That's great; it simplifies things a lot more.
the components is the x and y distance from the centre object

upload_2016-11-29_23-47-25.png

So to get each component, just take away a point's xy coordinates from the centre object's xy coordinates, and store the x and y values seperately.
 
E

eldinhelja

Guest
I got it working using this code :
Code:
repeat(7) {
            radius=abs(point_distance(pSec[i].x, pSec[i].y, centerX, centerY)) //Radius Between the current point and the center
            newAngle=(deg*30)+point_direction(pSec[i].x, pSec[i].y, centerX, centerY);
                      //show_message("Radius between " + string(pSec[i].x) + ", " + string(pSec[i].y) + " and " + string(centerX) + ", " + string(centerY) + " is = " + string(radius));
            pSec[i].x = centerX+cos((pi/180)*newAngle)*radius;
            pSec[i].y = centerY+sin((pi/180)*newAngle)*radius;
            i++;
        }
This is my idea :


And I turn them one by one by some angle Theta.

@Nux Thanks for support!
 
Top