无法在Matter.js中定位fromVertices

huangapple go评论64阅读模式
英文:

Unable to position fromVertices in Matter.js

问题

我这里有一个演示:

https://codepen.io/EightArmsHQ/pen/ExOGgoE/898e7ae5b9ac6ebf7dcf2a7300c4a0cc

我试图将两个三角形定位如下:

无法在Matter.js中定位fromVertices

我使用了以下尺寸来定义这些三角形:

const funnelHeight = 100;
const funnelWidth = (this.sizes.w - pipe) / 2;

因此,左侧的中心点为:

funnelWidth / 2, this.sizes.h - funnelHeight / 2

这是我感到困惑的地方。从这一点开始,我绘制了三角形的顶点:

const leftFunnel = [
  {x: -funnelWidth / 2, y: - funnelHeight / 2},
  {x: funnelWidth / 2, y: funnelHeight / 2},
  {x: -funnelWidth / 2, y: funnelHeight / 2},
];

然后将其添加:

this.leftFunnel = Bodies.fromVertices(funnelWidth / 2, this.sizes.h - funnelHeight / 2, leftFunnel , { isStatic: true});

然而,它被定位在远离画布边缘的位置:

无法在Matter.js中定位fromVertices

我做错了什么?

编辑

我已经取得了一些进展。如果我将形状设置为矩形,它就可以正常工作。所以问题在于设置重心,我错误地将对象放在了那个点上,而不是它占据的区域的中心。

英文:

I have a demo here:

https://codepen.io/EightArmsHQ/pen/ExOGgoE/898e7ae5b9ac6ebf7dcf2a7300c4a0cc

I am trying to position two triangles like so:

无法在Matter.js中定位fromVertices

I used the following dimensions for the triangles:

const funnelHeight = 100;
const funnelWidth = (this.sizes.w - pipe) / 2;

The left therefore has a center point of

funnelWidth / 2, this.sizes.h - funnelHeight / 2

Here is where I get confused. So from this point, I draw the points of the triangle:

const leftFunnel = [
  {x: -funnelWidth / 2, y: - funnelHeight / 2},
  {x: funnelWidth / 2, y: funnelHeight / 2},
  {x: -funnelWidth / 2, y: funnelHeight / 2},
];

Then add it:

this.leftFunnel = Bodies.fromVertices(funnelWidth / 2, this.sizes.h - funnelHeight / 2, leftFunnel , { isStatic: true});

However it is being positioned at a distance away from the edges of the canvas:

无法在Matter.js中定位fromVertices

What am I doing wrong?

Edit

I have made some progress. If I set the shape up as a rectangle, it works perfectly. So the issue is matter is setting up the centre of gravity, and I am incorrectly placing the object at that point, not the centre of the area it takes up.

答案1

得分: 1

这是您在编辑后的消息中提到的正确 - 您还应该考虑质心,因为从那一点开始计算身体的位置。通过使用Vertices.centre方法,您可以找到该点并将其用作偏移量:

const Vertices = Matter.Vertices;

//...

const center = Vertices.centre(leftFunnel);

this.leftFunnel = Bodies.fromVertices(
  funnelWidth / 2 + center.x,
  funnelWidth / 2 + center.y,
  leftFunnel,
  { isStatic: true }
);
英文:

It is right as you mentioned in your edited message - you should also take into consideration the center of mass, because from that point position of body gets calculated. By using the Vertices.centre method, you may find that point and use it as offset:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const Vertices = Matter.Vertices;

//...

const center = Vertices.centre(leftFunnel)

this.leftFunnel = Bodies.fromVertices(
  funnelWidth / 2 + center.x,
  funnelWidth / 2 + center.y,
  leftFunnel,
  { isStatic: true }
);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 20:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定