Jump to content

Player with a hammer


titmael
 Share

Recommended Posts

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

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) :

5002082014061812h3928.png

 

Anyone already achieve this ?  -_-

Link to comment
Share on other sites

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

It's never called, whereas the update is well called

Weapon.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

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

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

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

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

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

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

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 hammer

Like 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

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

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

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

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

 Share

  • Recently Browsing   0 members

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