我想让立方体按着他的一条边旋转,我找不到其他的办法了,大佬们有没有办法
在 Quat 四元数里面
我看了这个api 但是我实现不了这个功能,(out: Out, rot: Out, axis: VecLike, rad: number),这个rot参数我不知道是该传一个什么Quat参数,我看unity 的RotateAround(Vector3 point, Vector3 axis,float angle);好像传的是一个v3,我搞不太懂了,要是还有其他方法实现这个需求也行,我刚学这个。。望指教
我研究半天了,找不到解决办法,有没有人知道的,我太难了
我简单写一个给你参考吧。
import { _decorator, Component, Node, SystemEvent, systemEvent, EventMouse, Quat, Vec3} from "cc";
const { ccclass, property } = _decorator;
const tempVec3: Vec3 = new Vec3();
const tempQuat: Quat = new Quat();
@ccclass("testRotLocal")
export class testRotLocal extends Component {
/* class member could be defined like this */
// dummy = '';
/* use `property` decorator if your want the member to be serializable */
// @property
// serializableDummy = 0;
private _isMove: boolean = false;
private moveTime: number = 0.5;
private _curMoveTime: number = 0;
private _onceAngle = Math.PI / 2;
private _forwardAnchor: Vec3 = new Vec3(0, -0.5, -0.5);
private _startPos: Vec3 = new Vec3();
private _startRot: Quat = new Quat();
private _anchorPos: Vec3 = new Vec3();
private _curRotAxis: Vec3 = Vec3.UNIT_X;
start () {
// Your initialization goes here.
systemEvent.on(SystemEvent.EventType.MOUSE_UP, this.onMouseUp, this);
}
onMouseUp(event: EventMouse) {
if (event.getButton() === 0) {
this.rotateForward();
}
}
rotateAround(startPos: Vec3, startRot: Quat, point: Vec3, axis: Vec3, angle: number) {
Quat.fromAxisAngle(tempQuat, axis, angle);
Vec3.subtract(tempVec3, startPos, point);
Vec3.transformQuat(tempVec3, tempVec3, tempQuat);
Vec3.add(tempVec3, point, tempVec3);
this.node.setWorldPosition(tempVec3);
Quat.rotateAround(tempQuat, startRot, axis, angle);
Quat.normalize(tempQuat, tempQuat);
this.node.setWorldRotation(tempQuat);
}
rotateForward() {
if (!this._isMove) {
this._isMove = true;
this._curMoveTime = 0;
this.node.getWorldPosition(this._startPos);
Vec3.add(this._anchorPos, this._forwardAnchor, this._startPos);
this.node.getWorldRotation(this._startRot);
}
}
update (deltaTime: number) {
// Your update function goes here.
if (this._isMove) {
this._curMoveTime += deltaTime;
if (this._curMoveTime > this.moveTime) {
// end
this.rotateAround(this._startPos, this._startRot, this._anchorPos, this._curRotAxis, -this._onceAngle);
this._isMove = false;
} else {
let angle = this._curMoveTime * (-this._onceAngle) / this.moveTime;
this.rotateAround(this._startPos, this._startRot, this._anchorPos, this._curRotAxis, angle);
}
}
}
}
13赞
给力
真的十分感谢,太给力了。
收藏mark一下
mark mark