Tuesday, October 18, 2011

My brother is awesome.

I received an email from my younger brother today.




I love it.

I've been working on liquids tonight.  It seems to work pretty well so I'll be testing that for a few days.  I've also started preparing for the kickstarter release.  Overall it's been a very busy week and these pictures made it all the better! :]


3 comments:

paul_nicholls said...

Hi Roy,
I was wondering if perhaps you could share a bit about how you did your water in the game? It looks very cool, and I am hoping to add something similar to a boulder-dash inspired game I am writing (see here for a video, pick the HD option!):

http://www.youtube.com/watch?v=Ho_g9gB1KGM"

My email is:
paulfnicholls AT gmail DOT com

cheers,
Paul

Roy said...

Sorry I missed your comment Paul.

I have an object in the game that represents each block of water. I then have a Dictionary<Vector2,waterObject> that stores all of the water in my game.

The Vector2 key is rounded to the nearest block position (each block is placed in increments of 32x32). Every 10 frames (the game is locked to 60fps), I go through all of the water within range and run an update() method on them.

The update method on each water block first checks below. If there's no land below, I drain() the current block into the water below me. If that block is filled, I move onto the next step. If the block below is not full, I keep draining into it until it's full or the source block is empty.

The next water step is checking to the left of the water block. It will drain() in that direction if it can (it can drain in that direction if the there is space for water). I then do the same to the right of the block.

The left/right drain direction is based off of where the water source came from. If the water is coming from the left side, the water block drains to the right first. If it's coming from the right, it drains to the left first. If you don't do this, you will have blocks constantly draining water back and forth between eachother.

The nice thing about dictionaries is that for key based lookup, they are O(1). Very fast. I only update water within a certain range, so I'm not moving mountains of water at a time.

I also have a threshold set on the update function. The max water level is 100. If the water level for a given block is < 10, I don't drain any water at all. I also don't drain water if the block that I want to drain to has a higher amount of water than the block I'm draining from. By having that threshold, I'm not constantly draining water back and forth and wasting CPU resources.

I hope that answers your questions. It might not be the most efficient solution, but it seems to work well and fast. It also lets me add splash effects and such to the game.

If you'd like, you can see it for yourself. Sign up on my forums at http://www.astrominer.net/f and I'll send you a link to my game to try out.

paul_nicholls said...

Thanks for the explanation Roy, and no worries...you didn't miss my comment for long anyway :)

I will hopefully be able to get something similar going in my game.