Co-ordinate Project / Drawing Text Over Models

Anything libgdx related goes here!

Co-ordinate Project / Drawing Text Over Models

Postby Beale » Wed Mar 18, 2015 5:00 pm

Good evening.

I'm currently trying to write text over 3D models in a scene.

Of course, this involved projecting the models co-ordinates to screen co-ordinates.

Currently, my render code is like so:

Code: Select all
   
public void render(PerspectiveCamera _camera, Array<ModelInstance> _modelInstances)
{
      _spriteBatch.setProjectionMatrix(_interfaceCamera.combined);
      _spriteBatch.begin();

      Vector3 _vector = new Vector3();
      
      _modelInstances.get(4).transform.getTranslation(_vector);
      
      Vector3 _project = _camera.project(_vector);
      
      _font.draw(_spriteBatch, "MARS", _project.x, _project.y);
      
      _spriteBatch.end();
}


So, I'm picking the fourth model instance, getting its co-ordinates and supposedly projecting to screen co-ordinates.

Now, here's the problem.

The draw position is totally off!

The text I believe should be displayed over the fourth planet, yet it appears to float elsewhere.

Image

Thanks for any help!
Beale
 
Posts: 42
Joined: Sat Mar 07, 2015 4:07 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby xoppa » Wed Mar 18, 2015 9:10 pm

You want to transform world coordinates into hud coordinates. The easiest to understand is to project using your world camera (_camera) and then unproject using the hud camera (_interfaceCamera). You don't actually need to convert to screen coordinates (pixels) in this case (which is what camera#project/unproject does), you can directly project the vector instead:
Code: Select all
_vector.prj(_camera.combined).prj(_interfaceCamera.invProjectionView);
xoppa
 
Posts: 689
Joined: Thu Aug 23, 2012 11:27 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby mmachida » Wed Mar 18, 2015 9:25 pm

I use this for 2D game co-ordinates (game -> screen / screen -> game), maybe you can adapt for your 3D/perspective game.

Code: Select all
Vector3 newPos = new Vector3();
newPos.set(planetX,  planetY, 0);
camera.unproject(newPos);

font.draw(_spriteBatch, "MARS", newPos.x, newPos.y);


I think xoppa's code can be better and easy for what you want.
mmachida
 
Posts: 145
Joined: Tue Mar 25, 2014 8:26 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby Beale » Thu Mar 19, 2015 9:44 am

xoppa wrote:You want to transform world coordinates into hud coordinates. The easiest to understand is to project using your world camera (_camera) and then unproject using the hud camera (_interfaceCamera). You don't actually need to convert to screen coordinates (pixels) in this case (which is what camera#project/unproject does), you can directly project the vector instead:
Code: Select all
_vector.prj(_camera.combined).prj(_interfaceCamera.invProjectionView);


Thank you both for the advice!

The "_vector.prj" code works beautifully.

Image
Beale
 
Posts: 42
Joined: Sat Mar 07, 2015 4:07 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby Beale » Wed Mar 25, 2015 5:46 pm

Beale wrote:The "_vector.prj" code works beautifully.


Hello!

Sorry to bump up my own thread, but it seems better than creating a new one.

There is a small problem with the code, that objects that are behind the camera also have their names projected onto the screen.
How may I alter the code so that only objects inside the field of view will have their names rendered?

Thanks & Kind Regards.

For example - Jupiter here is behind the camera, yet its name is rendered.
Image

Code: Select all
   
public void renderPlanetNames(PerspectiveCamera _camera, SimBody _bodies[], Array<ModelInstance> _modelInstances)
{
      _spriteBatch.setProjectionMatrix(_interfaceCamera.combined);
      _spriteBatch.begin();

      Vector3 _vector = new Vector3();
      
      for(int i = 0 ; i < _modelInstances.size ; i++)
      {
         _modelInstances.get(i).transform.getTranslation(_vector);
         _vector.prj(_camera.combined).prj(_interfaceCamera.invProjectionView);
         _font.draw(_spriteBatch, _bodies[i]._name, _vector.x+15, _vector.y);
      }
      _spriteBatch.end();
}
Beale
 
Posts: 42
Joined: Sat Mar 07, 2015 4:07 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby evilentity » Wed Mar 25, 2015 6:48 pm

So dont render it if its behind?
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4867
Joined: Wed Aug 24, 2011 11:37 am

Re: Co-ordinate Project / Drawing Text Over Models

Postby Beale » Wed Mar 25, 2015 7:53 pm

evilentity wrote:So dont render it if its behind?


This is the idea...

But, I do not know so well the mathematics involved, or there is a function?

Kind regards.
Beale
 
Posts: 42
Joined: Sat Mar 07, 2015 4:07 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby xoppa » Wed Mar 25, 2015 8:07 pm

xoppa
 
Posts: 689
Joined: Thu Aug 23, 2012 11:27 pm

Re: Co-ordinate Project / Drawing Text Over Models

Postby Beale » Wed Mar 25, 2015 8:37 pm

xoppa wrote:http://blog.xoppa.com/3d-frustum-culling-with-libgdx/


So, I can use this code just?

Also - If I replace instance.transform.getTranslation, with my own Vector3, there's no problems?

Thanks.

Code: Select all
protected boolean isVisible(final Camera cam, final GameObject instance) {
    instance.transform.getTranslation(position);
    position.add(instance.center);
    return cam.frustum.sphereInFrustum(position, instance.radius);
}
Beale
 
Posts: 42
Joined: Sat Mar 07, 2015 4:07 pm


Return to Libgdx

Who is online

Users browsing this forum: MSN [Bot] and 1 guest