Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 25 Feb 2011
Offline Last Active Apr 09 2015 12:39 PM

#5047568 How to update a lot of random tiles in a 2D array?

Posted by on 28 March 2013 - 03:20 AM

In the tile based game I am making I sometimes need to update several tiles at once. Since I keep my world in a 2D tile array, I can't seem to find a good way to update the specific tiles I want updated.


An example:

When grass grows, I periodically need to check if it is time for it to grow. Since there are a lot of dirt tiles, there will be a lot of dirt tiles to go through. When searching for dirt tiles that needs to be updated, I search from top to bottom, until I hit a dirt tile, which needs to be updated. The part that gets taxing, is that since I don't necessarily know which tiles need updating, I have to iterate through a lot of them (Or at least all withing my Update Radius). Since the player can change the world, the tiles to be updated can change very quickly. I have considered adding the reference variables to a list, and then update the tiles on the list. But since the list can become quite long fast, I'm worried that I might be spending too many resources.


Solutions I've thought about:

- The first one is more or less just brute force. Periodically I iterate through the tiles within my Update radius, and update them. I've done this  and sometimes this causes a lag for a second or so when the updates are applied- So this is not a good solution.

- Every time I draw my world, I iterate through a lot of tiles (those within my Draw radius). I was thinking about simply doing the tile updates  there, since I already have an iteration going. I am pretty sure this is bad practice, and I know that Draw is potentially called fewer times than update, so I dropped the idea.

- The list idea I mentioned earlier, where I add tiles that needs to be updated to a list, and iterate through that list periodically. The problems I see with this method is that I still need to figure out which tiles to put on the list, which means I need to check for that every time the world changes somewhere. Another problem I see is if the list is getting too big. I don't think memory will be a big issue, but updating potentially thousands of tiles at once, will cause huge slowdowns. A fix could be to look at the list more often, and only update small parts of it- Like a queue system.


So my question is: What would be a good way to update "random" tiles in a 2D array?


Thanks for reading smile.png

#5009030 2D Platformer - Shooting in the direction of the mouse cursor

Posted by on 10 December 2012 - 03:52 AM

I'm back, and with success.
So to get the snowball to "shoot" towards the mouse pointer, I do the following:

First I convert the mouse position into worldspace position. I then create a Vector2 for the direction, and give it the value of the convertedMousePosition minus the spawn point for the snowball.
Vector2 direction = mousepos - snowballSpawnPos;

After that I normalize the direction vector, which gives a vector with a length of one, which is then used to determine the direction to move. Then simply multiply direction with a float with the speed of the snowball. If you multiplay with gametime.ElapsedGameTime.TotalSeconds the speed will be the number of units it moves pr second.

So basically it would be something like this:
Vector2 direction = mousePos - snowballSpawnPos;
float speed = 200.0f;
if(direction != Vector2.Zero) //If direction is 0,0 normalize would throw a divide by zero exception.

//Update code
public void Update(GameTime gameTime)
	 Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;

I found this solution on StackExchange - here's the link: http://gamedev.stack...e-mouse-pointer

Check out the link if my explanation is to vague Posted Image

Thanks for your help guys!

#5008893 2D Platformer - Shooting in the direction of the mouse cursor

Posted by on 09 December 2012 - 04:02 PM

I've been working quite a while on my 2D tile based side scroller, and it's been going smooth for quite a while, but now I've run into a problem that I'm hoping someone could help me with.

I've just implemented my first throwing weapon- a snowball. Now I want the player to be able to throw in the direction of the mouse curser, instead of just throwing to either sides. The problem is I can't seem to grasp how to do this the right way.
When I create a snowball I give it a starting position and a velocity (a Vector2). I add the velocity to the position, and update the velocity with gravity and such accordingly.
My current solutions, that I haven't had good results with are the following:
- Convert the mouse position into tile coordinates, and use the distance in tiles on the y axis to determine the up- or downwards speed in worldspace. No changes to the X velocity,
- Do some math with radians or degrees, to get the angle between the player and the cursor. This solution is one I haven't tested, since I have a hard time figuring out the math

My last suggestion is just trial and error and simply hardcode some values that I find works out, based on the mouse position, but this is really not the way to do it (unless there really aren't any other way, which I doubt).

I am going to sneak in another quick question about weather.
I've recently added rain and snow to my game. Snow will slowly gather on the ground and be "harvestable". Now I want the snow to smelt again, when it gets warmer, but I'm looking for a smart way of doing this. Currently I just randomly pick a spot on the x-axis in tile terms within a set distance of the player, and then I iterate through the Y axis to check for snow, and remove it. I do this to add a sense of smelting over time, instead of the snow just dissapering in a predictable manner. Is my approach good enough, or are there better ideas?

If you have a hard time picturing what I mean, I have a video you can look at to see the snow in effect. If you don't want to see the whole video go to 1.28 to see where the snow starts, or go to 2:05 for the harvest part.

So to sum it up:
- How do I shoot/throw an object in the direction of the mouse in a 2D tile based platformer?
- (Not so important) Are there a smart way to make the snow smelt over time, or is my method good enough?

Any help is greatly appreciated. - Thanks in advance :-)

#4999926 Affordable SFX Libraries

Posted by on 11 November 2012 - 09:20 AM

This is really amazing. I will definitely buy some of your libraries. You are gonna save me a ton of time in the future searching for affordable sound effects with a decent quality. You already got a lot of genres covered here, I am truly amazed Posted Image

#4997291 How to check if a unit is doing the same thing over and over?

Posted by on 04 November 2012 - 02:57 PM

On my journey through creating myself a 2D scroller, in the Terraria genre, I've started updating my basic AI a little. Since I've decided against pathfinding, I am currently using a very basic, "move towards player position" for hostile mobs. I've spiced it up with some basic abilities to jump over obstacles, within a certain height, and also jumping over holes if they are too deep to jump into, and aren't too wide.

This approach does end up with many situations where the mob will get stuck in doing the same thing like trying to jump over an obstacle, but because there's some tiles above it, it can't jump over it and stuff like that. It will quickly turn into a lot of code if I have to take everything into account, so instead I decided to look at how Terraria did it. So I took a look at how Terraria zombies work (haven't tested on other mobs) and, apart from being a bit more smooth in their movement, there are certain situations, like mine, where they just keep doing the same thing over and over. In Terraria however, the zombies will, after a short while, simply leave. They turn around and walk away, until they're offscreen or the player attacks them.
I would like to mimic this behavior. But I am trying to think up a smart way to do this. So far I've considered the following:
- Over the course of a few seconds, check a few times if the mobs position is the same, or within a very short range (for cases where the mob moves back and forth because it's on top of you, but with solid tiles between you).
- Make a small list of the mobs movement and compare all the elements, and check if the same position is passed multiple times.

So my question is:
Are there any better ways than the ones I've described above? - and if so,what should I do? Posted Image

Thank you for reading.

#4993951 Help me chose a performant 2D framework / engine for a tile based game

Posted by on 25 October 2012 - 04:35 PM

Thanks. Phantobra is the project I've learned most of what I know now. It's like a learning by doing project, which has grown to be much more than I initially thougth possible Posted Image

Is Microsoft abandoning C#?
In short - I don't think so. I think XNA will continue. I've read around on the internet and although there's a lot of people who believes it won't, I found an article with a bit more optimistic view, and he does a good job explaining why.. This article is from 2011 though, but check it out:

Good luck with your project - I'm curious to know what your choice will be, so drop me a line, once you do Posted Image

#4968236 Best language to make 2d games

Posted by on 10 August 2012 - 05:05 PM

Some months ago I decided to create a 2D game based on Terraria - http://en.wikipedia.org/wiki/Terraria

I actually started out with Unity (3D engine for C# and Javascript). But doing 2D in a 3D engine felt like a complete waste.
So. I decided to give XNA a try, and after a few online tutorials I decided to buy a book about XNA 4.0(my very first programming book). After working through half of it (the part that covered 2D) I decided that I'd give it a try in XNA. And I've never been happier with my choice.

During the last 2-3 months I've learned a lot. XNA does a lot of the hard stuff for you, while still giving you the feel that you are actually creating something.
I'll link to a video below that I've made recently showing the progress of my game. It might not be much for the pros out there, but for me, who's mostly self taught, I am quite proud of what I've accomplished so far. - My point is - I'd go for XNA since it's a nice first step into game developing (I'm still taking my first steps) and I'm pretty sure that if I had jumped straight into c++, I wouldn't have gotten this far, and chances are I would've lost my motivation on the way.

The Video: