Tom Atom

[Phaser] Brick Stacker

5 posts in this topic

Nice one, trying to build a game like this myself but I'm still struggling with the p2 :))

I was wondering how do you check the balance of the brick tower?

Share this post


Link to post
Share on other sites

@NumbOfAthma - problem of physics engines is in how they work. If you have two bricks - one on top of other, then gravity sinks upper brick into bottom one. Physics engin then solves this penetration and repels birck from each other to restore position befeore they started to penetrate. But it can happen, that another new penetrations may appear (with floor, other bricks on side, ...) For this physics engines usually have several iterations for solving positions per frame.
 Problem is, that with pile of bricks you have really lot of interactions betwwen them. With 5-6 bricks, that should stay still, usuall number of iterations per frame is not enough. Bricks start to strangely slide on top of each other and so on. When bricks are falling and should bounce from each other, then everything is OK, but not when it should be static and stable... so I had to come with another solution...

 Fortunatelly, I drink a lot of tea! It has no impact on my skills, but I have lot of empty boxes, that look like brick :) Simply, when will such brick fall off your table? When more than half of it is over edge. If brick above is too much left over edge or too much right (see first row on picture) it will fall. Then I used boxes do simulate more complex situations and I found, that for game it would be enough to take brick N and: take all bricks above current brick and sum their parts to the left and to the right from edge of brick N. See second row. On series of pictures on right you can see increasing tower one by on. In step 1 and 2 brick is still OK, because more of the mass is still above bottom brick. When 3rd brick is dropped, mass on right is higher then on left and all three bricks will fall. I do this check for every brick in pile - always suming all bricks above it as described, because point of break can be at different places.

Bricks.png.47d41c7bb3d27167c55cb054699194e5.png

 This is fake physics, but it looks good. When I find point of instability, I rotate upper part of tower around it by 5-10 degrees and then I switch bricks that should fall into P2 objects to just let them fall nicely. Because it is already rotated and gravity continues fall immediatelly, it does not suffer from looking like elastic soft bodies (which would if it was not rotated ... because of interactions - iterations problem above).

 Next thing: only visible part of tower is calculated - it would be unfair to break it two or three screens below display when player can not see it. Tower below screen is just recorded - no actual bricks are there. When game ends and camera scrolls down, sprites for bricks are placed at correct positions according to recorded data.

NumbOfAthma and old_gamer_adam like this

Share this post


Link to post
Share on other sites

@Tom Atom Duuuuudee! Thank you very much for taking your time to write such a post! I really appreciated it!

I'll give it a try and let you know how it works!

Thank you again! You are awesome!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.