Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-10-2012, 11:17 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question How to handle falling blocks?

Let's say I have a match 3 type game where one or more blocks are removed from a grid.
And then the remaining blocks would fall down, filling in the gaps.

How would you handle the falling block animation?

Would you move them using a UIView animation, or would you use some kind of game loop,
and handle the movement yourself?

Thanks for any suggestions.
samurle is offline   Reply With Quote
Old 01-29-2012, 07:39 PM   #2 (permalink)
Registered Member
 
Memir's Avatar
 
Join Date: Jan 2012
Posts: 11
Memir is on a distinguished road
Default

I know little about UIView stuff. Instead of making things complex, I just treat the iPhone like any other computer when making games, so whatever you'd do if you were making a PC game or a SNES, Gameboy, N64 etc. game, do the same. - Although for certain games/apps, the iOS way of doing things may be quicker.

I've actually not made a block-drop/tetris game before. But the way I'd tackle this, is just to render the X by Y tetris-container like a Tilemap, every frame. Tiles that don't move, but animate can be dealt with easily as we know. However, dealing with gaps, and allowing the tiles above to fall. I think the best approach would be to give every square a height value (e.g. from 0 to 32 pixels) which dictates how to render them (you'd render the tilemap as columns in ascending height order). By default all tiles have a height value of 32. So the process is:

Column 0: Row 0: Draw Tile.index (if > 0). Y-=Tile.height;
Column 0: Row 1: Draw Tile.index (if > 0). Y-=Tile.height;
.
.
.
Column 1: Row 0: Draw Tile.index (if > 0). Y-=Tile.height;

When a tile has been removed, and thus that square now 'dead'. The height value should be decreased by K every frame until it is 0 (where K is your pixels/frame velocity). (note: only do this to the lowest dead tile in each column at any one time). Keep doing this until all of the dead tiles have their heights decreased to 0. Once this happens, your falling animation is complete.

To clean up, just scan up these columns, and regenerate your new tilemap (devoid of those dead squares). So everything looks and is logically normal again.

When this falling animation is going on, usually the user has to wait and watch.

Bit hard to describe without a drawing, so I hope that kinda made sense.

Apologies if you already knew how to do it this way, and was just asking if there was a "UI" way. I figured I'd just put my thoughts here in case others were wondering.

Last edited by Memir; 01-29-2012 at 07:43 PM.
Memir is offline   Reply With Quote
Old 01-30-2012, 01:44 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

Quote:
Originally Posted by Memir View Post
I know little about UIView stuff. Instead of making things complex, I just treat the iPhone like any other computer when making games, so whatever you'd do if you were making a PC game or a SNES, Gameboy, N64 etc. game, do the same. - Although for certain games/apps, the iOS way of doing things may be quicker.

I've actually not made a block-drop/tetris game before. But the way I'd tackle this, is just to render the X by Y tetris-container like a Tilemap, every frame. Tiles that don't move, but animate can be dealt with easily as we know. However, dealing with gaps, and allowing the tiles above to fall. I think the best approach would be to give every square a height value (e.g. from 0 to 32 pixels) which dictates how to render them (you'd render the tilemap as columns in ascending height order). By default all tiles have a height value of 32. So the process is:

Column 0: Row 0: Draw Tile.index (if > 0). Y-=Tile.height;
Column 0: Row 1: Draw Tile.index (if > 0). Y-=Tile.height;
.
.
.
Column 1: Row 0: Draw Tile.index (if > 0). Y-=Tile.height;

When a tile has been removed, and thus that square now 'dead'. The height value should be decreased by K every frame until it is 0 (where K is your pixels/frame velocity). (note: only do this to the lowest dead tile in each column at any one time). Keep doing this until all of the dead tiles have their heights decreased to 0. Once this happens, your falling animation is complete.

To clean up, just scan up these columns, and regenerate your new tilemap (devoid of those dead squares). So everything looks and is logically normal again.

When this falling animation is going on, usually the user has to wait and watch.

Bit hard to describe without a drawing, so I hope that kinda made sense.

Apologies if you already knew how to do it this way, and was just asking if there was a "UI" way. I figured I'd just put my thoughts here in case others were wondering.
Thanks, but, I'm still a little confused as how you're handling falling blocks.

Are you referring to the block's screen position when you say "height value" ?
A block's height is just its dimensions?
samurle is offline   Reply With Quote
Old 01-30-2012, 02:07 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

If I understand you, tetris blocks move in discrete amounts: e.g. move-stop-move-etc...
This type of stop-go motion is not what I'm trying to do.

The type of falling that I'm interested in is free-falling blocks. The block doesn't
stop falling until it hits something.
samurle is offline   Reply With Quote
Old 01-30-2012, 04:11 AM   #5 (permalink)
Registered Member
 
Memir's Avatar
 
Join Date: Jan 2012
Posts: 11
Memir is on a distinguished road
Default

Quote:
Originally Posted by samurle View Post
If I understand you, tetris blocks move in discrete amounts: e.g. move-stop-move-etc...
This type of stop-go motion is not what I'm trying to do.

The type of falling that I'm interested in is free-falling blocks. The block doesn't
stop falling until it hits something.
Sorry, I was referring to dealing with the falling part, once you've dropped your block/removed blocks, and want the blocks above to fall down in the space created.

If you're talking about the smooth falling of the 3-block piece that the user controls, then that's easy. It's separate to the grid tilemap. It's just 3 sprites, you can treat that in pixels coords.

So.

State 1: "User controlling 3-block piece, or 4-block piece if this were tetris".

In this state, all of the settled blocks are just rendered as part of the standard gridded tile map, nothing special there.

The 3-block piece is rendered separately as a sprite overlay, in pixel resolution. It's subject to linear gravity (e.g. Y+=dY, where dY is your pixels/frame linear gravity).

Also when the user presses left or right, it is told to move Left say 32 pixels, or right 32 pixels respectively (which it'll do at a rate of X+=dX, this can either work linearly, or based on some ease-in/out etc. equation for effect).

As for collisions with the tilemap. You can create a tilemap solid testing function, something like this:

b1 TileMap::IsSolid(s32 x, s32 y)
{
if(x<0 || x>=COLS) return true;
if(y<0 || y>=ROWS) return true;

s32 tx = x / TILE_WIDTH;
s32 ty = y / TILE_HEIGHT;
return m_pTileMap[ty*COLS+tx] > 0;
}

You'd call the above numerous times to detect when how the perimeter of your 3-block piece interacts with the tile-map.

State 2:

Once your 3-block piece has come to rest for more than N Frames. Remove 3-block piece, and commit the values to the Tilemap, and evaluate how this affects the tilemap (i.e. if it causes blocks to disappear)

State 3:

Now any gaps need to be contracted (a method to do this, I described in my first post).

I'll see if I can do some drawings in photoshop to illustrate some of this. But there are many solutions for this. Some more elegant than others.
Memir is offline   Reply With Quote
Old 01-30-2012, 05:21 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

Thanks, I've trying to do something like what you suggested:
Quote:
b1 TileMap::IsSolid(s32 x, s32 y)
{
if(x<0 || x>=COLS) return true;
if(y<0 || y>=ROWS) return true;

s32 tx = x / TILE_WIDTH;
s32 ty = y / TILE_HEIGHT;
return m_pTileMap[ty*COLS+tx] > 0;
}
Where I'm snapping the block's screen position to a grid index. But, I have this odd case where
two falling blocks might be fighting over the same empty grid position. One of the block succeeds
and the other, well, disappears.

I might have to use a physics library to get this straighten out. I was hoping there was a simple
way to deal with free-falling block collisions or conflicts.
samurle is offline   Reply With Quote
Old 02-21-2012, 07:21 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 43
iphonesdkdevnewbie is on a distinguished road
Default

I am very interested in doing something along these lines but not with free falling objects. Rather I want the user to be able to drag objects around. And if Object1 for example touches object2 then it either spawns another object or deletes it. I tried using GameSalad but you have to program every event.
iphonesdkdevnewbie is offline   Reply With Quote
Old 02-21-2012, 09:08 AM   #8 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Default

Quote:
Originally Posted by iphonesdkdevnewbie View Post
I am very interested in doing something along these lines but not with free falling objects. Rather I want the user to be able to drag objects around. And if Object1 for example touches object2 then it either spawns another object or deletes it. I tried using GameSalad but you have to program every event.
If you're dealing with collisions, then a simple answer would to use a 2D physics library.
Sorry, I don't know what GameSalad is or what it provides.

My problem deals with falling blocks. Although it seems simple enough, a 2D physics library
still might be necessary to handle even the simplest collisions. I haven't come across any
non-physics solutions yet.
samurle is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, blocks, falling

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 387
8 members and 379 guests
apatsufas, comicool, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:05 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0