Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Scaling Box2D sprites and world?


jcharry
 Share

Recommended Posts

Hello!

I'm new to Phaser and Box2D, but I've looked through as many posts as I could find on scaling box2d and am still struggling to get it working well.  I'm trying to scale a bunch of sprites, and on each scale step increase the worldsize as well so that I can reset box2d world bounds.  I've gotten the world scaling and the sprites scaling, but the physics bodies stay behind.

It looks like this: the sprites are beyond the bounds of the world and they are offset from their box2d bodies.589281bb00972_ScreenShot2017-02-01at7_43_01PM.png.7411934c97be5e3a5da7a1744af26de6.png

Here's what I've tried - using body.setRectangle() to scale the Box2D rectangle as well.  I've also noticed that the sprites are scaling faster than the world and are leaving the world bounds.

I've been banging my head against the wall for a while and was hoping someone had some wisdom to share.

        if (this.cursors.up.isDown) {
            // An internal function that I use to track world scale.  This is working fine
            this.scaleWorld(1);

            // Scale all the bodies in the world inside a group called stageGroup
            this.stageGroup.scale.set(this.worldScale);

            // Set the world scale & reset bounds
            this.game.world.scale.setTo(this.worldScale);
            this.game.world.setBounds(0, 0, this.worldSize.width * this.worldScale, this.worldSize.height * this.worldScale);

            // Reset box2d world bounds     
            this.game.physics.box2d.setBoundsToWorld();

            // For all sprites in the sprite group, scale them
            this.stageGroup.children.forEach(child => {
                // scale x and y
                child.position.x = child.position.x * this.worldScale;
                child.position.y = child.position.y * this.worldScale;
  
                // This is what I've tried but it's not working
                child.body.setRectangle(child.initialSize* this.worldScale, child.initialSize * this.worldScale, 0, 0, 0);
            });
        }

 

Thanks!!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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