Libgdx OrthographicCamera - How to set graphical objects always to center? - libgdx

I have defined VIEWPORT_WIDTH and VIEWPORT_HEIGHT values. After that I would like to set all my objects to the center of this area.
By default I see all of my graphical items, sprites at the left corner. I use now this code:
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(camera.viewportWidth/2f, camera.viewportHeight/2f, 0);
How can I set "always center"?

An OrthographicCamera is always looking directly at its center. The center is equal to the position of an OrthographicCamera.
Thus, if you want to center your camera onto a certain object, you set the camera's position to the position of what you are trying to center on. For example:
camera.position.set(sprite.getX(), sprite.getY(), 0);

Related

How to get the relative position of a pointer inside a p-dialog element?

I have a p-dialog with a fixed position in the center of my screen. Inside the p-dialog I have an image on which should appear a small dot when clicked on the position of the click.
When I set the position of the dot as: positionX = $event.offsetX it works fine when the application is on fullscreen. However when I resize the window the offset value changes and the dot appears moved to the side.
I tried to do as follows:
const rect = $event.currentTarget.getBoundingClientRect()
positionX = $event.pageX - rect.left
or using $event.client.x but each time the position is extremely wrong reaching negative or too big values. I get really confused with all the values we can get from PointerEvent and none of them seem to let me calculate the correct position of the dot. I feel like I would need to get the current size of the image relative to the size of the screen so I can manage to calculate the value of the offset, but in the $event.target the width stays always the same, but the offset changes.
I need to get the relative position to the image so I can place the dot properly on any screen size. How to achieve this?

How to set subclass CCNode size?

I have a CCNode subclass and I have draw several lines on it. And I want to add this CCNode subclass to other Layers. However I don't how big this CCNode is. And the center of coordinate is the lower left corner of screen.
So my question is how can I change the CCNode size? And how can I change the center of coordinate when I am using my custom CCNode?
Now I am using Cocos2d-x-2.1.5
CCNode Size means I think it is setContentSize.
You can user setPosition method of CCNode to change position.(to change the center of coordinate) It depends on AnchorPoint also.
Just refer CCNode.h. You will get to know what you have to do.

scaling and positioning with tweenlite

I have a movieclip with the mesurements of a rectangle. When the application is launched the movieclip is being scaled before placed on the stage as following.
menu.width = 400;
menu.scaleY = menu.scaleX;
this is a smaller size than the original.
the position of the movieclip at this moment is in the middle on the x and top of the stage on the y.
when i click iti would like to do a tween with tweenlite wich scales it to its original(bigger) width and height and position it in the center of the stage on x and y.
the problem is when i position it with tweenlite, it gets done according to its old scale and not according to its new(bigger) scale so the movieclip isnt placed in the exact center of the stage.
Anyone know how i can resolve this?
I tried to compensate by adding a bigger number on the position so it gets positioned in the right way.
But when i click it again i would like it to rescale to its beginning scale and position so it would be very messy to compensate again. Is there any easier way to do this kind of tween?
I doubt that i'm being clear to what i want but i hope i am after all.
The easy and way of tween position and scale is probably to add the menu to a container.
You would then on one hand tween the position of the container, and on the other apply the scale to the menu it self without having to recalculate the proportional position if the scale changes.
This way you can even control the registration point of the menu within the container. e.g. to always scale from the middle...
This can be done with a matrix as well if you want to avoid to stack objects, but honestly it can get really tricky whereas the container method is bullet-proof.

How to prevent external translation of a movieclip object on stage in AS3?

I have a MovieClip object, which is exported for actionscript (AS3) in an .swc file.
When I place an instance of the clip on the stage without any modifications, it appears in the upper left corner, about half off stage (i.e. only the lower right quadrant of the object is visible). I understand that this is because the clip has a registration point which is not the upper left corner.
If you call getBounds() on the movieclip you can get the bounds of the clip (presumably from the "point" that it's aligned on) which looks something like (left: -303, top: -100, right: 303, bottom: 100), you can subtract the left and top values from the clip x and y:
clip.x -= bounds.left;
clip.y -= bounds.top;
This seems to properly align the clip fully on stage with the top left of the clip squarely in the corner of the stage.
But! Following that logic doesn't seem to work when aligning it on the center of the stage!
clip.x = (stage.stageWidth / 2);
etc...
This creates the crazy parallel universe where the clip is now down in the lower right corner of the stage.
The only clue I have is that looking at:
clip.transform.matrix
and
clip.transform.concatenatedMatrix
matrix has a tx value of 748 (half of stage height) ty value of 426 (Half of stage height)
concatenatedMatrix has a tx value of 1699.5 and ty value of 967.75
That's also obviously where the movieclip is getting positioned, but why? Where is this additional translation coming from?
I believe that I have solved the problem.
It seems that when the stage is not set to specific dimensions, the scale of the items (or this particular vector object in my case) on the stage is relative to some preset scale/dimensions (The origin of which I'm not sure -- with some testing it looks like it is affected by the ratio and dimensions of the stage).
Increasing the size of the stage increases that scaling value, which through some set of concatenations of matrices results in the movieclip getting scaled and translated at that same percentage (as the stage), even if you explicitly set the x/y/width/height values.
(Note: not just resizing dynamically. Resizing the window, closing and re-opening the window at that new size produces the same behavior)
I suspect this only happens with vector objects?
Anyway, the fix is to set the stage to an explicit width and height. Then the translation and scaling of the object appear to use an identity parent matrix, and everything works like you'd expect.

Resize a movieclip wihout messing up mouse coordinates

How do I do this? I have a seekbar which I'm retrofitting to resize with the FLVPlayback. Right now it gets the mouseX when you click the seekbar, and divides that by the length of the seekbar in order to know what percentage it should seek to.
My problem is that now that I'm dynamically setting the width of the seek movieclip the width returns the width I set, while the mouse event returns the position as if it was still the original width it has in the library.
How do I get around this problem?
Just scale against the original width you had. Say that you've got a 100px wide bar, just go:
var seekTo:Number = seekbar.mouseX / 100;
This will give you the percentage clicked at regardless the current width of the seekbar.
Nice and easy and no funky coord space conversions needed.
The MouseEvent object has a stageX and stageY property - you just need to offset those values by the position of the clip and you should be set.