xxxxxxxxxx
let bodyOnSpring;
let bodyOnMouse;
const punchBagRadius = 100;
function setup() {
createCanvas(windowWidth, windowHeight);
let engine = Matter.Engine.create();
Matter.Engine.run(engine);
//Make a body attatched to mouse that will NOT be pushed around by forces from the simulator (other bodies, gravity, etc)
bodyOnMouse = Matter.Bodies.circle(0, 0, 50, { //x, y, radius, options
isStatic: true
});
Matter.World.add(engine.world, bodyOnMouse);
//make a punchbag body
bodyOnSpring = Matter.Bodies.circle(width / 2, 200, punchBagRadius);
//make a spring
const mySpringConstraint = Matter.Constraint.create({
pointA: {
x: width / 2,
y: height / 2
},
bodyB: bodyOnSpring,
pointB: {
x: 0,
y: 0
},
stiffness: 0.005,
damping: 0.001, //between 0 (none) and 0.1 (heavy)
length: 0 //target resting length of the constraint
});
Matter.World.add(engine.world, bodyOnSpring);
Matter.World.add(engine.world, mySpringConstraint);
}
function draw() {
noStroke();
background(60);
fill('#ffd3b6');
circle(bodyOnSpring.position.x, bodyOnSpring.position.y, punchBagRadius*2);
fill('#fdffab20');
circle(bodyOnMouse.position.x, bodyOnMouse.position.y, 100);//x, y, diameter
Matter.Body.setPosition(bodyOnMouse, {
x: mouseX,
y: mouseY
})
}