Jump to content

Too many stacked sprites falling through ground


ckskylight
 Share

Recommended Posts

Hi All!

 

I'm new to Phaser and I've been trying to figure out how the physics engine works by modifying the tutorial code. In the game I want to make, I want the user to be able to build walls by stacking blocks on top of one another, and I want the walls to be demolished by projectiles, so I need to use the physics engine. However, I discovered that stacking too many blocks makes the blocks fall through the ground because they are "too heavy". Is there a way to get rid of this problem? Below are two screenshots: the first one is as the blocks start to fall through and the second is after they've fallen through and are stable again.

 

post-6118-0-96659000-1389161863.png

 

post-6118-0-47907700-1389161864.png

 

 

Link to comment
Share on other sites

Here is the relevant code:

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {    game.load.image('sky', 'assets/sky.png');    game.load.image('ground', 'assets/platform.png');    game.load.image('star', 'assets/star.png');    game.load.spritesheet('dude', 'assets/dude.png', 32, 48);    game.load.image('diamond', 'assets/diamond.png');    game.load.image('block', 'assets/firstaid.png');}var platforms;var cursors;    var blocks;function create() {    //  A simple background for our game    game.add.sprite(0, 0, 'sky');    //  The platforms group contains the ground and the 2 ledges we can jump on    platforms = game.add.group();    // Here we create the ground.    var ground = platforms.create(0, game.world.height - 64, 'ground');    //  Scale it to fit the width of the game (the original sprite is 400x32 in size)    ground.scale.setTo(2, 2);    //  This stops it from falling away when you jump on it    ground.body.immovable = true;    //  Now let's create two ledges    var ledge = platforms.create(400, 400, 'ground');    ledge.body.immovable = true;    ledge = platforms.create(-150, 250, 'ground');    ledge.body.immovable = true;    //  Our controls.    cursors = game.input.keyboard.createCursorKeys();        blocks = game.add.group();        game.input.onDown.add(createBlock, this);        }function update() {            game.physics.collide(blocks, platforms);    game.physics.collide(blocks, blocks);}        function createBlock() {    var block = blocks.create(game.input.mousePointer.x, game.input.mousePointer.y, 'block');    block.body.acceleration.y = 100;    block.body.allowRotation = true;    block.body.mass = 3;    block.body.bounce.y = 0;}
Link to comment
Share on other sites

  • 4 months later...

if you reduce mass to 0 they won't go through the floor but still they will move a little bit into it. I'm trying to figure out a better solution.

 

Edit: The problem is (probably) that two collisions happen on the same time.Sadly my knowledge stops here, i can't help you further .

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...