New to Phaser JS and trying to build a "simple" game. If you can imagine the game brick breaker - just the base and the ball. I want to be able to move the base up and have the ball still sitting on the base. I also want to be able to tilt the base and have the ball "roll" down depending on which side is tilted.
Initially the ball drops and lands on the base and if I move the base right away everything works the way I expect. But my issue is after the ball is resting on the base, I can not move the base up without having the ball fall thru the base - I also can not tilt the base in that state because the ball does not move along while the base is tilting.
Maybe I need to somehow set the ball sprite (using Box2d physics in Phaser) to enabled / active so that the physics engine takes the sprite into consideration instead of ignoring it (once it's been "timed out")?
UPDATE: From @philipp's suggestion, using Phaser's default Arcade Physics, I am now able to get the up movement of the base to keep the ball sitting on-top (although there are some brief overlap's that I'd like to keep from happening and not sure how to prevent that at the moment?); I was not able to allow the ball to "roll" down either side while tilting the base. It seems that even though the base is being rotated that the collision area is not being moved and the ball does not know that it needs to "roll" down. Here is an example phaser sanbox with debugging information (move left or right to see the base rotate): http://phaser.io/sandbox/WVVnswOt/play
UPDATE 2: Here is a codepen of my Box2d Phaser attempt. If you use the arrow keys immediately to move the base up,down,left, right (while coliding with the box) then physics work as expected. If you wait a few seconds after the box rests on the base, moving the base has no effect on the box; I would like the box to be effected by the base even after the initial wait: http://codepen.io/mgalpy/pen/wWRBpo
;
UPDATE 3: Finally was able to achieve my goal. Made the base "static" but movable by using kinematic (instead of static) and zero'ing the base's velocities on each update. There is now rotation left and right. Here is a codepen with Box2d Phaser physics moving the bottom sprite with a sprite resting on top , and having the top sprite's physics respond - even after it has been inactive or at rest: http://codepen.io/mgalpy/pen/qNLVdA
// JavaScript source code
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', {
preload: function () {
this.scale.pageAlignHorizontally = true;
this.game.stage.backgroundColor = "#FFFFFF";
this.game.load.image('boxImg', boxImg);
},
create: function () {
this.game.physics.startSystem(Phaser.Physics.BOX2D);
this.game.physics.box2d.gravity.y = 500;
this.game.physics.box2d.setBoundsToWorld();
var vertices = [[-57, 51.5, 54.5, 53, -55, 53.5]];
this.box2 = this.game.add.sprite(420, 50, 'boxImg');
this.game.physics.box2d.enable(this.box2);
this.createPolygon(this.box2, vertices);
this.box = this.game.add.sprite(420, 450, 'boxImg');
this.game.physics.box2d.enable(this.box);
this.createPolygon(this.box, vertices);
this.box.body.fixedRotation = true;
this.box.body.kinematic = true;
this.cursors = this.game.input.keyboard.createCursorKeys();
},
createPolygon: function (piece, vertices) {
piece.body.clearFixtures();
for (var i = 0; i < vertices.length; i++) {
piece.body.addPolygon(vertices[i]);
}
},
render: function () {
this.game.debug.box2dWorld();
},
update: function () {
this.box.body.velocity.x = 0;
this.box.body.velocity.y = 0;
this.box.body.angularVelocity = 0;
if (this.cursors.left.isDown) {
this.box.body.rotateLeft(30);
}
else if (this.cursors.right.isDown) {
this.box.body.rotateRight(30);
}
if (this.cursors.up.isDown) {
this.box.body.moveUp(200);
}
else if (this.cursors.down.isDown) {
this.box.body.moveDown(200);
}
}
});
var boxImg = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAABYCAYAAAB4fpBOAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8v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