0
\$\begingroup\$

I have created a simple robot in webgl with cubes and now i want to rotate one "leg" around a point. But the leg is rotating around its origin. I want to rotate it around the point that connects with the body. I dont understand how to do it. How i am supposed to use translations and rotations outside of a push/pop when these translations will change the center of the world for the cubes i want to draw later on?? A little help would be appreciated. Thanks.

\$\endgroup\$
  • \$\begingroup\$ This is a common task in manipulating 3D scenes that has been discussed several times. It may be helpful to review existing answers to see if they can get you on the right track. \$\endgroup\$ – DMGregory Mar 26 '17 at 15:42
  • \$\begingroup\$ I checked them but still im confused. Do you have any experience about this? Thanks in advance. \$\endgroup\$ – Chris Mar 26 '17 at 21:22
  • \$\begingroup\$ The abstract is that you apply a translation transformation to the center of rotation, then the rotation transformation and then the inverse of the translation transformation you did before. Also, you want to rotate the object first, then place it on the world. And finally if you express the transformations as matrices, you can combine them by matrix multiplication (that way you don't need push and pop). Btw, if want to direct a message to somebody use "@" and the name, that way that person gets a notification. \$\endgroup\$ – Theraot Mar 26 '17 at 22:44
2
\$\begingroup\$

One way is using a matrIx stack

https://webglfundamentals.org/webgl/lessons/webgl-2d-matrix-stack.html

Basically you need to translate the object so it's center point is at the origin, rotate, then translate it back

\$\endgroup\$
0
\$\begingroup\$
push();
rotateY(frameCount*0.01);
translate(-100, 0, 0);
sphere(10);
pop();

This is literally your entire code, this is literally ALL you need, that link in the other answer is almost entirely useless. This code will do the job.

\$\endgroup\$
  • 1
    \$\begingroup\$ This answer could benefit from an explanation of what the code you posted does. \$\endgroup\$ – Tyyppi_77 Jan 5 '18 at 10:21

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.