How to calculate a rectangle corners position depending on her rotation



  • Hi,

    I'm searching how to calculate a rectangle corners position depending on the rectangle rotation.
    below a simple example to illustrate what I want to do:

    # Rectangle size
    width = 200
    height = 100
    
    # Rectangle Coordinate
    rec_pos = Vector(0,0,0)
    rec_rot = Vector(0,0,0)
    
    # Calculate corners position when rectangle rotation = Vector(0,0,0)
    x_pos = width/2
    y_pos = height/2
    
    top_left_corner = Vector(rec_pos.x - x_pos, rec_pos.y + y_pos, rec_pos.z)
    top_right_corner = Vector(rec_pos.x + x_pos, rec_pos.y + y_pos, rec_pos.z)
    bottom_left_corner = Vector(rec_pos.x - x_pos, rec_pos.y - y_pos, rec_pos.z)
    bottom_right_corner = Vector(rec_pos.x + x_pos, rec_pos.y - y_pos, rec_pos.z)
    
    # Calculate corners position when rectangle rotation != Vector(0,0,0)
    # example
    rec_pos = Vector(0,0,0)
    rec_rot = Vector(90, 30, 0)
    
    top_left_corner =  ?
    top_right_corner =  ?
    bottom_left_corner = ?
    bottom_right_corner =  ?
    
    

    Thanks



  • Hi,

    you will need a rotation matrix for that. There is c4d.utils.HPBToMatrix() which lets you construct a rotation matrix from a rotation vector, the input however is expected in radians and not in degrees. The rotated point is then p_rot = HPBToMatrix(angles) * p.

    Cheers
    zipit



  • Hi,

    you will need a rotation matrix for that. There is c4d.utils.HPBToMatrix() which lets you construct a rotation matrix from a rotation vector, the input however is expected in radians and not in degrees. The rotated point is then p_rot = HPBToMatrix(angles) * p.

    Cheers
    zipit



  • Hi @mfersaoui thanks for reaching out us and thanks @zipit for the given answer.

    On top of zipit's answer I recommend to check that the values first are set in radians and second that the proper rotation order set is selected otherwise you can easily end up with unexpected results.

    Cheers, R



  • @zipit
    Hi, Thanks @zipit and @r_gigante for your answers.

    I tried with HPBToMatrix but that works only if rectangle has as position Vector(0,0,0), but when the rectangle position change this doesn't work. Or I misused the HPBToMatrix function.

    Here is .c4d scene example: HPBToMatrix.c4d
    in my example the rectangle is replaced by a light object



  • Hi,

    you will need to set the offset of the matrix returned by the function to the offset of your rectangle or translate your points manually into the desired frame of reference.

    Cheers,
    zipit



  • @zipit
    Hi, Thank you so much.


Log in to reply