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

Resize a center anchor sprite result on collision misposition


diofinus
 Share

Recommended Posts

Hi all,

 

I'm new to Phaser and I want to ask about something that became a problem for me and sorry for my bad english.

So I have shape sprite that I add child to the background sprite. I want to make the background fit the screen size and it working. I also make the shape sprite anchored to middle (0.5, 0.0) so whatever the shape the position still in the middle. The problem came when I try to create collision. If the size is original size then the collision is correct but when I resize the background the collider start to not fit the sprites. I search for this and found out that if I comment out the anchor then the collider is fit to the sprite again. I also already do setting on body size.

Here is the list of code I use

//scaling background
background.scale.set(game.width, game.height);

//add physics
game.physics.arcade.enable(shape_sprite)

//anchor sprite
shape_sprite.anchor.set(0.5, 0);

//reset shape_sprite collision
shape_sprite.body.setSize(
	shape_sprite.width*shape_sprite.parent.scale.x,
	shape_sprite.height*shape_sprite.parent.scale.y,
	shape_sprite.width*shape_sprite.parent.scale.x/2,
	0
);

I think maybe I'm doing it wrong.

without anchor:

e6a8c3c5a8de4218946a41a95395ea7c.png 

with anchor:

6a2f3c779a8d4329ac455724df562529.png

Link to comment
Share on other sites

Can you align the shape sprite the way you want without making it a child of the background? That will be simpler for physics. You can use, e.g.,

shape.alignIn(background, Phaser.BOTTOM_CENTER);

Otherwise you can try

var anchor = shape_sprite.anchor;
var frame  = shape_sprite.texture.frame;
var scale  = shape_sprite.parent.scale;

shape_sprite.body.setSize(
	frame.width  * scale.x,
	frame.height * scale.y,
	-anchor.x    * scale.x, // ??
	-anchor.y    * scale.y  // ??
);

 

Link to comment
Share on other sites

After several of hours and analyze the difference pattern I found the answer for this, but somehow quite strange for me.

shape_sprite.body.setSize(
	shape_sprite.width*shape_sprite.parent.scale.x,
	shape_sprite.height*shape_sprite.parent.scale.y,
	shape_sprite.width*(1-shape_sprite.parent.scale.x)/2,
	0
);

I don't understand why I need to subtract but it works just fine.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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