# Weird results from lookAt

why are some of these cubes Rolling? would expect them all to be evenly facing the origin like the middle column is.

``````void ofApp::setup(){

ofBackground(45,45,45);

}

void ofApp::draw(){
cam.begin();
ofEnableDepthTest();
ofEnableLighting();
pointLight.enable();

material.begin();

for(int j = -5; j < 5; j++){
for(float i = 0; i < TWO_PI; i+= TWO_PI/12){
ofBoxPrimitive box;
box.set(0.5, 0.5, 0.5);
box.setPosition(cos(i)*5, sin(i) *5, j);
box.lookAt(ofVec3f().zero(), ofVec3f(0,1,0) );
box.draw();
}
}

material.end();
ofDisableLighting();
ofDisableDepthTest();
cam.end();
}``````

I set the upvector to (0,0,-1) and I’m getting more of the results I would expect but there are still some weird stragglers in the group, look at the slight roll in the center column midway down

They are rolling because they are not on the same plane as the lookAt, the y axis is changing, while the y axis of the lookAt is always the same.

I thought the point of the lookAt function was: given an object and an arbitrary point in space, make the object face that point in space

That makes me think there shouldnt be roll involved. I guess the function I’m imagining takes an up vector and a forward vector for the object it’s reorienting

You could also do this with a Quaternion;
take the vector they are facing before the transform ( 0, 0, 1) as argument one and the vector between the target and their position as argument two.

code is from memory - might need tweaking:

``````ofQuaternion q;
q.makeRotate( ofVec3f(0, 0, 1),  box.getPosition() * -1.0 );
box.rotate(q); ``````