Jump to content

P2 Woes


seiyria
 Share

Recommended Posts

Today I started converting my Arcade Physics game to P2, with the hopes of getting a lot of neat stuff. So far I've only had problems, really.

 

For reference, my code is here (most relevant chunk is GameLevel) and there's a demo here.

 

So I got myself moving around in the world all nice and such, I can jump around, and that's great. Then I went to add platforms, and that was fine. I had platforms that gleefully moved back and forth like little platforms should. Then I wanted to make platforms actually carry me with them, so I found a thread that said to add a collision group to platforms and the player, set a collision, and have a contact material with some friction so the player would be stuck on it. Sure, sounded easy enough. So I did that, and then ... the player no longer stays on the stage. Basically, my code is like this:

    @game.physics.startSystem Phaser.Physics.P2JS    @game.physics.p2.gravity.y = 1000    @game.physics.p2.restitution = 0.2    @game.physics.p2.convertTilemap @map, @tileLayer    @game.physics.p2.setBoundsToWorld yes, yes, yes, yes, no    @game.physics.p2.setImpactEvents yes    @game.world.enableBodySleeping = yes    @game.playerMaterial = @game.physics.p2.createMaterial 'player'    @game.platformMaterial = @game.physics.p2.createMaterial 'platform'    @game.physics.p2.createContactMaterial @game.playerMaterial, @game.platformMaterial, {friction: 10, restitution: 0}    @players = @game.add.group()    @game.cg =      player: @game.physics.p2.createCollisionGroup()      platform: @game.physics.p2.createCollisionGroup()    @game.physics.p2.updateBoundsCollisionGroup() # EDITED IN    @game.player = @player = new Player @game, 96, @game.world.height-96, 'player'    @players.add @player    @game.physics.p2.enable @player    @player.body.fixedRotation = yes    @player.body.setMaterial @game.playerMaterial    @player.body.setCollisionGroup @game.cg.player # POINT 1    @player.body.collides @game.cg.platform        # POINT 2

Now here's the fun thing. I fall straight through the floor (as seen in the demo). However, if I remove @player.body.setCollisionGroup (POINT 1) and @player.body.collides (POINT 2) then I get default collision. I was talking with Fishrock and we found out that my collision masks should be correct. 

 

the worldBounds has a mask of 2

the player collision group has a mask of 4

the platform collision group has a mask of 8

 

player.body.data.shapes[0].collisionGroup is set to 4

player.body.data.shapes[0].collisionMask is set to 10 (which is 2 | 8, or worldBounds | platform)

 

I've also tried flat out removing POINT 1 and then adding @player.body.data.shapes[0].collisionMask |= 1 (which makes it collide with the terrain AND world bounds correctly, except I do _not_ collide with platforms now)

 

I've also tried a bunch of other things, but I can't seem to find an obvious solution.

Link to comment
Share on other sites

It might just not be included in the code above, but are you updating the worlds collision group after creating your new ones? Like so:

    //  Create our collision groups. One for the player, one for the pandas    var playerCollisionGroup = game.physics.p2.createCollisionGroup();    var pandaCollisionGroup = game.physics.p2.createCollisionGroup();    //  This part is vital if you want the objects with their own collision groups to still collide with the world bounds    //  (which we do) - what this does is adjust the bounds to use its own collision group.    game.physics.p2.updateBoundsCollisionGroup();
Link to comment
Share on other sites

I did post a demo up above, but I was wondering something as well - how do I collide with the terrain that I generate from a tilemap? This:

 
@game.physics.p2.convertTilemap @map, @tileLayer
 
does something, but I can't figure out where the collision mask for it is. Maybe I should do that after I create and set my player to its collision group? Additionally, I stumbled across this thread.. did you ever end up coming up with a demo for that? I'm probably just doing something horribly wrong.
Link to comment
Share on other sites

for more insight in phaser p2 tilemaps created with tiled you could download and read this demo of mine :

http://www.html5gamedevs.com/topic/6556-how-to-export-from-tiled-and-integrate-into-phaser/?p=39495

 

to get moving platforms with p2 where you can actually jump through from below i opened this thread (and found a solution)

http://www.html5gamedevs.com/topic/6148-the-moving-jumpthrough-platform-sum-up-thread-help-very-much-appreciated/

 

in this thread is all about the jump through from below and land on from top problem but the moving platform is in it ( i solved it in the following thread before that)

http://www.html5gamedevs.com/topic/5175-player-not-moving-with-moving-platform/?hl=%2Bmoving+%2Bplatform

Link to comment
Share on other sites

You could change the gravityScale of your player :

player.body.data.gravityScale = 0.5;

By default it's set to one. This above will cut the gravity affected to your player by half. -1 will reverse the player gravity etc....

Wouldn't that be the same as setting my physics gravity to 1500 instead of 3000? Meaning that I don't get the full effect of my gravity keeping my player on my platforms?

Link to comment
Share on other sites

Well, yeah, except you can adjust the gravity for each body.

I'm not sure using the gravity to stick your player to the platform is a good idea.

I remember valueerror doing some work on this and having a prototype of moving platforms. It seemed to work prety well at that time.

Here is the topic:

http://www.html5gamedevs.com/topic/6148-the-moving-jumpthrough-platform-sum-up-thread-help-very-much-appreciated/

And his demo

http://test.xapient.net/phaser/ALL/moving-jumpthrough-platform.html

Link to comment
Share on other sites

I have seen those, but I don't quite get how they work. I'm not really sure of the ideal way to do this, I just know that I would rather make use of the physics engine if possible (instead of manually moving the sprite, or manually setting the sprites velocity based on the platform).

Link to comment
Share on other sites

You may have the beginning of an answer here :

 

http://schteppe.github.io/p2.js/examples/canvas/character.html

 

I haven't work on that for now, but I will soon have the need of moving platforms like you. That is where I will begin my search.

 

I would also look at surface velocity.

http://schteppe.github.io/p2.js/demos/surfaceVelocity.html

 

Maybe by setting it at the same velocity as the body will simulate the player being dragged by the platform. But I don't know if you will be able to move during that ( never tested :) )

 

Good luck

Link to comment
Share on other sites

I have looked at the former demo, but that was a few days ago, I may have to look at it again. I was told only to use surfaceVelocity for conveyor belts, which makes sense, given the behavior in that demo. I did try using it, and it didn't make a lot of sense. Thanks though! I'll keep trying to figure something out.

Link to comment
Share on other sites

just one question.. what's wrong with this demo. why not use this approach.. i use it in my game and it works very reliable.. no hacks needed. its a simple normal physicsbody.. moved by velocity.. therefore all equations (friction, collison, gravity....) working normal for the whole world..

 

 

http://test.xapient.net/phaser/ALL/moving-jumpthrough-platform-final.html

function create() {    game.physics.startSystem(Phaser.Physics.P2JS);  //start p2 engine    game.physics.p2.gravity.y = 1200;               // we need a good portion of gravity    game.physics.p2.friction=4;                     // default friction     movingplatforms = game.add.group();    // add a group for our moving platforms//vertical moving platform    platform = movingplatforms.create(200,  200, 'movingplatforms');  //add first platform to group    platform.name = 'vertical';   //name it either vertical or horizontal    game.physics.p2.enable(platform,true);  //enable physics    platform.body.setRectangle(120,30,0,0);    platform.body.kinematic=true;       // set body to kinematic (movable static body)    platform.topbounds = platform.body.y;       //set bounds for later calculations    platform.bottombounds = platform.body.y + 128;      //128 defines the distance the platform will move down    platform.velo= 100;   //define the speed for the moving platform//horizontal moving platform    platform = movingplatforms.create(400,  200, 'movingplatforms'); //add second platform to group    platform.name = 'horizontal';    game.physics.p2.enable(platform,true);  //enable physics    platform.body.setRectangle(120,30,0,0);    platform.body.kinematic=true;    platform.leftbounds = platform.body.x;  // leftbounds is the initial position    platform.rightbounds = platform.body.x + 256;   //from here move 256 pixel right    platform.velo=50;   //define the speed for the moving platform}function update() {     movingplatforms.forEach(movePlatforms,this);   //call movePlatforms function every step on all members of the movingplatforms group}function movePlatforms(platform){    if (platform.body.sprite.name == 'horizontal'){   //check for the moving direction        if (platform.body.x > platform.body.sprite.rightbounds){  platform.body.sprite.velo *= -1;} //if reached bounds reverse speed        if (platform.body.x < platform.body.sprite.leftbounds) { platform.body.sprite.velo *= -1;}  // reverse speed        platform.body.velocity.x = platform.body.sprite.velo;    } else if (platform.body.sprite.name == 'vertical'){        if (platform.body.y > platform.body.sprite.bottombounds){  platform.body.sprite.velo *= -1;}        if (platform.body.y < platform.body.sprite.topbounds) { platform.body.sprite.velo *= -1;}        platform.body.velocity.y = platform.body.sprite.velo;    }}
Link to comment
Share on other sites

Because I don't know why it works, or what parts of it make it work in such a way to produce that result. However, like I said, I do have that working (as you can see in my demo), it just feels a bit wrong with all the gravity / friction. However, I was also told that friction should be a value from 0 to 1, so I will probably try making it higher and see if that changes it any.

Link to comment
Share on other sites

well with friction below 1 it is very hard to land on a platform because you slide right over it..  but if you manage to stand still on it - it will carry you nevertheless.. so a very high friction is not neccesary to get carried along the path of the moving platform...

 

 

btw.  why are you doing this?  this is for arcade physics where there is no friction setting

 this.body.velocity.x = 0;

i believe that you cancel out the velocity the moving platform gives you on every step.. thats why its  quivering and at some place falling of the platform

Link to comment
Share on other sites

No it's not. Have you tried the demo I linked? You can land on it and stay on it fine. Even when the friction was 0.5 or so, it was fine. I am doing that because it works for jumping. The reason it falls off the platform because of the gravity not being strong enough... not because of that. If I lower the gravity it will still fall off, and I suspect if I raise it, it will stay on there perfectly. Unless you have a demo that shows it both ways and that it's definitely that.

Link to comment
Share on other sites

since you obviously didn't even try my proposal i tried it for you...  and here it is - the demo you asked for...

 

http://test.xapient.net/phaser/ALL/moving-jumpthrough-platform-final-withveloreset.html

 

as you can see i am now falling of my horizontal moving platform if i set velocity.x of my player to 0 every step..

 

but do as you whish..

go try and fix your code with an even more insane gravity setting or  what about mass.. you could give your player 10000 mass ? 

 

scnr

Link to comment
Share on other sites

Yes, okay, that's what @schteppe told me too (and he said to disable it while on a platform, which would be the easiest solution probably). Thanks for putting together the demo.

 

I said I _didn't_ want to make the gravity higher, which is why I was looking for alternate solutions. However, when I said "I don't understand why yours works" I meant "I don't understand why yours works, so please explain why it does". I find it to be a messy solution to manually move the sprite so I was trying to not do that (which is what I suspected you were doing, but I wasn't sure).

 

Anyway, I suppose either way I likely have a solution but I'll try it later to be sure.

Link to comment
Share on other sites

see.. thats what    I   don't understand..  what do you mean by "messy solution to manually move the sprite"  

how else would you move a sprite if not with velocity?? 

 

manually as far as i would put it would be to set the x coordinates directly..  now that would mess up collision handling and whatelse...  but that's not what i do.. 

 

i simply apply a velocity to the body and if it reaches a certain point i change the direction.. eveything else seems to me like an overkill.

 

the only thing i do "manually" is to tell the sprite to check for this certain point in the updateloop..  you could subclass the sprite to do it on it's own

 

@gravity: sorry.. forgot the [sarcasm] tag.. 

@have you tried the demo:  i didn't just try it - i read the code.. or how do you think i found out you set velocity to 0 on every step ;-)

@what you suspected: no need to suspect.. just rightclick - and view/copy the source - thats what i put these demos up for  :)  (i know you wont actually copy the code since you seem to write code 10times more experienced than i do)

 

glhf

Link to comment
Share on other sites

I did manage to get something running: http://seiyria.com/dd-test/

 

I just conditionally do velocity.x = 0 (only if you're in the air, to prevent dumb air movement)

 

@valueerror I did look at your source code, but it did not make sense at the time why it worked. I probably understand it now.

 

Thanks @pandavigoureux29. I didn't want to need any events to determine if I was on the platform (this is how I previously did it, and I ended up having variables while I was in each state such as platform, climbing a ladder, etc, which was frustrating), I was hoping I could work with the contact materials (and I seem to have a working solution).

 

Thanks all for the help, I got it down eventually!

Link to comment
Share on other sites

How did you do that then? I see you're setting the contact material { friction : 1, restitution : 0 } but that doesn't seem to be enough for me ( player sliding when the platform changes direction)

 

Moreover your player is "sinking" on the vertical platform when they go back to the top, so there may be something more to it. 

 

Edit: okay, your player is sliding too :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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