Perspective view for scene2d actors

Anything libgdx related goes here!

Perspective view for scene2d actors

Postby ilkinulas » Sun Oct 13, 2013 10:10 pm

Hi, I am developing a card game (hearts) with libgdx and I am using scene2d.
What i am trying to do is:
* cards in my hand will be rendered as scene2d actors.
* cards on the table will be rendered with a "perspective" effect.

A picture is worth a thousand words, here is a screenshot showing what i am trying to do.
Any advice and suggestions will be greatly appreciated.
Attachments
Screen Shot 2013-10-14 at 12.44.52 AM.png
perspective view screenshot
Screen Shot 2013-10-14 at 12.44.52 AM.png (155.67 KiB) Viewed 5492 times
ilkinulas
 
Posts: 10
Joined: Tue Apr 02, 2013 11:08 am

Re: Perspective view for scene2d actors

Postby joaoneto » Mon Oct 14, 2013 12:20 pm

To render in perspective you need to use 3D.
joaoneto
 
Posts: 371
Joined: Sat Oct 01, 2011 11:26 am

Re: Perspective view for scene2d actors

Postby Kyu » Mon Oct 14, 2013 12:36 pm

If cards are statics, you can rotate and scale down actors to have a perspective effect.
Kyu
 
Posts: 389
Joined: Wed Aug 07, 2013 7:35 pm

Re: Perspective view for scene2d actors

Postby joaoneto » Mon Oct 14, 2013 1:14 pm

Kyu wrote:If cards are statics, you can rotate and scale down actors to have a perspective effect.

Rotate and Scale a 2D flat object don't create perspective.
https://www.google.com.br/search?q=skew+object&source=lnms&tbm=isch&sa=X&ei=9e1bUvHTN4KCyAHW24HgBQ&ved=0CAcQ_AUoAQ&biw=1280&bih=880&dpr=1#q=perspective&tbm=isch
joaoneto
 
Posts: 371
Joined: Sat Oct 01, 2011 11:26 am

Re: Perspective view for scene2d actors

Postby NateS » Mon Oct 14, 2013 1:56 pm

It would be interesting to use scene2d that way. You can replace the Stage's default ortho camera:

Code: Select all
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
PerspectiveCamera camera = new PerspectiveCamera(90, width, height);
camera.position.set(width / 2, height / 2 - 200, 200);
camera.near = 0.1f;
camera.far = 1000;
camera.rotate(30, 1, 0, 0);
stage.setCamera(camera);


You can adjust the position how you like obviously. Make sure you do not call Stage#setViewport! It works, here is UITest:

Image

You'll note that clipping is incorrect, because scene2d.ui uses ScissorStack which uses glScissors which uses screen axis aligned coordinates. If you don't need clipping this won't be an issue for you.

Another issue is scene2d doesn't expect a perspective camera, so screen coordinates aren't converted to world coordinates properly. To fix this, replace this method in Stage:

Code: Select all
public Vector2 screenToStageCoordinates (Vector2 screenCoords) {
   Ray pickRay = camera.getPickRay(screenCoords.x, screenCoords.y, viewportX, viewportY, viewportWidth, viewportHeight);
   Vector3 intersection = new Vector3(0, 0, 1);
   if (Intersector.intersectRayPlane(pickRay, new Plane(new Vector3(0, 0, 1), Vector3.Zero), intersection)) {
      screenCoords.x = intersection.x;
      screenCoords.y = intersection.y;
   } else {
      screenCoords.x = Float.MAX_VALUE;
      screenCoords.y = Float.MAX_VALUE;
   }
   return screenCoords;
}


Obviously you don't want to allocate in the method, I just hammered it out. There may be other methods you need to adjust, but then if you don't use those methods don't worry about them.
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: Perspective view for scene2d actors

Postby joaoneto » Mon Oct 14, 2013 2:01 pm

oO :shock: oO
That's awesome!
Nate are the Man!
joaoneto
 
Posts: 371
Joined: Sat Oct 01, 2011 11:26 am

Re: Perspective view for scene2d actors

Postby ilkinulas » Mon Oct 14, 2013 4:03 pm

Thank you,
I'll try the perspective camera solution, and post the results.
ilkinulas
 
Posts: 10
Joined: Tue Apr 02, 2013 11:08 am

Re: Perspective view for scene2d actors

Postby gsalgun » Mon Nov 11, 2013 3:02 pm

NateS wrote:It would be interesting to use scene2d that way. You can replace the Stage's default ortho camera:

Code: Select all
float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();
PerspectiveCamera camera = new PerspectiveCamera(90, width, height);
camera.position.set(width / 2, height / 2 - 200, 200);
camera.near = 0.1f;
camera.far = 1000;
camera.rotate(30, 1, 0, 0);
stage.setCamera(camera);


You can adjust the position how you like obviously. Make sure you do not call Stage#setViewport! It works, here is UITest:

..

You'll note that clipping is incorrect, because scene2d.ui uses ScissorStack which uses glScissors which uses screen axis aligned coordinates. If you don't need clipping this won't be an issue for you.

Another issue is scene2d doesn't expect a perspective camera, so screen coordinates aren't converted to world coordinates properly. To fix this, replace this method in Stage:

Code: Select all
public Vector2 screenToStageCoordinates (Vector2 screenCoords) {
   Ray pickRay = camera.getPickRay(screenCoords.x, screenCoords.y, viewportX, viewportY, viewportWidth, viewportHeight);
   Vector3 intersection = new Vector3(0, 0, 1);
   if (Intersector.intersectRayPlane(pickRay, new Plane(new Vector3(0, 0, 1), Vector3.Zero), intersection)) {
      screenCoords.x = intersection.x;
      screenCoords.y = intersection.y;
   } else {
      screenCoords.x = Float.MAX_VALUE;
      screenCoords.y = Float.MAX_VALUE;
   }
   return screenCoords;
}


Obviously you don't want to allocate in the method, I just hammered it out. There may be other methods you need to adjust, but then if you don't use those methods don't worry about them.


I'm implementing your solution for using perspective camera with scene2d stage. I want to rotate my Actors by Z index. What would you recommend for that?
gsalgun
 
Posts: 6
Joined: Wed Oct 09, 2013 6:24 am

Re: Perspective view for scene2d actors

Postby NateS » Mon Nov 11, 2013 10:11 pm

How could you rotate actors with Z index? :?:
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: Perspective view for scene2d actors

Postby gsalgun » Tue Nov 12, 2013 6:27 am

So it's not possible to make them have a Z axis. I was hoping to keep my scene2d members. I guess i will need to switch to Models when I need a Z rotation.
gsalgun
 
Posts: 6
Joined: Wed Oct 09, 2013 6:24 am

Next

Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], tenfour04 and 1 guest