mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Fix cannon body aabb update (#6450)
* fix cannon body aabb update * tweaks
This commit is contained in:
@@ -9,6 +9,7 @@ import { TransformBit } from '../../core/scene-graph/node-enum';
|
||||
import { Node } from '../../core';
|
||||
import { CollisionEventType } from '../framework/physics-interface';
|
||||
import { CannonRigidBody } from './cannon-rigid-body';
|
||||
import { commitShapeUpdates } from './cannon-util';
|
||||
|
||||
const v3_0 = new Vec3();
|
||||
const quat_0 = new Quat();
|
||||
@@ -63,9 +64,7 @@ export class CannonSharedBody {
|
||||
}
|
||||
} else {
|
||||
if (this.index >= 0) {
|
||||
// TODO: 待查,组件的 enabledInHierarchy 为什么还是 true
|
||||
const isRemove = (this.shapes.length == 0 && this.wrappedBody == null) ||
|
||||
(this.shapes.length == 0 && this.wrappedBody != null && !this.wrappedBody.rigidBody.enabledInHierarchy) ||
|
||||
(this.shapes.length == 0 && this.wrappedBody != null && !this.wrappedBody.isEnabled)
|
||||
|
||||
if (isRemove) {
|
||||
@@ -115,18 +114,15 @@ export class CannonSharedBody {
|
||||
|
||||
syncSceneToPhysics () {
|
||||
if (this.node.hasChangedFlags) {
|
||||
|
||||
if (this.body.isSleeping()) this.body.wakeUp();
|
||||
Vec3.copy(this.body.position, this.node.worldPosition);
|
||||
Quat.copy(this.body.quaternion, this.node.worldRotation);
|
||||
|
||||
this.body.aabbNeedsUpdate = true;
|
||||
if (this.node.hasChangedFlags & TransformBit.SCALE) {
|
||||
for (let i = 0; i < this.shapes.length; i++) {
|
||||
this.shapes[i].setScale(this.node.worldScale);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.body.isSleeping()) {
|
||||
this.body.wakeUp();
|
||||
commitShapeUpdates(this.body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export function fillRaycastResult (result: PhysicsRayResult, cannonResult: CANNO
|
||||
}
|
||||
|
||||
export function commitShapeUpdates (body: CANNON.Body) {
|
||||
body.aabbNeedsUpdate = true;
|
||||
body.updateMassProperties();
|
||||
body.updateBoundingRadius();
|
||||
body.aabbNeedsUpdate = true;
|
||||
}
|
||||
@@ -55,9 +55,7 @@ export class CannonShape implements IBaseShape {
|
||||
}
|
||||
|
||||
setCenter (v: IVec3Like) {
|
||||
const lpos = this._offset as IVec3Like;
|
||||
Vec3.copy(lpos, v);
|
||||
Vec3.multiply(lpos, lpos, this._collider.node.worldScale);
|
||||
this._setCenter(v);
|
||||
if (this._index >= 0) {
|
||||
commitShapeUpdates(this._body);
|
||||
}
|
||||
@@ -157,7 +155,7 @@ export class CannonShape implements IBaseShape {
|
||||
* @param scale
|
||||
*/
|
||||
setScale (scale: IVec3Like) {
|
||||
this.setCenter(this._collider.center);
|
||||
this._setCenter(this._collider.center);
|
||||
}
|
||||
|
||||
setIndex (index: number) {
|
||||
@@ -169,6 +167,12 @@ export class CannonShape implements IBaseShape {
|
||||
this._orient = Orient;
|
||||
}
|
||||
|
||||
private _setCenter (v: IVec3Like) {
|
||||
const lpos = this._offset as IVec3Like;
|
||||
Vec3.copy(lpos, v);
|
||||
Vec3.multiply(lpos, lpos, this._collider.node.worldScale);
|
||||
}
|
||||
|
||||
private onTrigger (event: CANNON.ITriggeredEvent) {
|
||||
TriggerEventObject.type = event.event;
|
||||
const self = getWrap<CannonShape>(event.selfShape);
|
||||
|
||||
Reference in New Issue
Block a user