Rotations!

  • UchihaKite
    Likes 0

    Problem Description

    Good evening fellow programmers! I apologize for submitting yet another question so soon, but I’m always excited to learn new things!

    So, for an assignment at school, we were required to take a humanoid model, break it into parts, body, left arm, right arm, left leg and right leg. The goal was to toss those models into OpenGL and animate them. Don’t worry, its a newbie class :P by animate, I mean, for example, rotating the right arm model at the shoulder, stuff like that. I decided to use the code from the Modern OpenGL tutorials as a base since that is what I am most familiar with. After breaking the models into parts, I uploaded them into OpenGL and noticed they lined up perfectly. However, I noticed that when I use the glm::rotate() that the arm rotates in an extremely weird way. It’s almost like the origin is in front of the fingers or something. I’m not sure if this is an issue with the model or how it was made, or an issue with my code. I uploaded my main.cpp to dropbox if anyone was curious to see the code. Thanks again for all of your help!

    https://www.dropbox.com/s/sexxgnpka09kfuv/Main.cpp?dl=0

    P.S. Excuse the extremely messy code, I was planning on making it look pretty once I was done lol :D

  • Sonar Systems admin
    Likes 0

    Could you show us a video of what happens.

  • Sonar Systems admin
    Likes 0

    It’s rotating around the origin, you need to move the object to the origin and then rotate and then move it back

  • UchihaKite
    Likes 0

    I’m not entirely sure what you mean by that, is there any way you can give me an example or is there a way to move the origin? Sorry, >.< I’m still new to OpenGL and my teacher won't answer my emails, most likely because he is too busy getting ready for G.D.C.

    But I do apologize for all the questions! 

  • UchihaKite
    Likes 0

    glm::mat4 lmodel;
    
    	if (withBody == false)
    	{
    		lmodel = glm::translate(lmodel, glm::vec3(positionX, 0.15, positionZ));
    		lmodel = glm::scale(lmodel, glm::vec3(0.01f, 0.01f, 0.01f));
    		lmodel = glm::rotate(lmodel, larmRotation, glm::vec3(1.0f, 0.0f, 0.0f));
    		lmodel = glm::translate(lmodel, glm::vec3(positionX, 1.0f, positionZ));
    	}
    	if (withBody == true)
    	{
    		lmodel = glm::translate(lmodel, glm::vec3(positionX, -0.15f, positionZ));
    		lmodel = glm::scale(lmodel, glm::vec3(0.01f, 0.01f, 0.01f));
    		lmodel = glm::rotate(lmodel, larmRotation, glm::vec3(1.0f, 0.0f, 0.0f));
    		lmodel = glm::translate(lmodel, glm::vec3(positionX, 30.0f, positionZ));
    	}
    
    glUniformMatrix4fv(glGetUniformLocation(lArmShader.Program, "model"), 1, GL_FALSE, glm::value_ptr(lmodel));

    Hey! So I have an update. The code above is what I am using to do my rotations now. The bool “withBody” is set based on what keys are pressed to rotate a body part. For example, if the T or G key are pressed, the bool is set to true, because those keys control the rotation of the upper body. While if you press the W or S key, it’ll be set to false, because those keys control the rotation of just the arms.

    So I have it set up, to when the T or G key is pressed, it’ll rotate the body (of course) but also the arms.

    	if (keys[GLFW_KEY_T])
    	{
    		withBody = true;
    		bodyRotation -= deltaTime * -2.0f;
    		larmRotation -= deltaTime * -2.0f;
    		rarmRotation -= deltaTime * -2.0f;
    	}
    	if (keys[GLFW_KEY_G])
    	{
    		withBody = true;
    		bodyRotation -= deltaTime * 2.0f;
    		larmRotation -= deltaTime * 2.0f;
    		rarmRotation -= deltaTime * 2.0f;
    	}

    So. You might be asking yourself why I have the translations, scaling and rotations set up weird like that. Well, in the links you sent me, it seemed the solution was as easy as just doing a translation, a rotation and then another translation. However, when I tried doing it like that, the body parts would end up in random locations.

    So, as it is set up right now, everything is lined up perfectly, I can rotate the arms without an issue, I can rotate the body without an issue. It’s when I flip flop between the two, that’s when the issue comes up.

    www.dropbox.com/s/7kxf69qkeb3mga4/video.mp4?dl=0

    Personally, I think it’s because when I am rotating the body, the bodys origin is being used, so the arms follow along with the body while it rotates. However, when you switch back to just rotating the arms, the arms origin is still in its original location, so that’s why the arm jumps to that location. I feel like if I can get the coordinates of the shoulder after it rotates, I could just move the arm to that location? However, I feel like their has to be a better way of doing that. The arm should always be connected to the body, but I can’t figure out how to fix it @_@

  • Sonar Systems admin
    Likes 0

    Almost seems to be working they way it should be, just tweak the values.

  • UchihaKite
    Likes 0

    I spent a few hours tweaking the values. What I’ve found, is I either limit how much the objects can rotate to prevent the jumping around the arms do, or I need to hit google. I don’t know if I set up the models and their pivot points incorrectly or if I am just using the matrixes incorrectly, but I can’t figure out what the correct values are.

  • Sonar Systems admin
    Likes 0

    Try a simpler model and see if that works, then apply those principles to the more complex model.

Login to reply