libGDX Scene2d - Use screen units to draw text on world coordinates - libgdx

Recently began learning scene2d and developed a card game with it. I used world units for my coordinate system whereby one card width is 1f. The center of the screen is (0, 0) using world units.
Now I'm trying to draw a label on the center card placed at (1,0) but it's showing extremely large, no matter how much i scale the font, and i can't scale less than 0.05f.
I did extensive research and found out here that i can use camera.project for this, tried but couldn't work quite well. I'd appreciate any examples.
The goal is to temporarily project my world coordinates to screen coordinates, draw the text at the required position and with the correct size(in screen pixels), then unproject the stage back to world units, and continue further processes.
I'd show code if asked to, but i really just require some guidance here.

Related

Libgdx - how to have screens rotated within a single viewport?

I've built an game in Libgdx which uses a single FitViewport passed-in to some Screens (Splash/Intro/Game/Menu/Pause etc.).
Each Screen has it's own Stage containing Groups of Actors - I've written a custom render loop to allow Screens to fade-in/out or slide around or render behind each-other - that's all great.
I now want a 'UI' screen which will rotate on smartphones to match their orientation (everything else in the game will NOT rotate).
I can make this work visually by using a TransformMatrix on the SpriteBatch but that doesn't affect the Stage's 'touch' detection (or debugdraw) and it seems there is no way to do this within the Stage (localtoparentcoords allows for rotation and scale but NOT transformation)
Bear in mind that it will not be 'square' (the FitViewport enforces 16x9 ratio) so it needs translation as well as rotation...
Note: I've tried to mess around with cameras but that's the wrong paradigm - cameras are a different view of the same thing - I want different things (transformation and rotated) drawn into the same view!
Note also: I've already started to create my own version of a Screen/Stage class to do this - I think it might be quicker than kicking the existing code into working properly but I'll be surprised if I'm the first person to want this?
I think I've sort-of solved this by stepping-back a bit and looking at how the Stage/Actor system works.
The idea of rotating AND transforming an entire Stage is fraught with complexity - that I could do it at the SpriteBatch level was a distraction which led to a lot of wasted time - sadly.
It was only when I realised that as I was calculating the position of all my UI (Actor) elements relative to either the screen center or a corner, I may as well take the further step of rotating and transforming them at the same time! I also realised that Grouping them ,made this very simple (indeed I could have static and rotating elements simply by using 2 separate Groups!)
Rotating/Moving an Actor will, of course, adjust it's bounding box/touch-area as well - so I now have a proper Stage/Group/Actor model which rotates as the device is rotated - I could be just static information (scores) or even a dynamic menu or an overlay for a 'cue' in a pool game or whatever...
As Edison would have said - I didn't waste 2 days, I just spent 2 days coming up with a large number of ideas which I now know not to work!

Rotating a rectangular solid about the y axis without image distortion using canvas renderer (three.js)

I've spent several hours trying to work around this issue... when rendering really simple shape (ie. a cube with very low complexity) and using a texture map feature of Three.js, when you rotate the cube the image seems to be distorted while in rotation, and then you can see a line which runs across the surface of the cube which appears as distortion.
http://screencast.com/t/VpSPRsr1Jkss
I understand that is a limitation of canvas rendering - but it seems like this is is a really simple thing to do - rotate a cube that has an image on one face without the distortion.
Is there another canvas library or approach i can take? I was really looking forward to using Three.js for animating some logos and other elemnets - but we can't have distortion like that in a logo or a customer facing landing page.
Thanks for reading, I'm open to suggestions here.
I don't accept increasing the complexity of the face as a solution because that just distributes the distortion through out the face. I really just want to render the image to a flat surface and be able to rotate that object.
The distortion you see is because only two triangles make that plane.
A quick fix is to have more detailed plane.
If you are using PlaneGeometry, increase the number of segments.
If you are using CubeGeometry, increase the number of segments on the plane you need (2 out of 3).
It will take a bit of fiddling to find the best balance between a decent look and optimal performance (as more segments will require more computing). Hopefully for simple scene you'll get away with no major delays.

As3 Movieclip background image size

I am working with box2d, and generating a few grounds of variable size (all rectangles). The image the grounds use is 500px by 200px, however, when I create and size my grounds, the background image stretches to fill the body. I am trying to have it aligned to top left and just tile with no resizing but am really unsure of how to control it.
I imported the image as a movieclip to my library, if it helps, and also have a basic empty class for it (EarthGround.as). I've been doing a little research but really haven't found anything definitive about manipulating the movieclips associated image.
You have to be specific with that. Maybe a screen shot would help.
Until then, possible causes could be:
Box2D uses real world values where 1 pixel = 1 meter. You have to
choose a scale factor.
Box2d uses the center of any object as its registration point. You
should always do the same with your flash sprite/movieclip, to keep
it simple.

Gap Between Sprites that are Tiled and Scaled

I am working on a map application and I have come across an issue with how my tiles are laying while scaling.
Here is a basic look at my structure:
There is obviously a lot more going on, but you get the idea. Now, I scale the Map App Sprite to zoom in. When that scaling occurs, there is a gap between each tile.
You can see the gap where 4 tiles meet here:
I am caching everything as a bitmap. For each Layer (which all extend Bitmap), I have smoothing set to true and pixelSnapping set to PixelSnapping.ALWAYS (pixel snapping shouldn't help here, but it shouldn't hurt either).
Does anyone have any suggestions on how to fix this issue?
(For the sake of completeness, the Map app is built entirely using AS3 and it is embedded in a Flex app)
Using integers for tile x,y locations and calculating those locations correctly is most likely the fix here, unless the images have seams in them!
The code that calculates and sets the x,y locations would be needed to properly pinpoint the issue in the code.
But, also if you are scaling that container sprite, you would want to ensure that you scale so that the width/height of a tile is an integer value.
For example, if you scale your sprite that contains these tiles, the widths/heights of the individual tiles might not always be integers, therefore creating those seams you see.
What you could do in that case is do your scaling by adjusting your width/height values by integer values, taking into account proportions, as opposed to using scaleX and scaleY on your container sprite.
Without seeing your code it's difficult to be sure, but it is possibly just a visual artifact due to scaling - eg: a 250px wide bitmap scaled to 155% should be rendered at 387.5px wide but thats impossible so its rendered at 388px wide - with the 0.5px part rendered as 1px at 50% alpha to give 'appearance' of 0.5px.
Ensuring scaled bitmaps widths/heights are always integers may solve it?
This looks like a rounding error.
Without code it's hard to know: it would be a great asset to you and us if posted a barebones example of your tiling class. In the process of subtraction you may very well discover your solution.
I'd offer that you should test what happens when you scale and algin four 100x100 bitmap images at various fine grain steps, to detect if it's a Flash rendering issue or a defect in your class.

Is it encouraged, or even beneficial to use Flash/HTML Canvas optimization when drawing shapes

I am starting to get a grasp of the HTML canvas and I wanted to know if it is encouraged to use optimization techniques. In games for example, is it encouraged to use bounding boxes and only redraw the portion of the screen that needs redrawing, or do people just blindly redraw everything. I ask this b/c I am creating a new engine for canvas and imposing bounding boxes complicates things greatly (ie. the user no longer has the freedom to quickly draw things to the stage).
Absolutely, it's almost always better to draw only what has changed on the screen at any one time. Attempting to draw everything causes unnecessary rasterisation of the images being drawn into one output image, when it's not even going to change on the screen.
In a small game I made a year ago, I was drawing a 2D array of tiles on the screen and getting around 15 frames per second. When I changed it to draw only the tiles visible on the screen, it was an amazing improvement. It will improve in this circumstance too.