Hi I’m trying to modify this example https://labs.phaser.io/edit.html?src=src/physics/matterjs/chain.js&v=3.24.1, in a way that the chain will perpetually move frome left to right like a pendulum.
I properly set inertia and friction to 0 but the chain will ultimatly stop moving.
This is my code :
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#1b1464',
parent: 'phaser-example',
physics: {
default: 'matter'
},
scene: {
preload: preload,
create: create,
update: update
}
};
var block;
var cursors;
var prev;
var prevConstraint;
var velocity = 10;
var game = new Phaser.Game(config);
function preload ()
{
this.load.image('block', 'assets/sprites/block.png');
this.load.image('ball', 'assets/sprites/shinyball.png');
}
function create ()
{
block = this.matter.add.image(400, 50, 'block', null, { ignoreGravity: true, inertia: Infinity });
block.setFixedRotation();
block.setMass(500);
var y = 150;
var x = 400;
prev = block;
block.setFriction(0.000);
block.setFrictionAir(0.000);
block.setFrictionStatic(0.000);
block.setBounce(1);
for (var i = 0; i < 12; i++)
{
var ball = this.matter.add.image(x, y, 'ball', null, { shape: 'circle', mass: 0.1, inertia: Infinity });
ball.setFriction(0.000);
ball.setFrictionAir(0.000);
ball.setFrictionStatic(0.000);
ball.setBounce(1);
//ball.setInertia('Infinity');
prevConstraint = this.matter.add.joint(prev, ball, (i === 0) ? 90 : 35, 1);
prev = ball;
y += 0;
x -= 35;
}
this.matter.add.mouseSpring();
cursors = this.input.keyboard.createCursorKeys();
}
var lastpos;
function update ()
{
if (cursors.left.isDown)
{
block.setVelocityX(-20);
this.matter.world.removeConstraint(prevConstraint);
}
else if (cursors.right.isDown)
{
block.setVelocityX(20);
}
else
{
block.setVelocityX(0);
}
if (cursors.up.isDown)
{
block.setVelocityY(-20);
}
else if (cursors.down.isDown)
{
block.setVelocityY(20);
}
else
{
block.setVelocityY(0);
}
}
Do you have a clue about how I can do it ?
Thanks