This requires us to use techniques when moving things around in physical space. For example, rotation.
Will setMotor ever be introduced? Or is it in place but not documented?
If possible, I feel that a get~~ function to measure relative rotations and distances would be a great help to those trying to work with physics in Babylon.js.
Thanks for the information, I was looking at that article too.
It seems that the person who asked the question in that article got the answer, but honestly I couldn’t understand what kind of solution he got.
The fundamental problem is that things that were in v1 are no longer in v2, so I would be most grateful if you could provide that.
Alternatively, I would like to see more documentation on the use of Motor with 6DF constraints.
As a beginner, I don’t want other beginners who are interested in babylon physics to suffer the same way.
I can gladly help you with this topic. My project uses physics v2 (Havok) and motors. (At some point, I migrated it from v1 to v2, and I tried every physics engine).
First of all, I recommend you A LOT to use the physics engine v2. It is much more reliable and performant. Every other physics engine have disastrous bugs (center of mass computation, missing primitives or constraints, issues in compound objets, etc.). I can’t imagine to use them in a serious project. Moreover the v2 API is much more logical and close to a modern physics engine.
Motors are indeed the black spot of v2. Constraints are quite easy to setup like in this example: Babylon.js Playground
… but when it’s about to motorize them, it becomes a nightmare.
To have a maximum power of expression, I finally coded all the constraints (hinge, prismatic, ball and socket, and lock) of my project directly as Physics6DoFConstraint constraints. To do so please refer to the example above. The tricky part is that every PhysicsConstraintParameters if the Physics6DoFConstraint constructor should be correctly setup. And don’t underestimate the perpAxis
… and I finally use the Physics6DoFConstraint.setAxisMotor* API to motorize them.
Here is a working sample:
To be honest, computing the 6DOF parameters has been a nightmare. In particular in my project were all the objects are shifted and rotated. But I finally get them work, even a motorized prismatic joint.
As I wrote at the end of the first question, how do you get the current angle when using Motor?
This is a very problematic point. If you look at the Havok API, it seems this property is not accessible: @babylonjs/havok - npm => click on HavokPhysics.d.ts
Finally, as the Havok body and the Babylon mesh position and orientation are accessible, implementing a function that computes the expected angle or the expected distance is possible in theory, but I never found a perfect way to do so. There are always issues with bounds and weird cases.
I have for example this function:
// ! works only from -Math.Pi / 2 to Math.Pi / 2 :-(
angleWithAnchor() { // TODO: improve angle with anchor to support a bigger range, and several axes.
const m0 = this.node;
const m1 = this.anchors[0];
const wm0 = m0.getWorldMatrix().getRotationMatrix();
const wm1 = m1.getWorldMatrix().getRotationMatrix();
const rm = wm0.multiply(wm1.transpose());
const q = new Quaternion();
rm.decompose(undefined, q, undefined);
const ea = q.toEulerAngles();
// console.log(ea);
const angle = ea.x;
// const angle = ea.x + ea.y + ea.z;
return angle;
}
you have to fix m0 and m1
there are limits were this is not working
this only works for hinges along the X axis
If somebody finds a better way, I would be happy to get it too
If somebody finds a better way, I would be happy to get it too
Actually, I’ve tried it too.
I would be happy if it was helpful.
These two examples are attempts to create joints to maintain 90 degree orientation.
The first is a case where the calculation breaks down under the specified conditions.
The second case is one that can always be maintained, but a negative angle cannot be specified.
It may be necessary to know how to express the state of m1 in the coordinate system of m0.
It might be a good idea to request a general-purpose function to find the angle between two objects from the Babylon.js community.