[SOLVED] Need help with rotations

width = 700
height = 500
recto = PVector(0,0)
mouse = PVector(0,0)
def setup():
    size(width,height)
    rectMode(CENTER)
    
def draw():
    background(191)
    mouse.x = mouseX
    mouse.y = mouseY
    pushMatrix()
    angle = PVector.angleBetween(recto,mouse)
    translate(width/2,height/2)
    rotate(angle)
    print(degrees(angle))
    rect(recto.x,recto.y,100,100)
    popMatrix()
    ellipse(width/2,height/2,50,50)

I need to rotate the rect() when i move the mouse, but I want that the angle is from rect center, and not X axis like in atan2()

1 Like

I think what you should keep in mind is that rotate function rotates around the point you set in translate function. So, with translate(width/2,height/2) is rotates around the (width/2,height/2) point.

What I suggest is to translate to the point that should be rect's center, so that it would rotate around that point, and this way would do the thing you want. So, replace corresponding lines with this:

    translate(width/2+recto.x+50,height/2+rectoy+50) #50 is to set to rect's middle point, not top left corner
    ...
    rect(-50,-50,100,100) #-50 is because rect draws from top left, and we need it's center to align with the axis point

I think this should work like you want.

1 Like

Hi @Sky,

Not sure to perfectly understand what you want (it seems to me you can operate a rotation from the center of a rectangle with atan2()). Does the following correspond to what you’re trying to achieve ?

def setup():
    size(700, 500)
    rectMode(CENTER)
    
def draw():
    background(191)
    
    angle = atan2(mouseY-height/2, mouseX-width/2)

    translate(width>>1, height>>1)    
    rotate(angle)

    rect(0, 0, 100, 100)
    ellipse(0, 0, 50, 50)
    line(0, 0, 25, 0)
    
    print degrees(angle)
3 Likes