Browse Source

修改底盘旋转

master
刘向辉 3 years ago
parent
commit
55204dbefb
  1. 17983
      package-lock.json
  2. 106
      src/app/babylonjs/FireTruck.ts
  3. 8
      src/app/babylonjs/babylonjs.component.ts
  4. 17934
      yarn.lock

17983
package-lock.json generated

File diff suppressed because it is too large Load Diff

106
src/app/babylonjs/FireTruck.ts

@ -78,9 +78,9 @@ export class FireTruck {
this.center = MeshBuilder.CreateBox("center", {}, scene);
this.box = MeshBuilder.CreateBox("box", { width: .5, height: .5, depth: 10 })
// this.box.parent = this.center;
// this.center.position.set(10, 0, 10);
// this.box.position.z = 5;
this.box.parent = this.center;
this.center.position.set(10, 0, 10);
this.box.position.z = 5;
});
@ -162,25 +162,53 @@ export class FireTruck {
this.Bi.rotate(Axis.Y, angle, Space.LOCAL);
}
// 自动旋转Bi
public AutoRotateBi() {
const v1 = this.targetPoint.subtract(this.Bi.absolutePosition);
let biPos_xz = this.Bi.absolutePosition.clone();
biPos_xz.y = 0; //只需指向目标点在xz平面的投影即可
const v1 = this.targetPoint.subtract(biPos_xz).normalize();//归一化,因为要用到叉乘的结果做速度
const v2 = this.Bi.forward;
const dir = Vector3.Cross(v1, v2).y;
//A 表示v1与v2的夹角
let cosA = Vector3.Dot(v1, v2);//点积的结果,表示夹角的cos值
if (dir > 0.001) {
// 在顺时针方向
this.Bi.rotate(Axis.Y, this.deltaTime * this.speed, Space.LOCAL)
} else if (dir < -0.001) {
this.Bi.rotate(Axis.Y, this.deltaTime * -this.speed, Space.LOCAL)
} else {
console.log("==========");
const cross = Vector3.Cross(v1, v2);//叉积的结果,表示v1\v2平面的法线向量,大小为v1\v2夹角的sin值(v1v2都是单位向量的前提下)
const sinA = cross.y;//因为,x、z都为0,y直接表示两者的sin值
if ((1 - Math.abs(cosA)) <= 0.00001) {//因为0度与180度的sin值都为0,不可判断是否到位,要用cos值
this.armActionState = ArmActionState.Bi1;
}
else {
let angleSpeed = sinA;// 当夹角不超过90度,则按照叉乘的结果旋转
if (cosA < 0) { //当夹角超过90度,以最快速度旋转
angleSpeed = 1 * cross.normalize().y;
}
let minSpeed = 0.1;//最小旋转速度
if (angleSpeed > -minSpeed && angleSpeed < 0) {
angleSpeed = -minSpeed;
}
if (angleSpeed > 0 && angleSpeed < minSpeed) {
angleSpeed = minSpeed;
}
this.Bi.rotate(Axis.Y, this.deltaTime * angleSpeed * this.speed, Space.LOCAL)
}
// const lookPosBox = this.targetPoint.subtract(this.box.absolutePosition).normalize();
// lookPosBox.y = 0;
// const q0 = Quaternion.FromLookDirectionRH(lookPosBox, Vector3.Up());
// if (!this.box.absoluteRotationQuaternion.equalsWithEpsilon(q0, 0.01)) {
// this.box.rotate(Axis.Y, 0.1, Space.LOCAL)
// this.box.rotate(Axis.Y, 0.01, Space.LOCAL)
// }
// const lookPos = this.targetPoint.subtract(this.Bi.absolutePosition).normalize();
@ -191,6 +219,10 @@ export class FireTruck {
// if (!this.Bi.absoluteRotationQuaternion.equalsWithEpsilon(q1, 0.01)) {
// this.Bi.rotate(Axis.Y, 0.005, Space.LOCAL)
// }
// else {
// console.log("==========");
// this.armActionState = ArmActionState.Bi1;
// }
// const absoluteRotationQuaternion = Quaternion.FromLookDirectionLH(lookPos, Vector3.Up());
// const absoluteRotationQuaternionOffset = absoluteRotationQuaternion.subtract(this.root.absoluteRotationQuaternion);
@ -212,7 +244,7 @@ export class FireTruck {
// const currentAngle = this.Bi.rotationQuaternion.toEulerAngles().y * 180 / Math.PI;
// // console.log(this.Bi.rotationQuaternion.equalsWithEpsilon(new Quaternion(-rotation.x, -rotation.y, -rotation.z, rotation.w), 0.001));
// console.log(this.Bi.rotationQuaternion.equalsWithEpsilon(new Quaternion(-rotation.x, -rotation.y, -rotation.z, rotation.w), 0.001));
// if (Math.abs(targetAngle - currentAngle) > 0.5) {
// const speedDir = (targetAngle - currentAngle) >= 0 ? 1 : -1;
@ -227,30 +259,30 @@ export class FireTruck {
this.Bi1.rotate(Axis.X, angle * 0.1, Space.LOCAL);
}
public AutoRotateBi1() {
const v1 = this.targetPoint.subtract(this.Bi.absolutePosition);
const v2 = this.Bi1.forward;
// const v1 = this.targetPoint.subtract(this.Bi.absolutePosition);
// const v2 = this.Bi1.forward;
const dir = Vector3.Cross(v1.normalize(), v2.normalize()).x;
// const dir = Vector3.Cross(v1.normalize(), v2.normalize()).x;
if (dir > 0.01) {
// 在顺时针方向
this.Bi1.rotate(Axis.X, this.deltaTime * this.speed, Space.LOCAL)
} else if (dir < -0.01) {
this.Bi1.rotate(Axis.X, this.deltaTime * -this.speed, Space.LOCAL)
} else {
console.log("已经转到指定方向");
this.armActionState = ArmActionState.Bi2;
}
// if (dir > 0.01) {
// // 在顺时针方向
// this.Bi1.rotate(Axis.X, this.deltaTime * this.speed, Space.LOCAL)
// } else if (dir < -0.01) {
// this.Bi1.rotate(Axis.X, this.deltaTime * -this.speed, Space.LOCAL)
// } else {
// console.log("已经转到指定方向");
// this.armActionState = ArmActionState.Bi2;
// }
// const d2E = Math.floor(Vector3.Distance(this.Bi6.absolutePosition, this.Bi.absolutePosition)) + 15;//臂长
// const d2T = Math.floor(Vector3.Distance(this.Bi.absolutePosition, this.targetPoint));//臂底端到目标的距离
// const distance = Math.round(Vector3.Distance(this.Bi6.absolutePosition, this.targetPoint)); //Bi6到目标距离
// const v1 = (new Vector3(this.targetPoint.x, this.targetPoint.y, this.targetPoint.z).subtract(this.Bi1.position)).normalize();
// const v2 = this.Bi.forward.normalize();
// const targetDir = Vector3.Cross(v1, v2).x > 0 ? -1 : 1;
const v1 = (new Vector3(this.targetPoint.x, this.targetPoint.y, this.targetPoint.z).subtract(this.Bi1.position)).normalize();
const v2 = this.Bi.forward.normalize();
const targetDir = Vector3.Cross(v1, v2).x > 0 ? -1 : 1;
// const lookPos = this.targetPoint.subtract(this.Bi1.position).normalize();
@ -258,18 +290,18 @@ export class FireTruck {
// this.Bi1.rotationQuaternion = Quaternion.Slerp(this.Bi1.rotationQuaternion, new Quaternion(rotation.x, rotation.y, rotation.z, rotation.w), this.deltaTime * 10);
// const targetAngle = Math.acos(Vector3.Dot(v1, v2)) * 180 / Math.PI * targetDir;
// const currentAngle = this.Bi1.rotationQuaternion.toEulerAngles().x * 180 / Math.PI;
const targetAngle = Math.acos(Vector3.Dot(v1, v2)) * 180 / Math.PI * targetDir;
const currentAngle = this.Bi1.rotationQuaternion.toEulerAngles().x * 180 / Math.PI;
// console.log(currentAngle,);
// console.log(targetAngle, "=================");
// if (Math.abs(targetAngle - currentAngle) > 0.5) {
// const speedDir = (targetAngle - currentAngle) >= 0 ? 1 : -1;
// this.RotateBi1(speedDir * 0.01);
// } else {
// this.armActionState = ArmActionState.Bi6;
// }
if (Math.abs(targetAngle - currentAngle) > 0.5) {
const speedDir = (targetAngle - currentAngle) >= 0 ? 1 : -1;
this.RotateBi1(speedDir * 0.01);
} else {
this.armActionState = ArmActionState.Bi6;
}
// console.log(distance, '')
// if (d2E < d2T) {

8
src/app/babylonjs/babylonjs.component.ts

@ -63,7 +63,7 @@ export class BabylonjsComponent implements OnInit {
const fireTruck = new FireTruck(scene);
// 检测鼠标点击事件
scene.onPointerDown = (evt: PointerEvent, pickInfo: PickingInfo, type: PointerEventTypes) => {
if (pickInfo.hit) {
if (evt.button == 2 && pickInfo.hit) {
sphere.position = pickInfo.pickedPoint;
fireTruck.setTarget(sphere.absolutePosition);
}
@ -82,6 +82,12 @@ export class BabylonjsComponent implements OnInit {
engine.runRenderLoop(() => {
scene.render();
});
// 监听浏览器改变大小的事件,通过调用engine.resize()来自适应窗口大小
window.addEventListener("resize", function () {
engine.resize();
});
}
public createScene(): Scene {

17934
yarn.lock

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save