liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.85k stars 1.97k forks source link

How to get pivot point of a complexe body ? #958

Open DzzD opened 3 years ago

DzzD commented 3 years ago

Hello, not sure if is the right place to ask a question, I apologize if it is not the case.

First some context, I use matter.js in a graphical library I write (https://github.com/DzzD/TIGLDemo) wich is not intended to run into a web browser, it is intended to be used in Titanium mobile application wich embed its own JavaScript engine (I said that because everything related to webbrowser are useless in my case and even if it is possible to compile matter.js without that I would love that).

TiglPhysics

Anyway here is my (simple) question, how to I find the pivot point of a body ?

Let me be more precise, for rendering my engine needs posx, posy, pivotx, pivoty, rotation, this is easy with rectangle/circle & standard sprite as I set the pivot to width/2 and height/2 but I cant figure how to do with more complexe body ? what should I set as the pivot point ?

Thanks

liabru commented 3 years ago

The body.position is the point the body rotates about, which is the centre of mass by default (in world space). The angle about that point is body.angle. Does that help?

DzzD commented 3 years ago

Yes I understand that the body.position is also the center of rotation, but it seems that the vertices are translated (by minus the center of mass) after creation and I dont know how much they are translated except for simple cases : rectangle, circle.

liabru commented 3 years ago

How are you creating the bodies?

If it's using Bodies.fromVertices then the vertices are translated such that their centre of mass is now at the point body.position. You can find that offset using the bounds after creation e.g. Vector.sub(body.position, body.bounds.min) if the vertices were offset bottom-right of (0, 0) originally.

DzzD commented 3 years ago

Ok I think this is probably what I am looking for. but is that true even if no vertices was set to 0,0 when created the body ? does that means that the center of mass is always the body's bounds center ?

For now I only create body from rectangle or circle as I did not know how to properly center/set pivot to them.

Let say I have those vertices : 200, 100 200, 200 100, 200

And that I create a body at position 0,0.

The mass center is (500/3, 500/3) = 166, 166

Vertices will be translate to satisfy the fact that center of mass must be at 0,0 (body position) so they will be translated by -166, -166 wich does not seems to corespond to min bounds limits wich is 100, 100.

liabru commented 3 years ago

Ah the key for that working is that the bounding box of your original input vertices was aligned to be bottom-right of the origin (0, 0), which is how a lot of tools will export.

But in the general case, if the original bounds have some other offset, then you will need to account for that by first finding the original bounds of the input vertices (e.g. Bounds.create can help) then subtract the same point on both bounds to find the translation vector relative to the original.

I realise though it's not obvious that this is the case, so I'll think of ways to document or return that vector.

DzzD commented 3 years ago

Thanks, this should works, but as you suggested an helper method/properties would be welcome