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.18k stars 7.1k forks source link

issues with physics (body separation) #219

Closed jcd-as closed 10 years ago

jcd-as commented 11 years ago

physics interactions between sprites seems to have some issues, particularly when combined with tilemaps - sprites can become embedded in "solid" tiles, pass through each other etc

this is most easily illustrated by adding another sprite to the 'starstruck' sample. e.g.:

to the 'preload' function, add the line

game.load.spritesheet('ball', 'assets/sprites/blue_ball.png', 17, 17);

add a var named 'ball' to the file,

then to the create function, add:

ball = game.add.sprite( 64, 32, 'ball' );
ball.body.setSize( 17, 17, 0, 0 );
ball.body.gravity.y = 10;

and finally to the update function add collision-handling for the new sprite:

game.physics.collide( ball, layer );
game.physics.collide( player, ball );

'kick' the ball around a bit, particularly into corners and such, and you'll notice that you can sometimes force the ball or the player into walls, floors, each other, etc. (tweaking the bounce, drag and gravity of the sprites can make it easier to get some of these to occur, but it's not too difficult. getting the ball down into the bottom left area and pushing it around / jumping on it usually does the trick).

I've been trying to track this down, but haven't had much luck yet - it's quite a pain to debug. hoping that those with more intimate knowledge of the update, collision and separation logic may have some ideas.

(note that these occur on a laptop that keeps a steady framerate of about 60 fps, but things seem to get much worse when the framerate gets lower/choppier, as on mobile devices)

photonstorm commented 10 years ago

I suspect these are all delta timer related, it's the only thing that makes sense re: object tunnelling.

jcd-as commented 10 years ago

meaning time.physicsElapsed time? I assumed this was the issue at first, but I hard-coded it to 0.166 in Time.js and the issues still reproduce

jcd-as commented 10 years ago

the sprite->tile separation routines are using hullX and hullY rects to determine overlap and do separation, but the hullX and hullY rects weren't updated when sprite->sprite separation occurred. so if a sprite pushed another sprite into a tile, the second sprite wouldn't separate properly from the tile and could end up "inside" the tilemap, where all kinds of weird things would then happen.

framerate issues seem to have been a red herring (or possibly a separate issue, since I still see odd things happen with collision/tilemaps at low framerates)

there could still be some odd ordering issues - I had to make sure that the order I check collisions in was sprites<->sprites first and then sprites<->tilemaps

photonstorm commented 10 years ago

Now that makes a lot more sense, thanks for the digging. I will play with this today. Collision order shouldn't be an issue once this part works I would hope. Also we've just started work on integrating p2.js and it's going really well, so I'm looking forward to offering that as a solution around version 1.1.4 - 5.