valueerror Posted June 26, 2014 Share Posted June 26, 2014 i need to check if there is an object above the player with which i doesn't collide yet (but will collide if he jumps).. so lets say: i want to find out if there is something 40 pixels above my player.. or in other words: ;-) if (isObjectOnPosition(player.y-40)){ wouldcollideifjump = true; } thx Link to comment Share on other sites More sharing options...
lewster32 Posted June 26, 2014 Share Posted June 26, 2014 The best way to accomplish this would be to have an invisible sprite with a body positioned 40 pixels above the player and check that sprite for overlap whenever the player presses the jump button, thus allowing you to determine what's above the player (if anything). Link to comment Share on other sites More sharing options...
CtlAltDel Posted June 26, 2014 Share Posted June 26, 2014 you're looking for raycasting, there's a demo on the examples page Link to comment Share on other sites More sharing options...
lewster32 Posted June 26, 2014 Share Posted June 26, 2014 I don't think raycasting works this way in Phaser; only TilemapLayers implement this natively. Raycasting is indeed one of the ways you'd do it in Unity for instance, but not yet in Phaser, without doing a lot of line intersect stuff a la this example: http://gamemechanicexplorer.com/#raycasting-1 I should elaborate that with my method, your detection body should be 40px tall and positioned so the body.bottom is 1 pixel above the player.body.top so you don't end up with gaps and detect everything potentially above the player. Link to comment Share on other sites More sharing options...
CtlAltDel Posted June 26, 2014 Share Posted June 26, 2014 I assumed he was using a tilemap Link to comment Share on other sites More sharing options...
lewster32 Posted June 26, 2014 Share Posted June 26, 2014 I didn't But if you are using a tilemap, then yeah listen to the guy above me - a raycast would be a perfect way to do it. Link to comment Share on other sites More sharing options...
valueerror Posted June 26, 2014 Author Share Posted June 26, 2014 if i would use tilemaps i could use the gettileworldXY function to find out if there is a tile on the given xy position on a specific layer... Thats the way i did it until now.. But today i changed all collision layers to object layers and i convert the polylines i paint into physics bodies so all getTile methods are out of service now...maybe i will add a sensor shape to my player and check for collusions on this shape.. I just hoped there is a way to obly check for this potential collision if needed.. i guess with the sensor i will have to set a variable to true on begincontact and then to faose on endcontact .. And then i run into the endcontact is not send bug again.. Link to comment Share on other sites More sharing options...
wayfinder Posted June 26, 2014 Share Posted June 26, 2014 You can use the hitTest function in p2 http://docs.phaser.io/Phaser.Physics.P2.html#hitTest Link to comment Share on other sites More sharing options...
valueerror Posted June 26, 2014 Author Share Posted June 26, 2014 hittest sounds very good.. ill give it a try tomorrow.. in the meanwhile - thx to all of you for your suggestions Link to comment Share on other sites More sharing options...
valueerror Posted June 27, 2014 Author Share Posted June 27, 2014 hittest always returns an array of ALL objects in my game world.. no matter what i try.. unfortunately this is useless but i dont think this is supposed to be that way.. Link to comment Share on other sites More sharing options...
wayfinder Posted June 27, 2014 Share Posted June 27, 2014 Interesting – it works as advertised here. Link to comment Share on other sites More sharing options...
valueerror Posted June 27, 2014 Author Share Posted June 27, 2014 i tried it in a simple setup only my player and one object... console.log( game.physics.p2.hitTest([400,400]));this should return an empty array and instead returns the player and the object.. Link to comment Share on other sites More sharing options...
lewster32 Posted June 27, 2014 Share Posted June 27, 2014 You need to pass it a Phaser.Point object:console.log(game.physics.p2.hitTest(new Phaser.Point(400, 400)));Or you could probably get away with an object literal (untested):console.log(game.physics.p2.hitTest({x: 400, y: 400})); Link to comment Share on other sites More sharing options...
valueerror Posted June 27, 2014 Author Share Posted June 27, 2014 damn.. thx again lewster.. now wayfinders suggestion works like charm.. thank you both !! Link to comment Share on other sites More sharing options...
lewster32 Posted June 27, 2014 Share Posted June 27, 2014 Just be aware that by testing for a single point, you will miss situations like low ceilings with a gap above them - you ideally need to 'sweep' the 40 pixels up from your character to detect what's in the way, rather than testing a discreet point. A raycast would give you that sweep, as would a tall collider. It's implementation dependent of course, if your platforms are all 'deep' or you'd not have situations where you'd have floating platforms etc. you'd be fine. Link to comment Share on other sites More sharing options...
wayfinder Posted June 27, 2014 Share Posted June 27, 2014 In my game, I work with a sensor array (which ironically enough aren't sensor shapes; I disable their collisions in presolve but I need the full overlap data so I can't make them sensors). It's currently made up of ground feelers ("feet" that point into the floor) and a forward sensor that is always oriented to where the player is going. Ceiling and walls aren't as important for me right now, so I use the player body for these. Link to comment Share on other sites More sharing options...
Recommended Posts