Browse Source

bi1指向目标

master
刘向辉 3 years ago
parent
commit
069a27ef40
  1. 13
      src/app/babylonjs/FireTruck.ts
  2. 5
      src/app/babylonjs/babylonjs.component.ts

13
src/app/babylonjs/FireTruck.ts

@ -132,7 +132,6 @@ export class FireTruck {
} else if (this.input.C) {
this.RotateBi6(this.deltaTime);
} else if (this.targetPoint != null) {
switch (this.armActionState) {
case ArmActionState.Bi:
this.AutoRotateBi();
@ -178,7 +177,7 @@ export class FireTruck {
public AutoRotateBi() {
let biPos_xz = this.Bi.absolutePosition.clone();
biPos_xz.y = 0; //只需指向目标点在xz平面的投影即可
biPos_xz.y = this.targetPoint.y; //只需指向目标点在xz平面的投影即可
const v1 = this.targetPoint.subtract(biPos_xz).normalize();//归一化,因为要用到叉乘的结果做速度
const v2 = this.Bi.forward;
@ -189,6 +188,7 @@ export class FireTruck {
const cross = Vector3.Cross(v1, v2);//叉积的结果,表示v1\v2平面的法线向量,大小为v1\v2夹角的sin值(v1v2都是单位向量的前提下)
const sinA = cross.y;//因为,x、z都为0,y直接表示两者的sin值
console.log(cosA);
if ((1 - Math.abs(cosA)) <= 0.00001) {//因为0度与180度的sin值都为0,不可判断是否到位,要用cos值
this.armActionState = ArmActionState.Bi1;
}
@ -331,12 +331,13 @@ export class FireTruck {
const v1 = (this.targetPoint.subtract(this.Bi1.absolutePosition)).normalize();
const v2 = this.Bi1.forward.normalize();
const sinA = Vector3.Cross(v1, v2);
// console.log(sinA);
let rotateDir = sinA.x > 0 ? -1 : 1;
const sinA = Vector3.Cross(v1, v2);//叉乘结果,反应速度
let rotateDir = v1.y - v2.y < 0 ? 1 : -1;//y向量差值,表示需要旋转的方向
let loaclSpeed = sinA.length() * rotateDir;
// console.log(loaclSpeed, "speed");
this.RotateBi1(loaclSpeed);

5
src/app/babylonjs/babylonjs.component.ts

@ -65,7 +65,10 @@ export class BabylonjsComponent implements OnInit {
scene.onPointerDown = (evt: PointerEvent, pickInfo: PickingInfo, type: PointerEventTypes) => {
if (evt.button == 2 && pickInfo.hit) {
sphere.position = pickInfo.pickedPoint;
fireTruck.setTarget(sphere.absolutePosition);
// setTimeout(() => {
fireTruck.setTarget(pickInfo.pickedPoint);
// }, 100);
}
}
window.addEventListener("keydown", (ev) => {

Loading…
Cancel
Save