PDA

View Full Version : Rotating an Object on Key Press and following camera



kumakun
08-03-2016, 11:16 PM
hey guys, i haven't idea how to coding rotating an object on key press and when object rotating camera follow an object rotating...
please help me, ..
thanks

john_connor
08-04-2016, 04:01 AM
1. how to coding rotating an object on key press
2. and when object rotating camera follow an object rotating

1. you could implement a "input" class which helps you to detect all kinds of input in everywhere in your code
or if you are doing it very simple, declare your object globally
the point is: you need to have access to that object as well as the type of input



struct Object {
glm::vec3 position{0, 0, 0};
glm::quat rotation{1, 0, 0, 0};
glm::vec3 scale{1, 1, 1};
} myobject; // globally declared

// callback function (GLFW library)
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_E && action == GLFW_PRESS)
{
// rotate object if "E" is pressed:
float angle = glm::radians(2.0f);// 2 degree per frame
glm::vec3 axis(0, 1, 0);
myobject.rotation = glm::rotate(myobject.rotation, angle, axis);
}
}


to build the model matrix:


glm::mat4 modelmatrix = glm::translate(myobject.position) * glm::toMat4(myobject.rotation) * glm::scale(myobject.scale);


2. if you want to "lock" a certain cameras rotation to an objects rotation, you could use references
otherwise you can just use spherical coordinates to describe your cameras position, using the target as center of te coordinate system