I am building a pool game, and I want to make the cue rotate around the white ball.
Here is the live code:
Here is the code for the white ball:
balls = game.add.group(); balls.enableBody = true; balls.physicsBodyType = Phaser.Physics.P2JS; wball = balls.create(350, 240 , 'ballW'); wball.body.setCircle(15); wball.body.setCollisionGroup(wballCollisionGroup); wball.body.collides([wballCollisionGroup, ballsCollisionGroup , sidesCollisionGroup , holesCollisionGroup , playerCollisionGroup]); game.physics.p2.setImpactEvents(true);
Here is the code for the cue, which I call
player = game.add.sprite( 0 , 0 , 'player'); game.physics.p2.enable(player , true ); player.enableBody = true; player.body.setCollisionGroup(playerCollisionGroup); player.body.setZeroVelocity(); player.body.createBodyCallback(wball, ballHit , this); player.anchor.setTo(-0.5, 0.5); player.pivot.x = wball.x +150; player.pivot.y = 0; player.body.x = wball.x ; player.body.y = 240 ;
The problem is that all pivot or anchor does is change the sprite image position. I got the cue to appear in the right place by using both pivot/anchor and it seems to work fine... the cue was rotating around the ball, but after enabling debug mode here is what I saw:
Basically only the position of the image has been changed but the sprite still rotates around the center which is the red part. If I run this code, the ball and cue are already colliding and the ball jumps around without player doing anything.
How can I rotate the cue around the ball without them colliding?