I am using PhysicsJS to create a juggling game. All I have is a ball (circle shape) and a shoe (square using convex-polygon shape). The shoe is set to fixed and moves along the x-axis on mousemove.
Square:
square = Physics.body('convex-polygon', {
x: 250,
y: 400,
vertices: [
{ x: -68, y: 29 },
{ x: 68, y: 29 },
{ x: 69, y: -29 },
{ x: -68, y: -29 }
],
angle: -0.2,
view: shoeImage,
fixed: true,
restitution: 1
});
world.add(square);
Mousemove event:
jQuery('canvas').on('mousemove', function (e) {
var offset = $(this).offset();
var relativeX = (e.pageX - offset.left);
square.state.pos.set(relativeX, 400);
});
I also added a click event on the shoe to have a 'kick effect'. So far I've done this by changing the square.state.angular.pos
and set it back to the previous angular position with a setTimeout
function.
jQuery('canvas').on('click', function (e) {
if (square.state.angular.pos == -0.2) {
square.state.angular.pos = 0.3;
}
else {
square.state.angular.pos = -0.2;
}
setTimeout(function() { resetShoe(square); }, 500);
});
function resetShoe(square) {
if (square.state.angular.pos == -0.2) {
square.state.angular.pos = 0.3;
}
else {
square.state.angular.pos = -0.2;
}
}
You can see it working here. It works fine but I'd like this to be animated rather than a stop-motion kind of thing. I just can't figure out a way to do it.