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

Problem with body when using world.scale


Martiny
 Share

Recommended Posts

I'm trying to make a zoom feature and when I try using this.world.scale, everything scales fine, except the collision body that keeps the same size. I wonder if this is a bug or I'm using the scale property wrong.

 

var newScale = 0.5;
this.world.scale.setTo(newScale,newScale);

To fix the body not matching the sprite, I made this, but I believe if there is a better way of doing it. This doesn't work if the sprite has an anchor different than 0, I have no idea how to put anchors into account when calculating the position. Maybe someone could help?

 

this.gameObjects.forEach(function(object) {
			object.body.setSize(object.body.width * newScale, object.body.height * newScale);
		}, this);

I uploaded an image that shows the body not changing size.

 

If someone needs more code/clarification, just ask.

problem.png

Link to comment
Share on other sites

This is normal (but maybe surprising). Sprite bodies are sized by the sprite's own scale without considering the scale of any ancestors.

For "pure" rendering scaling you can use

game.renderer.resize( unscaledGameWidth / scale, unscaledGameHeight / scale );

I think you can resize bodies with

object.body.setSize(
	object.body.width    * newScale,
	object.body.height   * newScale
	object.body.offset.x * newScale
	object.body.offset.y * newScale
	);

Be careful with that since I think those scales will compound if you call it repeatedly with the same value.

Based on the sprite texture it's (I hope)

var width  = object.texture.frame.width;
var height = object.texture.frame.height;

object.body.setSize(
	width                    * newScale,
	height                   * newScale
	width  * object.anchor.x * newScale
	height * object.anchor.y * newScale
	);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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