seiyria Posted September 7, 2014 Share Posted September 7, 2014 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 2Now 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 2the player collision group has a mask of 4the platform collision group has a mask of 8 player.body.data.shapes[0].collisionGroup is set to 4player.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 More sharing options...
rich Posted September 8, 2014 Share Posted September 8, 2014 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 More sharing options...
seiyria Posted September 8, 2014 Author Share Posted September 8, 2014 Yeah, I just started doing that as well, I've edited it in where it's being called, and I'm still getting some weird behavior (now I'm behind the terrain instead of in front of it, but I still appear to be falling through). Link to comment Share on other sites More sharing options...
rich Posted September 8, 2014 Share Posted September 8, 2014 Fair enough. I can't run your code (can barely read it ) so there's nothing more I can suggest really. Link to comment Share on other sites More sharing options...
seiyria Posted September 8, 2014 Author Share Posted September 8, 2014 I'll try and make up a demo (probably tomorrow), so at least you could test it out. Link to comment Share on other sites More sharing options...
seiyria Posted September 8, 2014 Author Share Posted September 8, 2014 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 More sharing options...
valueerror Posted September 8, 2014 Share Posted September 8, 2014 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 More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 I'm actually a lot closer now: http://seiyria.com/dd-test/ Unfortunately the gravity feels really strong (it's set to 3000 currently), and the platforms don't work really well, but they're set to friction 1, restitution 0, and I kinda slide along with them when I'm on them (but I fall off after a time). Link to comment Share on other sites More sharing options...
pandavigoureux29 Posted September 9, 2014 Share Posted September 9, 2014 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.... Link to comment Share on other sites More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
pandavigoureux29 Posted September 9, 2014 Share Posted September 9, 2014 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 demohttp://test.xapient.net/phaser/ALL/moving-jumpthrough-platform.html Link to comment Share on other sites More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
pandavigoureux29 Posted September 9, 2014 Share Posted September 9, 2014 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 More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
valueerror Posted September 9, 2014 Share Posted September 9, 2014 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.htmlfunction 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 More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
valueerror Posted September 9, 2014 Share Posted September 9, 2014 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 More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
valueerror Posted September 9, 2014 Share Posted September 9, 2014 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 More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
valueerror Posted September 9, 2014 Share Posted September 9, 2014 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 More sharing options...
pandavigoureux29 Posted September 9, 2014 Share Posted September 9, 2014 I know you 'd want to use only raw p2 features, but here's what I got so far : http://jsfiddle.net/pandavigoureux29/zphsz7kp/ I'm pretty happy with that and, as valueerror said, this might be the minimum of "home code" you would have to write to make it work like you want. At least I didn't find any "native P2 oriented" solution Link to comment Share on other sites More sharing options...
seiyria Posted September 9, 2014 Author Share Posted September 9, 2014 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 More sharing options...
pandavigoureux29 Posted September 10, 2014 Share Posted September 10, 2014 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 More sharing options...
valueerror Posted September 10, 2014 Share Posted September 10, 2014 @pandavigoureux29your fiddle is very interesting.. but isn't it a little bit unnatural for the body on the platfrom to be on par with the platform pixelperfect ? the natural thing would be to slide a little bit into the last direction if the floor your standing on suddenly changes direction.. Link to comment Share on other sites More sharing options...
Recommended Posts