fonta.romain Posted August 27, 2012 Posted August 27, 2012 Hello ! I'm having some trouble using WorldTransform class for a rotation only movement. My code is : /** * Rotates an object around given axis in the given time * @param[in] o Object to translate * @param[in] axis Rotation axis * @param[in] angle Rotation angle * @param[in] time Time to travel */ void RotateObject(Object o, vec3 axis, float angle, float time) { // Valid time and object ? if (o != NULL && time > 0.0f && length(axis) > 0.0f) { // Declare a world transform WorldTransform wtr = node_remove(new WorldTransform()) ; // Build a frame in the transform for start point int frameId = wtr.addFrame() ; wtr.setFrameTransform(frameId, o.getTransform()) ; wtr.setFrameTime(frameId, 0) ; // Build a new frame for end point frameId = wtr.addFrame() ; wtr.setFrameTransform(frameId, o.getTransform() * rotate(axis, angle)) ; wtr.setFrameTime(frameId, time) ; // Add child to this tranform wtr.addChild(o) ; // And then play ! wtr.play() ; } } The rotation appears to be done from (0;0;0) coordinates instead of the center of my object. However, this code : o.setTransform(o.getTransform() * rotate(axis, angle)) produce the correct behavior (rotate around the object's center not depending on its space position). Could you please explain what I'm doing wrong ? Thanks !
unclebob Posted August 29, 2012 Posted August 29, 2012 Hi, That is probably because child transformation. Lets put your object transform as T_obj and WorldTransform as T_wt. When you add your object as WorldTransform child it will have T_wt * T_obj world transform. T_wt in your sample is equal to T_obj * rotate(axis,angle), so child world transform will be T_obj * rotate(axis,angle) * T_obj and that is not what you want. Try to set child transformation to identity matrix. Here I modified your code sample: /** * Rotates an object around given axis in the given time * @param[in] o Object to translate * @param[in] axis Rotation axis * @param[in] angle Rotation angle * @param[in] time Time to travel */ void RotateObject(Object o, vec3 axis, float angle, float time) { // Valid time and object ? if (o != NULL && time > 0.0f && length(axis) > 0.0f) { // Declare a world transform WorldTransform wtr = node_remove(new WorldTransform()) ; // Build a frame in the transform for start point int frameId = wtr.addFrame() ; wtr.setFrameTransform(frameId, o.getTransform()) ; wtr.setFrameTime(frameId, 0) ; // Build a new frame for end point frameId = wtr.addFrame() ; wtr.setFrameTransform(frameId, o.getTransform() * rotate(axis, angle)) ; wtr.setFrameTime(frameId, time) ; // Add child to this tranform wtr.addChild(o) ; o.setTransform(mat4_identity); // NEW! // And then play ! wtr.play() ; } } Hope this was not complicated. :)
fonta.romain Posted August 29, 2012 Author Posted August 29, 2012 Hello, I understand perfectly :) In fact, it's my models that are not correctly built under 3DS Max and lead to this incorrect behavior to my eyes. Thanks for your time !
Recommended Posts