0

I have a 3D shape defined in STL file and I'd like to rotate it around its axis/center using Yaw, Pitch and Roll. However, I've already implemented a solution and it doesn't work as expected as it rotates the 3D shape around the origin axis instead. Here's what I've done so far (written in PHP):

function rotate($vertices, $roll = 0, $pitch = 0, $yaw = 0) 
{
    if(!empty($vertices))
    {
        $cosa = cos($yaw);
        $sina = sin($yaw);

        $cosb = cos($pitch);
        $sinb = sin($pitch);

        $cosc = cos($roll);
        $sinc = sin($roll);

        $Axx = $cosa * $cosb;
        $Axy = $cosa * $sinb * $sinc - $sina * $cosc;
        $Axz = $cosa * $sinb * $cosc + $sina * $sinc;

        $Ayx = $sina * $cosb;
        $Ayy = $sina * $sinb * $sinc + $cosa * $cosc;
        $Ayz = $sina * $sinb * $cosc - $cosa * $sinc;

        $Azx = -$sinb;
        $Azy = $cosb * $sinc;
        $Azz = $cosb * $cosc;

        //loop through all triangles
        foreach($vertices as $i => $vertex) 
        {
            $px = $vertex->x;
            $py = $vertex->y;
            $pz = $vertex->z;

            $points = 
            [
                'x' => ($Axx * $px + $Axy * $py + $Axz * $pz), 
                'y' => ($Ayx * $px + $Ayy * $py + $Ayz * $pz), 
                'z' => ($Azx * $px + $Azy * $py + $Azz * $pz)
            ];

            //update the vertex
            $vertices[$i]->setVertex($vi, $points);
        }
    }

    return $vertices;
}

Please let me know if I'm missing something. any help would be appreciated.

6
  • The code above rotates about the origin. If your centre of rotation (say, $c), isn't the origin, you can move the object before rotating: $px = $vertex->x - $c->x and so on. After rotating, move it back: 'x' => ($Axx * $px + $Axy * $py + $Axz * $pz) + $c->x, ...
    – M Oehm
    Dec 9, 2019 at 7:34
  • @MOehm - so in other words the rotation must be executed on the origin? and there's no other way to do that?
    – Desolator
    Dec 9, 2019 at 7:41
  • What's wrong with that? You can, of course, create a 4×4 transformation matrix that describes both rotation and transformation, but it's essentially the same under the hood.
    – M Oehm
    Dec 9, 2019 at 7:51
  • @MOehm - nothing is wrong with it. I was just wondering. but that works too. Thanks for your help. please add that comment as an answer so I can accept it.
    – Desolator
    Dec 9, 2019 at 7:57
  • @MOehm There's a small issue, so when I add the rotation for example for yaw = 1, is that in degrees or radians? because it applies a big rotating of the object
    – Desolator
    Dec 9, 2019 at 8:01

1 Answer 1

2

The code above rotates about the origin. If your centre of rotation (say, $c), isn't the origin, you can move the object before rotating:

$px = $vertex->x - $c->x;
$py = $vertex->y - $c->y;
$pz = $vertex->z - $c->z;

After rotating, move the point back to the centre of rotation:

$points = 
[
    'x' => ($Axx * $px + $Axy * $py + $Axz * $pz) + $c->x, 
    'y' => ($Ayx * $px + $Ayy * $py + $Ayz * $pz) + $c->y, 
    'z' => ($Azx * $px + $Azy * $py + $Azz * $pz) + $c->z
];
1
  • perfect! thanks for your help. I just converted the radians to degrees and now it's rotating perfectly.
    – Desolator
    Dec 9, 2019 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.