How to create chain/rope with object on the end with Matter physics


#1

Hey all,

How can I create a chain that hangs from the ceiling (top of game) with an item attached on the lower end? I need this with matter physics so that it moves naturally if collided.


#2

Hey @netgfx
Are you looking something like this: Double Pendulum
This example written in p5.js by Coding Train but atleast u might wanna see the logic behind if this one is your need :slight_smile:


#3

If you use the code here:

http://labs.phaser.io/view.html?src=src\physics\matterjs\chain.js

…it will create a chain. You can either set the holder to ignore gravity like in the example, or you can set it’s position and use the mySprite.setStatic(true); function. Then add your main object and create a new constraint between the main object and the chain object…

block= this.matter.add.image(400, 50, ‘block’, null, {ignore gravity:true});
block.setStatic(true);

var y= 150;
var prev= block;

for (var i = 0; i < 12; i++){

var ball= this.matter.add.image(400, y, ‘ball’, ‘’, {shape: ‘circle’} );

var chain=this.matter.add.joint(prev, ball, (i===0) ? 90 : 35, 0.4);

  prev= ball;
  y += 18
  
  }

mySprite= this.matter.add.image(block.x, block.y+300, ‘mySprite’);

spring= this.matter.add.constraint(mySprite, ball, 5, 0.4, {pointA: {x: 0, y: -10}, pointB: {x:0, y: 300}});

This example has random numbers added to it, but it should get you on the right track.