Fix cannon body aabb update (#6450)

* fix cannon body aabb update

* tweaks
This commit is contained in:
jiaxin.lai
2020-03-31 16:53:11 +08:00
committed by GitHub
parent 1b365e7698
commit febd4971d0
3 changed files with 13 additions and 13 deletions
+4 -8
View File
@@ -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);
}
}
}
+1 -1
View File
@@ -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;
}
+8 -4
View File
@@ -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);