Android: How to make a squared tile clip on a grid - java

I'm new to android programming and have some questions. I'm trying to create a tile puzzle game where I have to move squared tiles into a grid and the tiles would automatically clip onto place. For example, I have a 3x3 squared grid with 9 position. Each position measures 100x100 pixel. If I drag a tile(also 100x100 pixel) to any of the 9 positions inside the grid, the tile would automatically clip in place inside the 100x100 pixel area even if it's a bit off. How do I do this?

x=((int)(x/100))*100
y=((int)(y/100))*100
for example if you have x=140 y=260 then
((int)(140/100))*100=100
((int)(260/100))*100=200
Because of the (int) the ((int)(140/100)) is 1 not 1.4 so the point of this is to round the number

Related

Efficient algorithm for detecting center of ellipse

I'm implementing a virtual world for an autonomous machine. The machine has to be able to detect the center coordinates of different colored ellipses in the screen.
The image is given to the machine in an width x height array, with ints for the colors (for example white = 0, red = 8, blue = 9 etc).
Also if the ellipses are behind eachother you do not have to calculate the excact middle point.
I was thinking of some sort of sweepline algorithm? Which will keep the first time it sees a color and when it exits it gets the average between those two? and then do it horizontally and vertically. Then you have the middle coordinates.
Would this be efficient? Any other ideas?
Thanks in advance

libgdx sprite dimension meters or pixels?

I have been reading "Learning Libgdx Game development". I tried the below snippet:
// First the camera object is created with viewport of 5 X 5.
OrthographicCamera camera = new OrthographicCamera(5, 5);
I have a texture having a dimension of 32 pixels by 32 pixels. I form a sprite out of this
Sprite spr = new Sprite(texture);
// I set the size of Spr as
spr.setSize(1,1);
According to the book the dimensions above are meters and not pixels.
What I don't understand is how is mapping from meters to pixels happening on the screen? When I draw the sprite on the screen the size is not even half a meter let alone 1.
Also, the size of the underlying texture is 32 X 32 pixels. WHen I resize, the size of my sprites also changes.
Then, what would be the dimensions of spr.setPosition(x, y)? Will they be meters or pixels?
The library uses pixels for dimensions like texture size, and meters for in-game units.
setPosition will move an object in game units. When you move an object X game units, the number of pixels changes based on the camera's projection matrix amongst other settings.
If you think about it, it wouldn't make sense to move in pixels. If camera A is zoomed in more than cameraB moving X pixels in the view of each camera would require moving two different amounts.
Edit: Sorry, I made some assumptions in your understanding above, partially misunderstood the question, and frankly used the misleading wording. The key is that the convention of meters for units is not built-in, it's one that you enforce because the ratio of one pixel to one meter in Box2D wouldn't make sense. The wording I used implied that internally setPosition cares about meters, but you should be doing the scaling yourself. Often times the ratio I see in libgdx is 30 pixels = 1 meter.

View Following Object Java JFrame

I have a problem that I'm not sure how to overcome. What I'm working on right now is a game that just generates an infinite floor of dirt, and I have a player object. I would like to keep the player object in the center of the screen, but move the view. I thought that this would be a common subject, but I haven't been able to find anything related to it on the internet. Do I have to move every other instance and keep the player still to simulate a moving view?
Thank you,
~Guad
Yes, you move every other instance except the player.
One way to display a large area of terrain is to use tiles. A common size for terrain tiles is 256 x 256 pixels. You create a grid of tiles that's a bit larger than the game area.
Let's say we have an 800 x 600 pixel display area. This would be completely covered by a 4 x 3 area of tiles. In order to make the motion smoother, you create a 6 x 5 area of tiles in memory, and display 800 x 600 pixels of the 6 x 5 area.
As the player moves to the right, you add 5 tiles to the right of the 6 x 5 area, and drop the 5 tiles on the left. On the screen, it looks like the player is covering a great distance, but you're just adding and removing terrain tiles. You would add and remove tiles in a similar manner when the player moves up, down, and to the left.
I hope this is enough information to get you started.

Warping a contour to a perfect square Mat in Java OpenCV Android

I, like many before me, am trying to digitize a Sudoku board from an image. I have:
Gray-scaled, blurred and thresholded the image into a crisp, binary image
Identified the largest contour which correctly corresponds to the perimeter of the Sudoku board
However, if the paper is curved, the board's contour won't fit neatly into a boundingRect; its edges will be elliptical.
What I would really like is a way of taking some MatOfPoint maxContour;
and warping the corresponding region in the parent Mat to be a square, so that I can remove the board as a perfect square and manipulate it on its own.
However, ANY advice on getting the board or cells out of the image of Sudoku board are appreciated, I've played with Hough Transforms, the Sobel algorithm, and many other hare-brained schemes that have left me a little frazzled.
Thanks in advance!
One approach is to first get the Minimum bounding rectangle of the rectangle represented by your sudoku corners which is effectively a perspectively transformed rectangle.
Then use the sudoku corners with the resulting rectangle corners to warp the image (or just warp the corners, whatever suits you), this will result in a straight image.

Finding Coordinates on a Bufferred Image

I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage like this, you save a Rectangle for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains method of Rectangle to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout or BoxLayout works well.

Categories