babylonjs中坐标转换示例,当我们需要在不同资源中转换坐标时就需要用到坐标转换,一般情况下坐标转换我们使用矩阵,这是一个常见的用法,如果你之前没有接触过相关知识可以通过网上找一些矩阵相关的知识进行学习,它可以轻松实现我们想要的功能。以下代码实现的功能就是box在全局中平衡0.001。看一看是怎么写的吧。
scene.registerAfterRender(function () {
box.rotate(BABYLON.Axis.Y, Math.PI / 150, BABYLON.Space.LOCAL);
box.rotate(BABYLON.Axis.X, Math.PI / 200, BABYLON.Space.LOCAL);
box.translate(new BABYLON.Vector3(-1, -1, -1).normalize(), 0.001, BABYLON.Space.WORLD)
small.rotationQuaternion = box.rotationQuaternion;
matrix = box.getWorldMatrix();
y += 0.001;
local_pos = new BABYLON.Vector3(0, y, 0);
small.position = BABYLON.Vector3.TransformCoordinates(local_pos, matrix);
})
以下代码显示了如何使用相对坐标,关键是matrix = disc.getWorldMatrix(); 和 boxes.position = BABYLON.Vector3.TransformCoordinates(boxes_position, matrix);
var phi = 0;
scene.registerAfterRender(function () {
matrix = disc.getWorldMatrix();
disc.rotate(BABYLON.Axis.Y, Math.PI / 150, BABYLON.Space.LOCAL);
disc.rotate(BABYLON.Axis.Z, Math.PI / 200, BABYLON.Space.LOCAL);
disc.position = new BABYLON.Vector3(15 * Math.cos(phi), 16 * Math.sin(phi), 5)
boxes.rotationQuaternion = disc.rotationQuaternion;
boxes.position = BABYLON.Vector3.TransformCoordinates(boxes_position, matrix);
phi +=0.01;
});