Libgdx Position camera in TiledMap - libgdx

I have create a sample tile map like this, I am confused on how to show the camera on the center of the tilemap or in a specific position.
the output looks like this. how do I make the camera show the center of the tilemap also how do I make the image show smaller

The camera's position is always considered to be the center of the camera.
An uninitialized camera will point to (0,0) and show as much space to the top, bottom, left and right as the viewport allows.
You can set the position in the create method:
OrthographicCamera camera = new OrthographicCamera(width, height);
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
camera.update();
Do not forget to call the update method after any changes or they will not be visible.
In order to show the camera in a specific position after creation you need to move the camera by calling
camera.translate(x,y,z);
To zoom in and out simply set the zoom:
camera.zoom = someFloat;
If this does not make sense i recommend researching more about the orthographic camera
(https://github.com/libgdx/libgdx/wiki/Orthographic-camera)
and viewports (https://github.com/libgdx/libgdx/wiki/Viewports).
If this does not help at all, because I misunderstood something, post more information plus some code and we will figure something out.

Related

Problem- can't get the accurate coordinates of mouse on page resize, when using html5 canvas

I wanted to create this Pixel Effect from Frontend Expert.
Although I was able to implement the entire pixel effect on a full screen canvas:
const canvas = getElementById('canvas');
canvas.height = window.innerHeight; // Gives the canvas height of fullscreen
canvas.width= window.innerWidth; // Gives the canvas width of fullscreen
and got the coordinates of the mouse pretty easily
const mouse = {
x: undefined,
y: undefined
}
canvas.addEventListner('mousemove', function(e) {
mouse.x = e.x;
mouse.y = e.y;
}
About now the canvas width and height was equal to that of the document, therefore, it was pretty easy to get the exact coordinates of the mouse.
But when I tried to implement it with 800px X 400px dimensions and used a flexbox with it (like shown in the website) my mouse coordinates got completely messed up and I spent hours in fixing but wasn't able to get the accuracy as shown in the above website. Also there were some issues related to resize.
I would like to know how can I preserve the mouse accuracy.
Your help is much appreciated.
I believe when you use e.x and e.y, even though used in an event listener for tha canvas, they return the mouse coordinate relative to the top-left pixel of the entire page, not just the canvas. If by messed up, you mean that anywhere you click, the pixel effect is offset in some constant direction, this may be your issue and you should replace e.x and e.y with e.clientX and e.clientY. The ‘client’ in e.clientX refers to which element the listener is for and specifies to give event coordinates relative to that element instead of the page. If it is messed up in some other way, then I don’t think I have an answer for that.

Image crop position with HTML5 Canvas

I have an image which is 640px by 480px and I have coordinates where I want to create a canvas crop of the image. I am a bit confused as to how to work out for 1, the crop size, where to crop and so on.
How would one work out the correct position to crop and the crop size? e.g. what size to set the canvas from coordinates such as the following
coordinates = [160, 593, 345, 407] // [top, right, bottom, left]
essentially im returning image coordinates from a Python script but no idea how to work out the correct positions.
The drawImage() method takes arguments for source and destination coordinates.
context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);

AS3 Side Scroller Not exactly how I want it

Im trying to create a side scroller game on actionscrip3.0. I have it down and everything but it's not exactly what I'm looking for. I want my character to always be in the center of the screen. (www.realmofthemadgod.com) Is a great example of what type of side scroller I want my game to be like. Any help would be greatly appreciated.
Your best bet and mine is to implement a world camera, as I call it, to your game.
Each of your game objects will have two sets of coordinates. One is in relation to the game world (world coords), the other in relation to the camera (screen coords).
World coords are for everything other than displaying. So stuff like physics. Screen coords are just for displaying.
Your camera will also need a set of world coords. It doesn't need screen coords because that doesn't make any sense.
In the end your objects' screen coords will just be the differences between theirs and the camera's world coords.
For example if your object is at (100, 100) and your camera is at (90, 90) then your object will look like it's at (10, 10). Object at (200, 200) and camera at (150, 250) will end up at (50, -50).
Keep in mind the origin point of your screen coords is the top left corner of your screen by default. If you want the origin to be the center of your screen, that is an object that is right on top of the camera should appear in the center of your screen, you must add half the width and half the height of your screen to your final screen coords.

Increasing the width of line drawn using Shape Renderer in LibGDX

I am drawing a line using Shape Renderer in LibGDX and i am using Orthographic Camera in my code, so to increase the width i used
camera = new OrthographicCamera();
int lineWidth = 8; // pixels
Gdx.gl10.glLineWidth(lineWidth / camera.zoom);
Now, I get a wider line. But the problem arises when the screen power goes off and then turns on, the line becomes the normal one again. How to keep the width constant?
ShapeRenderer has the method rectLine that is intended to be used to draw thick lines. It draws a rotated filled rectangle with the smaller opposite sides centered on the points passed to it (as coordinates or as Vector2 objects). The method has the parameter width to set the line width.
You can see the documentation here
Example:
shapeRenderer.rectLine(new Vector2(x1,y1),new Vector2(x2,y2),lineWidth);
As you can see here:
libgdx Shaperenderer line .. How to draw line with a specific width
And here:
Libgdx gl10.glLineWidth()
And here:
Why Libgdx Gdx.gl10.glLineWidth(width); does not react to change projection properities
Playing with the line width in the shaperenderer isn't the best way to do what you want to achieve.
Try using a 1x1 white TextureRegion (you can change the color the Batch draws it with later), and draw it with the width and height that you desire:
batch.draw(region, x, y, width, height);

AS3 - Display Coordinates of User Interface Elements

I'm looking for a way to determine the display/screen coordinates of a movie clip element.
point = uie.localToGlobal(new Point(uie.x,uie.y));
posX = point.x + (uie.width / 2);
posY = point.y + (uie.height / 2);
The code above only retrieves coordinates within the stage. However - I need display/screen coordinates and NOT stage coordinates.
Maybe there is a way to retrieve the display/screen position (top left corner) of the flash container or move the mouse to a position within the stage, retrieve the screen/display and stage position of the mouse and determine an offset between screen position and stage position. Is this possible within (!) AS3?
This isn't possible within the Flash player. You could try to get the coordinates of the flash container with javascript this will get you a coordinate relative to the browser window. Javascript can get the browser's screen position relative to the desktop. You need the window.screenLeft api of the browser. With this you can get the position of the flash element on the page and add it to the browser window. This will give you the correct position with a small error margin.
You can also achieve this with Adobe Air. With the NativeWindow api.
stage.nativeWindow.x = 0
stage.nativeWindow.y = 0
This code snippet will set the window in the top left corner for example.