Jump to content

Claw grab with Phaser


darnpunk
 Share

Recommended Posts

Hi,

I'm trying to create a claw machine game with Phaser. Something like 3d fun catcher but in isometric view. I can't find any resources on how to have an object grab another object. Can this be done with arcade physics?

I'm trying to make it feel like the real machine where depending on how much of the object is grabbed by the claw machine, it could drop.

For the isometric portion, I'm going to use isometric plugin at rotates.org.

Would be great if there's any samples or advice. Thanks in advance!

Link to comment
Share on other sites

Hey drhayes, yes that could work but it might feel like a magnetic or vacuum effect than grabbing. I'm trying to achieve picking up objects with 2 claws and the claw may not be able fully close if the object is too big. It can then drop.

I'll give your suggestion a try and see if I can try to calculate how much is grabbed. Is there a way to use the bounds to check if the object is partially within the 2 claws?

Link to comment
Share on other sites

If you want to simulate the actual grabbing to be simulated using physics then I don't think you need sample code on "grabbing" - just find a demo on setting up a scene with forces and joints, as grabbing would just be a consequence running the simulation with the appropriate objects (forces closing the claw jaws, and pulling it up, gravity acting on potentially grabbed object, etc.)

I think you probably want/need P2 for this also.

Link to comment
Share on other sites

if you use P2, on collision callback set a constraint (locked distance or spring depende of how do you want it)

 

if you use arcade physics you can add the object to the claw with addchild BUT  after this you must reset the child position: if your claw and object measure 32px each one, you should set de grabbed object position AFTER addChild to somithing like x:0 y:16

Link to comment
Share on other sites

Hey guys, thanks for the tips. Looks like I will need P2. I saw the picking up object example using a mouse and it looks pretty close to what I want to do. Except I got to make 2 claws instead of the mouse.

The isometric plugin only comes with arcade physics within.

For me to make the isometric view work with p2, I am guessing there is some form of conversion required to isometric coordinates. But I'm not sure if this is right? There's also the polygon data to look into.

Link to comment
Share on other sites

I've decided to go the non-isometric way as it was too complicated for me. I will be using P2. Currently I have 3 graphics - Machine, left claw, right claw. I am trying to make these claws attach to the machine and when the machine move it follows.

 

Then when the machine moves downwards the claw should open up and when the machine or the claw has any collision with an object, the claw will then close up and grab the object.

 

Firstly, how can I lock the claw to the machine and have its pivot set to the machine itself? I am thinking once I can lock the claws to the machine, the rotation of the claw open and close can be done using a tween. Is this correct?

 

And since the claws will have P2 enabled to it's body, when it tries to close, any object within it should be grabbed.

 

Or can all these be done with P2 purely. Maybe when the claw is being pushed down, it will open up due to the collision? Any advice?

Link to comment
Share on other sites

I've decided to go the non-isometric way as it was too complicated for me. I will be using P2. Currently I have 3 graphics - Machine, left claw, right claw. I am trying to make these claws attach to the machine and when the machine move it follows.

 

Then when the machine moves downwards the claw should open up and when the machine or the claw has any collision with an object, the claw will then close up and grab the object.

 

Firstly, how can I lock the claw to the machine and have its pivot set to the machine itself? I am thinking once I can lock the claws to the machine, the rotation of the claw open and close can be done using a tween. Is this correct?

 

And since the claws will have P2 enabled to it's body, when it tries to close, any object within it should be grabbed.

 

Or can all these be done with P2 purely. Maybe when the claw is being pushed down, it will open up due to the collision? Any advice?

to prevent this you could set he claw as static before open.

Link to comment
Share on other sites

I need some help with putting the machine parts together. I have my graphics ready and the polygon setup using PhysicsEditor. I am trying to position the claws to the machine and have the rotation pivot set to the circle like object at the machine. Basically this is where the claws should be attached. I have attached an image of what I wish to achieve.

 

post-16436-0-52818400-1443083033.png

 

But somehow I can't make it work and what I've got is this one below. The positioning seems off for some reason. I tried playing around with the pivotB param but still can't get it right.

 

post-16436-0-28535800-1443083064.png post-16436-0-32491600-1443083592.png

    // Assets and polygon data are preloaded in preload    // These are in create        blockCollisionGroup = game.physics.p2.createCollisionGroup();    catcher = game.add.sprite(500, 10, 'catcher');    game.physics.p2.enable(catcher, false);    catcher.body.clearShapes();    catcher.body.loadPolygon('physicsData', 'catcher-pivot');    catcher.body.setCollisionGroup(blockCollisionGroup);    catcher.body.collides([blockCollisionGroup]);    catcher.body.fixedRotation = true;    catcher.body.debug = true;    clawRight = game.add.sprite(500, 240, 'clawRight');    game.physics.p2.enable(clawRight, false);    clawRight.body.clearShapes();    clawRight.body.loadPolygon('physicsData', 'catcher-claw-right');    clawRight.body.setCollisionGroup(blockCollisionGroup);    clawRight.body.collides([blockCollisionGroup]);    clawRight.body.fixedRotation = true;    clawRight.body.debug = true;    clawLeft = game.add.sprite(500, 240, 'clawLeft');    game.physics.p2.enable(clawLeft, false);    clawLeft.body.clearShapes();    clawLeft.body.loadPolygon('physicsData', 'catcher-claw-left');    clawLeft.body.setCollisionGroup(blockCollisionGroup);    clawLeft.body.collides([blockCollisionGroup]);    clawLeft.body.fixedRotation = true;    clawLeft.body.debug = true;    this.game.physics.p2.createRevoluteConstraint(catcher, [0, 0], clawRight, [0,0]);    this.game.physics.p2.createRevoluteConstraint(catcher, [0, 0], clawLeft, [0,0]);

Ultimately, I am trying to achieve something like what have been done here in Flash but using Phaser - http://zlap.deviantart.com/art/Claw-Machine-Flash-Physics-99956440

 

Appreciate any advice and tips. Thanks in advance!

Link to comment
Share on other sites

I don't know about pivot, but you could create a sprite for the root of your claw, add 2 children to it (both 'grab arms') and position them so that they fit on the root, set their anchor at the root (the circles), and just make them rotate using sprite.angle (just increment/decrement it in the update() function when activated).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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