Position of objects does not match

Hi,

I draw lots of cube as my ground and then i have a little tank model I can move around using it’s node for direction and moving it’s node and model position.

calculate_movement(); // Change the position value
model.setPosition(position.x, position.y, position.z);
node.setPosition(position.x, position.y, position.z);

but the tank position coordinate seems to be wrong when i compare to the position of a cube drawn in the scene altought the tank is drawn where it should be…
I’m a bit confused. Does this have something to do with a ofScale() or ofRotation i would have applied to the cubes or something to do with the camera?

Hmm I think you would need to share a bit more of the code but it could easily be related to ofScale or ofRotate as those affect the whole world coordinate system unless wrapped with an ofPushMatrix(); ofPopMatrix();

Can you share the draw function, that might help provide some insight?

Thanks!

Well my main draw call is in my Renderer classe like so :

void Renderer::draw() {
    playerCam.setScale(-1, 1, 1);
    playerCam.begin();
    ofEnableDepthTest();
    light.setDirectional();
    light.setSpecularColor(ofColor(255));
    light.setDiffuseColor(ofColor(255));
    light.setAmbientColor(ofColor(100));
    light.enable();
    light.setPosition(ofVec3f(100, -2000, 200));
    light.lookAt(ofVec3f(0, 0, 0));
    ofClear(0, 0, 255);
    currentLevel.drawGame();
    light.disable();
    ofDisableDepthTest();
    playerCam.end();
    //Show game commands in the top corner of the screen
    ofPushStyle();
    ofSetColor(0, 0, 0);
    ofDrawBitmapStringHighlight("Use Up/Down key to move", ofVec3f(10, 60, 0));
    ofDrawBitmapStringHighlight("Use Right/Left key to rotate", ofVec3f(10, 80, 0));
    ofDrawBitmapStringHighlight("Use Space key to shoot", ofVec3f(10, 100, 0));
    ofPopStyle();
}

Then the currentLevel.drawGame() from my Level classe :

void level::drawGame() {
   themeTexture.bind();
  
      for (auto const& o : levelGameObjects)
      {
          ofPushMatrix();
              ofTranslate(o->position);
              ofScale(cubeSize, cubeSize, cubeSize);
              if (o->rotation.x != 0) ofRotate(o->rotation.x, 1, 0, 0);
              if (o->rotation.y != 0) ofRotate(o->rotation.y, 0, 1, 0);
              if (o->rotation.z != 0) ofRotate(o->rotation.z, 0, 0, 1);
  
              if (o->tileColor == GROUND_COLOR) {
                  mVboGround.draw();
              }
              else if (o->tileColor == SOLID_COLOR) {
                  mVboSolid.draw();
              }
              else if (o->tileColor == WALL_COLOR) {
                  mVboWall.draw();
              }
              else if (o->tileColor == OUTER_COLOR) {
                  mVboOuter.draw();
              }
          ofPopMatrix();
      }
      // Draw turrets and targets
      for (auto const& o : levelEntityObjects)
      {
          o->model.setPosition(o->position.x, o->position.y, o->position.z);
          if (o->rotation.x != 0) o->model.setRotation(0, o->rotation.x, 1, 0, 0);
          if (o->rotation.y != 0) o->model.setRotation(1, o->rotation.y, 0, 1, 0);
          if (o->rotation.z != 0) o->model.setRotation(2, o->rotation.z, 0, 0, 1);
          o->model.setScale(o->scale.x, o->scale.y, o->scale.z);
          o->draw();
          if (showBoundingBox) draw_bbox(o->model);
      }
      themeTexture.unbind();

    (*tank).draw();
}

and finally the tank.draw :

void tank::draw()
{
	shader.begin();

	shader.setUniform3f("color_ambient", color_ambient[0], color_ambient[1], color_ambient[2]);
	shader.setUniform3f("color_diffuse", color_ambient[0] / 2, color_ambient[1] / 2, color_ambient[2] / 2);
	shader.setUniform3f("light_position", light.getGlobalPosition());

	model.drawFaces();

	shader.end();
}

That’s about it for the draw… tell me if you need anything else

Thanks

Well i found my problem, nothing to do with a ofScale of ofRotate it’s just me being dumb, the code was checking one of the cube that was indeed far from my tank model, if i do compare with a cube supposed to be at my position than everything is just fine!

Thanks for taking the time tought

1 Like