#Rotating coordinate by 90 degrees

10 messages · Page 1 of 1 (latest)

tame minnow
#

I'm trying to make a function rotate_coord() that takes an x pos, y pos, angle, x_size and y_size, and then rotates the coordinate around the middle by angle. See image for a better illistration.

I just don't know how to code this.

tame minnow
#

I tried to write a function (it doesn't work) but this is where I got to.

    
    var half_length = length/2
    var half_height = height/2

    x -= half_length
    y -= half_height
    
    var dist = Vector2(0, 0).distance_to(Vector2(x, y))
    var new_y = sin(angle)*dist
    var new_x = cos(angle)*dist
    
    return Vector2(new_x+half_length, new_y+half_height)```
fringe stirrup
#

What you need is this vector

#

Consider your image center is (4,4), in the first image the vector is (1,-3), the second image it is (3, 1), flipped the x-y and adjust the positive/negative accordingly

prisma drum
#

Something like this:

    var center = Vector2(width / 2, height / 2)
    var vec = Vector2(x, y) - center
    return center + vec.rotated(angle)```
#

If you want something general purpose that works with any angle

tame minnow
#

🤦‍♂️ I forgot about the rotated() function

tame minnow
#
return Vector2(round(ret.x), round(ret.y))```
Solved, it didn't work for a while, but after doing this it works perfectly.
#

Thank you Nisovin!

prisma drum
#

If you're always rotating 90 degrees, then you should just do what the person above said, and do the x/y swap