# Phaser/P2 troubles

## Recommended Posts

Hi!

I'm using p2 physics to implement a platformer.

One of the things I want to do is latch the player onto the (polygonal) path when he's not jumping or falling, and basically treat it like a straight horizontal in terms of movement. I have successfully implemented a system to get the current angle of the ground and move the player tangentially to it, but I'm having trouble with the player's behavior when he lands.

I would like to avoid him bouncing back upon impact, so I set restitution to 0. Still, there is some bounce because the shapes overlap on impact and are only separated in the next step. I played around with the relaxation parameter, but at 0 the player shoots out of the shape, and at higher values than 1 he gets stuck in the shape for a little while and slowly eases out of it. So I'm keeping it at 1.

I added a long, narrow rectangular p2 body to my player, which I orient in his current movement direction (so that if he's falling down, it's pointing down, when he's walking left, it points left, etc). I use the collisions this body provides to find the shapes that the player would impact with if he continued to move in his current direction. I then check the distance of my player's body to these shapes, and I want to limit the velocity at which he goes precisely so that instead of overlapping the shape on impact, he comes to rest flush against the ground/obstacle/ceiling in question.

And there's my problem: I cannot seem to set the right values at the right time. Here are two consecutive frames of an impact:

Fig.1                                                                     Fig. 2

The orange line is a record of my player's position, the red line is the narrow rectangle feeler. As you can see, the player dips into the ground pretty severely there upon impact. The green line is drawn between the point on the player's circular p2 body that was closest to the impact surface BEFORE the impact (in Fig.1), and the point where it would impact if I managed to do it right (i.e., the shortest vector between the player and the ground). The black line is drawn from the aforementioned hull point into the direction of movement. I don't know why it only drew those after it already had impacted - the data is available in the update function while the pre-impact frame is still visible, and I do set the velocity so that it SHOULD not overlap in the next frame. And yet it does.

Do you have an idea why that should be?

##### Share on other sites

I *think* I've got it figured out now - my forecast of the next position wasn't looking far enough ahead, and I was changing the velocity at a point where it was already too late – in presolve, when it should have been in state.update. At least that's what I think was happening

The whole "when does what move and what else does it influence" issue is still not very clear in my head. I now manipulate the p2 body's data directly and not through the Phaser body proxy – that seems to work, but I am really unsure when exactly the stuff in the Phaser body is updated, if at all, and which of the settings there would overwrite what's in the p2 body?