titmael Posted June 18, 2014 Share Posted June 18, 2014 Hi, I was wondering how to manage this with P2 : I have a player, with P2 activated. I want to attach to this player a hammer, with P2 too to check collisions with enemies. I try different things but not conviced, I have collisions between my player and the hammer, if I clear collision for hammer how can I check collisions with enemies ?I'm using lockconstraint to keep the hammer fixed to the player. Thx Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 If I use constraints the hammer isn't really attached to my player :var constraint2 = game.physics.p2.createRevoluteConstraint(hero, [ 0, 0], this, [ 0, 20 ]);When I use my hammer (rotating it) it "detaches" from the player and very slowly comes back to the attach point, so not what I want. If I use barbarian method in the hammer update :this.body.x = this.hero.body.x;this.body.y = this.hero.body.y;this.x = this.hero.x;this.y = this.hero.y;my hammer is moving away ... I don't get why (the debugged body is the hammer body, we can see the sprite slowly going down) : Anyone already achieve this ? Link to comment Share on other sites More sharing options...
wayfinder Posted June 18, 2014 Share Posted June 18, 2014 You could move the hammer AFTER the physics update, for instance in render() and not in update() Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 I'd probably do this not with physics, but with a tween. Make the hammer kinematic so the physics system does not influence its movement, then set up a tween chain for the hammer strike animation, and then use overlap (or the equivalent in P2 - I'm afraid I don't know P2 well at this point) to detect hits and, if necessary stop the tween and return the hammer to the resting position. Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 It's never called, whereas the update is well calledWeapon.prototype.render = function(){ this.body.x = this.hero.body.x; this.body.y = this.hero.body.y; this.x = this.hero.x; this.y = this.hero.y;} Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 I'd probably do this not with physics, but with a tween. Make the hammer kinematic so the physics system does not influence its movement, then set up a tween chain for the hammer strike animation, and then use overlap (or the equivalent in P2 - I'm afraid I don't know P2 well at this point) to detect hits and, if necessary stop the tween and return the hammer to the resting position. If I have a lot of enemies, will it be an efficient way to check overlap between the weapon and all enemies ? And how to detect collision with custom shape (not a circle or square) ? Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 If you only check for overlap during a specific period while the hammer is being swung, and maybe implement some other sanity checks to only look for collisions with enemies in a certain radius then it shouldn't slow things down to any significant degree. I'm afraid I've no real answer for the custom shape question; I assume P2 lets you detect overlaps between custom shapes without the need for the physics system to affect their movement? This is what kinematic is for I'd imagine? Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 Didn't know about KINEMATIC, I'm gonna try. In fact physics is enabled, just the body of my hammer to put in KINEMATIC mode Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 Using KINEMATIC only change that the sprite will move only with velocity, doesn't change anything of my probs Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 Sorry again, I've gotten this wrong, the body's motionState needs to be DYNAMIC. Kinematic means it responds to velocity and detects collisions and overlaps, dynamic means it can detect collisions and overlaps but only moves by manually positioning it, and static means it cannot move or detect collisions or overlaps. Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 Found a way, looks like I got what I want, I reused a CollisionHandler I created previously :function CollisionsHandler(game){ this.game = game; this.game.physics.p2.setPostBroadphaseCallback(this.checkCollision, this);}CollisionsHandler.prototype.constructor = CollisionsHandler;CollisionsHandler.prototype.checkCollision = function(body1, body2){ if(body1.sprite != null && body2.sprite != null){ //Hero's collisions if(this.checkBodies(body1, body2, [Weapon.name])){ if(this.checkBodies(body1, body2, [Hero.name])){ return false; } } } return true;}CollisionsHandler.prototype.checkBodies = function(body1, body2, constructorNames){ return body1 && body1.sprite && (_.contains(constructorNames, body1.sprite.constructor.name) || _.contains(constructorNames, body2.sprite.constructor.name));}I check the collision between the Hero and Weapon. If they collides I return false so physics reaction doesn't apply. To initiate this :BasicGame.Game.prototype.create = function(){ [...] new CollisionsHandler(this.game); [...]}So I still can use P2 for hero and hammer Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 Great stuff I should really get stuck in with P2 and learn a bit more about it. Link to comment Share on other sites More sharing options...
ZoomBox Posted June 18, 2014 Share Posted June 18, 2014 You have to:-Create 3 different collision group: playerCollisionGroup, hammerCollisionGroup and ennemyCollisionGroup-addPolygon on your hammer for its physic body-Do this in your hammer class:this.body.setCollisionGroup(hammerCollisionGroup);this.body.collides(ennemyCollisionGroup);-Do this in your player class:this.body.setCollisionGroup(playerCollisionGroup);this.body.collides(ennemyCollisionGroup);-Do this in your ennemy class:this.body.setCollisionGroup(ennemyCollisionGroup);this.body.collides([hammerCollisionGroup, playerCollisionGroup]);The code speaks itself: The player collides with ennemies, ennemies collides with the player and hammer, the hammer collides with ennemies. Link to comment Share on other sites More sharing options...
ZoomBox Posted June 18, 2014 Share Posted June 18, 2014 But I assume the hammer have to be trowable. If not, it looks like a terrible way to do it. Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 I got the impression the hammer was swingable, but not throwable - the original post mentions the hammer is to be attached, and a constraint was mentioned? Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 Right, the hammer is attached to the player and only rotate to hit enemies. @Zoombox : my player and enemies need to collide with the tilemap too (didn't talk about it there because it wasn't the topic), don't know how to do it, because it's automaticaly done when converting the tilemap I now have another problem : http://www.html5gamedevs.com/topic/7257-p2-rotation-and-anchor-point/ When I'm done with this one I'll give you my implementation Link to comment Share on other sites More sharing options...
ZoomBox Posted June 18, 2014 Share Posted June 18, 2014 If the Hammer is attached to the player, you should just make a spriteSheet of the player with the hammer rotating around him and then play the animation. So the player and the hammer are the same sprite.Then you do something similar to what I explayed but a bit more specific:-You clear your player's shapes-You add the the shape that should be his physic body (colliding to wall and ennemies) and you specify its mask-Same with the hammerLike this:// ---- Player class ---- //// The physic bodythis.body.addCircle(_radius);this.body.data.shapes[0].collisionGroup = playerCollisionGroup.mask;this.body.data.shapes[0].collisionMask = groundCollisionGroup.mask + ennemyCollisionGroup.mask + 2; //When you add multiples masks, you have to add 2 (I dont know why)// The hammer non-physic bodythis.body.addCircle(_radius); this.body.data.shapes[1].collisionGroup = hammerCollisionGroup.mask;this.body.data.shapes[1].collisionMask = ennemyCollisionGroup.mask;this.body.data.shapes[1].sensor = true;I explain:This way your player have 2 different bodies, the first is for the ground and walls and the second one is not physic (sensor=true) and is used only to detect ennemies. Then when an ennemy is in the "hammer range" of your player, you check if the player is currently attacking, if so, kill him. In both cases (even if you want to keep your hammer separated, which I don't recommend):You have to work with collisionGroup. Find what's the name of the collision group created for your tile map and it'll be fine. Link to comment Share on other sites More sharing options...
titmael Posted June 18, 2014 Author Share Posted June 18, 2014 My solution is working, only a rotation problem now. If the player and the hammer are in the same sprite I can detect the collision between hammer and enemies right ? And my game will have plainty weapons Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 There's nothing stopping you adding images or other sprites to your player sprite with addChild and using the body of the player sprite. I disagree with the idea that you have to combine all of your weapons at the asset-level, as you'd end up with a lot of repetition. Link to comment Share on other sites More sharing options...
ZoomBox Posted June 18, 2014 Share Posted June 18, 2014 You can do it like this, it is possible. But nobody does like this because of performances and surely other things. If the player and the hammer are in the same sprite I can detect the collision between hammer and enemies right ? And my game will have plainty weapons Yes, that's what I just explained. You can have as many weapons as you want and change the range of them by either changing the body itselft or creating multiple bodies and enabling the good one. Link to comment Share on other sites More sharing options...
wayfinder Posted June 18, 2014 Share Posted June 18, 2014 Sorry again, I've gotten this wrong, the body's motionState needs to be DYNAMIC. Kinematic means it responds to velocity and detects collisions and overlaps, dynamic means it can detect collisions and overlaps but only moves by manually positioning it, and static means it cannot move or detect collisions or overlaps. I think that's slightly off? My understanding is that DYNAMIC means it's a fully-fledged part of the physics world that will react to all forces, KINEMATIC means that it will only move if you make it but otherwise takes part in collisions and applies forces to dynamic bodies, and STATIC means that it doesn't react to forces, be it world- or user-generated one but generates collisions and exerts (stopping) forces on other bodies. Shapes that have the sensor flag set are the ones that do not generate collisions (they do however generate startContact and endContact events) Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 I thought this but it seems to be at odds with the documentation for motionState... Link to comment Share on other sites More sharing options...
wayfinder Posted June 18, 2014 Share Posted June 18, 2014 No I think that's the way it works.. static takes neither force nor velocity commands, kinetic takes velocity but not force and dynamic takes both. That's pretty much what the docs say, isn't it? Link to comment Share on other sites More sharing options...
lewster32 Posted June 18, 2014 Share Posted June 18, 2014 I see. Velocity is a physics calculation though still - does that mean you should use static when manually moving the object (i.e. by specifically setting its x and y)? Link to comment Share on other sites More sharing options...
wayfinder Posted June 18, 2014 Share Posted June 18, 2014 Not sure.. I use a kinetic body and manually move it via velocity. Link to comment Share on other sites More sharing options...
Recommended Posts