# Moving and rotating video file

Simple coding question: I am looking to move a video file across the screen and then rotate it around its centre by 90degrees . I have done the movement by updating the Loc X and LocY, but I am having trouble rotating the video file around its own centre once it has reached its new position. I am using ofVideoPlayer and ofSetRectMode(OF_SET_RECT_MODE_CENTER), but I cannot get the origin of the rotation to be at the centre of the video. I need it to be in two steps: move and then rotate., so i am trying to use the ofGetEllapsedTimeMillisCommand().

much appreciated,

Hi,
look at ofRotate() and ofTranslate() to do this

There is a tricky thing about rotation and traslation, as the order in which you apply these really matters
so,

``````ofPushMatrix();
ofTranslate(positionX, positionY);// use this to set the position of the center of the video
ofRotateZDeg(rotation);// this sets the rotation
ofSetColor(255);
video.draw(-video.getWidth()/2, -video.getHeight()/2);// this will make the center of rotation the center of the video
ofPopMatrix();
``````

this is what i have right now: I have a video file called chessGame moving from the left side of the screen to the right side of the screen. I need it to stay there for 10 seconds and then rotate around its centre 90 degrees. This is my code so far for the movement without the rotation.

``````**void** ofApp::update(){
chessGame.update();
**int** step = 10;
**if** (locX>=0 && locX<ofGetWidth()){
locX = locX+step;
}
}
**void** ofApp::draw(){
movingChess(0, 0, chessGame);
}
**void** ofApp::movingChess( **int** x, **int** y, ofVideoPlayer){
ofTranslate(ofGetWidth()/4, ofGetHeight()/2);
ofPushMatrix();
ofSetRectMode(OF_RECTMODE_CENTER);
ofScale(0.5);
ofRotateZDeg(0);
chessGame.draw(locX, locY);
ofPopMatrix();
}
``````

Hi,
please, edit your post and make the code look like code. it is super easy, just select it and press the `</>` button in the toolbar. It will be much easier to read.

well, There are a few problems with your code, first
the function `void ofApp::movingChess( int x, int y, ofVideoPlayer)` receives an ofVideoPlayer, and makes a copy of it, which then you are not using in the function itself. (it is a whole different story on how to deal with passing arguments to a function without copying, google “C++ function pass by reference” if you want to know more).
In the case of your code, it is inefficient and you are doing nothing with the x and y arguments too.
did you try something like?

``````ofPushMatrix();
ofTranslate(locX, locY);// use this to set the position of the center of the video
ofRotateZDeg(rotation);// this sets the rotation
ofSetColor(255);
chessGame.draw(-video.getWidth()/2, -video.getHeight()/2);// this will make the center of rotation the center of the video
ofPopMatrix();
``````

thanks again roymacdonald,

I understand your point about the misuse of the videoPlayer but this whole thing has to go into piMapper later and we were told to use functions to make life easier when dealing with fbo’s.

The code you gave me doesn’t really achieve the more difficult thing which is the time offset between the movement across the screen and the rotation. So far this is the closest I’ve gotten
but the centre point of the rotation is still off.

#include “ofApp.h”

``````//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0);
ofSetFrameRate(60);
chessGame.play();
ofSetBackgroundAuto(false);
locX = 0;
locY = ofGetHeight()/2;
angle = 0;
w = chessGame.getWidth()/2;
h = chessGame.getHeight()/2;
}

//--------------------------------------------------------------
void ofApp::update(){
chessGame.update();
int step = 1;
if (locX>=0 && locX<ofGetWidth()-625){
locX = locX+step;
}
if(ofGetElapsedTimeMillis()>10000){
angle = angle+1;
step = 0;
}
}
void ofApp::draw(){

movingChess(locX, locY, chessGame);
}
void ofApp::movingChess(int x, int y, ofVideoPlayer){
ofPushStyle();
ofPushMatrix();
ofTranslate(locX, locY);
ofSetRectMode(OF_RECTMODE_CENTER);
ofRotateZDeg(angle);
chessGame.draw(locX, 0, w, h);
ofPopMatrix();
ofPopStyle();
}
//--------------------
``````

If i comment out the locX conditional in the update the video file rotates around its centre, when i dont, it rotates off centre by quite a lot.

Hi,
you will need to do it this way regardless of using an `ofFbo`.

the problem is this line

``````chessGame.draw(locX, 0, w, h);
``````

Here, the position you specify is the center of the rotation. so if you want to make the center of rotation the center of the movie it should be

``````chessGame.draw(-video.getWidth()/2, -video.getHeight()/2);
``````

cheers

You were right, thank you!