phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.11k stars 7.09k forks source link

Area of collision is bigger than it should be #896

Closed pietrofxq closed 10 years ago

pietrofxq commented 10 years ago

I just started learning Phaser, and while making through the begginer's tutorial I came across this: http://i.imgur.com/xD1Nu4W.png

Am i missing something?

ghost commented 10 years ago

@pietrofxq collision It's because of the image's sprite's real size.

Now i don't know anything about phaser, but maybe it has some sort of collision management system, like array points. If not, you should try to make an invisible sprite (a rectangle) more narrow than the image itself, and put the image on top for it to follow the invisible sprite. So the collision will affect the rectangle only.

The invisible rectangle is the yellow one: collision2

pietrofxq commented 10 years ago

I will give it a try, but is really weird because I checked the image in Fireworks and there is no extra space

hilts-vaughan commented 10 years ago

No need for invisible sprites. You can set the body size.

On Wed, Jun 11, 2014 at 9:29 AM, Pietro Coelho notifications@github.com wrote:

I will give it a try, but is really weird because I checked the image in Fireworks and there is no extra space

— Reply to this email directly or view it on GitHub https://github.com/photonstorm/phaser/issues/896#issuecomment-45740534.

casensiom commented 10 years ago

This is not an issue, you need to tweak the sprite, otherwise it will take the full size as a collisión body, even the transparent pixels.

you need to do something similar to this:

    game.physics.enable(sprite, Phaser.Physics.ARCADE);
    sprite.body.setSize(boundWidth, boundHeight, boundOffsetX, boundOffsetY);
    sprite.body.allowGravity = true;
    ...

so in your example maybe you want something like this:

    var boundWidth   = sprite.width * 0.5;
    var boundHeight  = sprite.height;
    var boundOffsetX = sprite.width * 0.25; 
    var boundOffsetY = 0;

You can always override the render method and add the body to view his dimensions:

MyGame.prototype.render = function() {
    game.debug.body(sprite);
}

I think the forums are a better place to ask for help understanding the framework: http://www.html5gamedevs.com/forum/14-phaser/