Jump to content

[SOLVED] Player::getScreenPosition not working


photo

Recommended Posts

Hi, I am trying to get the screen position in Unigine 2.3.1 for world coordinates and it always returns 0,0

 

I am drawing the three points on the screen with the visualizer and they are showing up, so I know they are on screen and visible.  Capturing the screen and finding their points in a paint program I can also figure out what their screen coordinates are.

 

However, when I try to figure it out in code, it always returns 0,0.

 

I've also adapted the getScreenPosition script function to c++, but that doesn't quite work either.  Maybe there's a bug in my conversion code.

 

Anyways, how do I get the screen coordinate in c++?

 

Here's my c++ code:

 

  int screenX, screenY;
  player->getScreenPosition(screenX, screenY, Vec3(worldBase), App::get()->getWidth(), App::get()->getHeight());
  Log::message("screenBase 2 %f,%f\n", screenX, screenY);  //always outputs 0,0
In the case above, worldBase is set to (0,0,0).  In my example, it should be outputting 960, 540.
 
As I said above, I also tried converting your getScreePosition script function to c++, but it doesn't work, maybe you can spot a bug with it?  I can't.
 
dvec2 getScreenPosition(const dmat4& mvp, const dvec3& point) 
{
  int h = Gui::get()->getHeight();
  int w = Gui::get()->getWidth();
  dvec4 p = mvp * dvec4(point, 1.0);
  return dvec2(w * (0.5 + p.x * 0.5 / p.w), h * (0.5 - p.y * 0.5 / p.w));
}


dmat4 getModelViewProjection()
{
  PlayerSpectatorPtr player = PlayerSpectator::cast(Game::get()->getPlayer());
  mat4 projection = player->getProjection();
  Mat4 modelview = player->getIWorldTransform();
  projection.m00 *= float(Gui::get()->getHeight()) / Gui::get()->getWidth();
  dmat4 wvp = dmat4(projection) * dmat4(modelview);
  return wvp;
}

I calculate the modelViewProjection matrix once and then pass it to all the getScreenPosition calls.

 

You may also wonder why I'm not doing the w < 0 check.  I happen to know these points will always be on screen, so I just skipped that.

Link to comment

Ok, I am an idiot.

 

I was using %d in my log statement instead of %f, so it output 0.  getScreenPosition is working ok.  I couldn't ever figure out what I was doing wrong with my own c++ conversion, but it doesn't matter as I can use Player::getScreenPosition.

Link to comment
×
×
  • Create New...