How do you go about getting tiles in a 2d tilebased game? - java

I'm currently working on making a 2d tilebased game in java.
The way my game works right now is: Finding out which tiles are inside the screen area, then rendering these. The tiles are stored in an int array, where each tile is represented by a number.
For example 1 is grasstile and 2 is dirt tile.
It uses a getTile method to decide what numbers is at the specific coordinates, then returns a tiletype based on what number is at that coordinate, example:
int[] tiles=new int[width*height];
if(tiles[x+y*width]==1) return grassTile;
else if(tiles[x+y*width==2]) return dirtTile;
else if..... and so on
But i'm wondering if it would be more efficient to make an array of tile objects?
Tile[] tiles=new Tile[mapWidth*mapHeight];
return tiles[x+y*width];
I'm wondering what is best performance wise?
Sorry if this is a bad question. I'm new to programming and I can't seem to find this anywhere else.
Thanks you!

The performance cost of either of those methods is so positively tiny compared to the cost of doing graphical rendering that you really shouldn't care. Instead of worrying about performance there, you should focus on writing clear, robust, extensible code. With that in mind, I'd recommend:
1) Use a Tile class. It will allow you to add much more than tile_types to them later on.
2) Use new Tile[mapWidth][mapHeight], and address tiles as tiles[x][y]. This approach costs more memory, and you will have to create all of the sub-arrays yourself, but it guards you against a lot of easy-to-make off-by-one errors, and other mathematical mistakes.

Related

Sudoku board generation logic in Java

I want to make a program that will randomly generate a sudoku board to play. For those not familiar with the game, you are given a 9x9 gameboard with mostly empty spaces and some numbers pre-filled in. To complete it you must fill in the empty squares so there is 1-9 in every row, column, and 3x3 square, but only once.
The way I am currently imagining it is to use a 2d array. I use a for loop within a for loop to populate it, making sure that the number going into the square is never one that has already been used in the same row or column.
The problem I am having is how to check if the number is already used in the 3x3 part of the grid. I was thinking about using a 3d array and the third dimension is where the 3x3 data is stored, but I don't know a simple way to decide which array to check in for the current square.
I also don't know for sure if randomly generating the tiles the way I am will always produce a complete board. I am worried it might get to one of the last few tiles and find that no number between 1 and 9 will fit in the square because it is already used. I don't know how to check if this is a possibility.
I did do a minimal amount of looking at other questions on the topic but I don't want to accidentally come across the answer, I just need a pointer in the right direction. Also none of them seemed to be directly related to what I am asking.
Hopefully what I am asking makes sense, it is a little difficult to describe in text. I would appreciate it if you could give me a pointer in the right direction without giving me the answer. Also if you don't know much about sudoku (why did you click on this question) you can play it here:
http://www.websudoku.com/
If you need clarification I will respond to comments as quickly as I can.
I'll try to just give you some hints rather than giving you the answers outright.
First, its a great idea to use a 2d array -- that is exactly what a sudoku board is. As for your 3d array idea, it is a bit overcomplicated. Think about using mathematical functions to find the top corner of each 3x3 box (i.e. [0, 0], [0, 2], [2, 0], etc.) and use a for loop to traverse that 3x3 box (still in the 2d array). As for generating the board by putting numbers randomly in, it might not work, and board generation is maybe not as trivial as you might think provided you want each board to have only one correct solution. Make sure you can check board validity first, then take a look at the link posted by kenshinji.

Which method of managing an array of tiles is better?

So in this game I'm programming the "board" is an Array of tiles. I have to get which type of tile is at certain positions and move them around. Not every position that could have a tile has a tile.
I see two ways of doing this.
1) Have a 2D Array of tiles. The dimensions of the board in tiles would be equal to the dimensions of the Array. Places where there is no tile would be represented by null. These would be organized so that myArray[1][1] would refer to the tile at (1, 1).
Pros: Easy to find a tile at a specific coordinate
Cons: Seems bulky, many of the Tiles in the array will be null.
2) Have a regular unordered ArrayList of tiles. Since tiles have an x and a y component, I do not need to sort this.
Pros: Lightweight, takes less memory
Cons: When I need to find a tile at a specific coordinate I will need to use a for loop and search through every tile
Thanks :)
The question in my mind is: is it better to use a concrete array or a sparse array, and the solution (again in my mind) is "it depends". If your universe is potentially very large, then definitely use the sparse array, else you'll be wasting resources on cells that have no current relevance. If on the other hand the universe will be well limited in size, then the convenience of a concrete array (or some type of collection) will come to the fore.
You can try hashmap. Where keep the coordinate as key. And tile value at value position.
Ex pos(2,3)->2.3 so on ..
This way search and memory both can be optimized.

Collision Detection in libgdx

My question is mostly related to the theory behind it. I make a 2D game for a project and i detect collisions by using the .overlaps method in the Rectangle class and the collisions are handled beautifully. First of all , is that considered to be a continuous or discrete collision technique. As i'm reading the theory i say it is discrete ,however i'm reading in online articles that the major disadvantage of discrete is that it detects collision after it actually happened. So,my question is the following : is it actually discrete and if it is why i see no disadvantages?
Thanks
This is discreet because we only know if two bounding boxes collided after we check if the imaginary/invisible boxes intersected meaning they already overlapped. So by the time you take action (update) due to that collision, the objects are not in the collided position. Worse case, if they are not in relative speed, they can pass through. Think of the classic helicopter game where you dodge obstacles by going up and down. Say you put the velocity of the chopper on x really high, depending on your frame rate which depends on the hardware, you will see different positions of actual collision. For continuous, one object has to be aware of the physics properties of the other objects it may collide with to predict possible collision.
In reality, for 2d games like the helicopter game I mentioned, it really doesn't matter much. You can simulate the result of the collision by doing changes on an object's rotation, velocity, gravity and through some nice animations. If your game objects have abstract shapes, you should use something like box2d. There's a good Intersector class as well.
Also, you can experiment with different bounding box sizes (bounds) of an object rather than creating the bounding box of the object equal to its width and height.

Basic game logic/ai design

I'm currently working on my 2D game project (Java), but so far any kind of game logic or AI has been crudely implemented. For instance, say I need to randomly position a bunch of sprites a long the top of the screen, I'd be using the Random class to do this. I'd simply use Random.nextInt( size of x axis on which to spawn ); Although this does work I'd be interested to hear how I should really be going about this kind of thing.
As a second scenario (this is why I put AI in the title, although it's not really AI), say I want to have my characters randomly blink in a life-like fashion. What I'd do here is use the Random class to calculate a % (say 20% chance) of blinking and call it every second.
Any suggestions on how I should really be going about this would be greatly appreciated.
Google for a paper titled "Steering Behaviors" by Craig Reynolds. It address just this and you'll find great ideas to start with specifically some nice ideas for giving groups of sprites the appearance of 'intelligent' movement. The key for him in his different behaviors, i.e. flocking, etc. is making properties of any given sprite dependent on those of some other sprite. You could even go so far as to say, like -- any given sprite will blink only if it's two neighbors just have blinked. Something or other along those lines.
Hope this helps!
Are you using an OOP (Object-Oriented approach)? If not, you should definitely look into it. It's really simple with java and can speed up your development time and neaten your code.
I would make a sprite class, and give them a function, say actionSpawn, or actionMove (I like to start my "action" functions with the word action so they are easily identifiable). In this function you would encapsulate the Random.nextInt function, to set the sprite's x and/or y position.
You could use the same approach to make them blink.

Jumping and standing on objects in 2D game

Im making small 2D game and i would like how should, theoretically work jumping and standing on objects.
For jumping should there be some kind of gravity?Should i use collision detection?
Good example of what i want to undestand is jumping in Mario.
I think for jumping it would be best to create some simple model which use "game time". I would personally create some physical model based on gravity, but you can go for whatever you want.
If you want your character to "stand" on object, you will have to create some collision detection. Good start is to approximate object by one or more circles (or lines) compute collision of them and in case the approximation collides determine the final state by some more precise method.
In terms of should there be gravity and collision detection - personally for such a thing I'd say yes and yes! It doesn't have to be complicated but once you have those things in place the pseudocode for the "main loop" becomes relatively simple:
if not colliding with object underneath, apply gravity
if user presses jump key and not colliding with surface //i.e. if we're in the air already, don't jump again
apply upward velocity
That is of course oversimplified and there are other corner cases to deal with (like making sure when you're coming down after jumping, you don't end up embedded or potentially going through the floor.
You might want to take a look at Greenfoot which handles a lot of things like all the Java2d stuff, collision detection etc. for you. Even if you don't stick with it it'd be a good platform for building a prototype or playing around with the ideas talked about.
Standing on objects implies collision detection, you can approximate the characters and the environment objects with primitive shapes (rectangles, circles etc.).
To check if to shapes are colliding, check axis one by one (X and Y axis in your case). Here is an explanation of the "separating axis theorem" (Section 1).

Categories

Resources