matter-js
matter-js copied to clipboard
applyForce error
Hello, I am trying to apply force to a ball when I click on a arrow or press the right arrow key but it gives me a type error. This is my code: `` const Engine = Matter.Engine; const World = Matter.World; const Bodies = Matter.Bodies; const Body = Matter.Body; var ball; var rightButton; var leftButton;
function setup() { createCanvas(700,400);
engine = Engine.create(); world = engine.world;
options = { isStatic : false, restitution:0.3, friction:0, density:1.2 }
//ball = Bodies.circle(350, 50, 50, options) ball = new Ball(350, 50, 50); rightButton = createImg("up1.png"); rightButton.position(360,330); rightButton.size(50, 50); rightButton.mouseClicked(right);
leftButton = createImg("up2.png"); leftButton.position(290,330); leftButton.size(50, 50); }
function draw() { background(51); ball.show(); // ellipse(ball.position.x, ball.position.y, 50);
Engine.update(engine);
}
function right() { Matter.Body.applyForce(ball, ball.position, {x:5, y:0});
}
class Ball { constructor(x, y, r) { let options = { isStatic : false, density : 1.2, }
this.body = Bodies.circle(x, y, r, options);
this.x = x;
this.y = y;
this.r = r;
World.add(world, this.body);
} show() { var pos = this.body.position; push(); // circleMode(CENTER); ellipse(pos.x, pos.y, this.r); pop(); } }
function keyPressed() { if(keyCode === RIGHT_ARROW){ var ballPos = ball.position; Matter.Body.applyForce(ball, ballPos, {x:5.05, y:-60.5})
}
}`` Thanks
You can use three backticks before and after the code on their own lines to make the code block work.
Closing as it's not clear that this is a bug.