Skip to content

Commit

Permalink
fix physics 3d auto sync transform (#7455)
Browse files Browse the repository at this point in the history
* fix physics 3d auto sync transform

* absolutely scaling box
  • Loading branch information
JayceLai committed Nov 11, 2020
1 parent aa5a678 commit 7a93744
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
15 changes: 8 additions & 7 deletions cocos2d/core/3d/physics/cannon/cannon-shared-body.ts
Expand Up @@ -25,14 +25,13 @@

import CANNON from '../../../../../external/cannon/cannon';
import { ERigidBodyType } from '../framework/physics-enum';
import { getWrap } from '../framework/util';
import { getWrap, worldDirty } from '../framework/util';
import { CannonWorld } from './cannon-world';
import { CannonShape } from './shapes/cannon-shape';
import { Collider3D } from '../exports/physics-framework';
import { CollisionEventType } from '../framework/physics-interface';
import { CannonRigidBody } from './cannon-rigid-body';
import { commitShapeUpdates, groupIndexToBitMask, deprecatedEventMap } from './cannon-util'
import { updateWorldTransform, updateWorldRT } from "../framework/util"

const LocalDirtyFlag = cc.Node._LocalDirtyFlag;
const PHYSICS_SCALE = LocalDirtyFlag.PHYSICS_SCALE;
Expand Down Expand Up @@ -174,15 +173,16 @@ export class CannonSharedBody {

syncSceneToPhysics (force = false) {
let node = this.node;
let needUpdateTransform = updateWorldTransform(node, force);
let needUpdateTransform = worldDirty(node);
if (!force && !needUpdateTransform) {
return;
}
// body world aabb need to be recalculated
this.body.aabbNeedsUpdate = true;

Vec3.copy(this.body.position, node.__wpos);
Quat.copy(this.body.quaternion, node.__wrot);
node.getWorldPosition(v3_0);
node.getWorldRotation(quat_0)
Vec3.copy(this.body.position, v3_0);
Quat.copy(this.body.quaternion, quat_0);

if (node._localMatDirty & PHYSICS_SCALE) {
let wscale = node.__wscale;
Expand All @@ -201,7 +201,8 @@ export class CannonSharedBody {
if (this.body.type != ERigidBodyType.STATIC && !this.body.isSleeping()) {
Vec3.copy(v3_0, this.body.position);
Quat.copy(quat_0, this.body.quaternion);
updateWorldRT(this.node, v3_0, quat_0);
this.node.setWorldPosition(v3_0);
this.node.setWorldRotation(quat_0);
}
}

Expand Down
3 changes: 3 additions & 0 deletions cocos2d/core/3d/physics/cannon/shapes/cannon-box-shape.ts
Expand Up @@ -52,6 +52,9 @@ export class CannonBoxShape extends CannonShape implements IBoxShape {

set size (v: IVec3Like) {
this.collider.node.getWorldScale(v3_0);
v3_0.x = Math.abs(v3_0.x);
v3_0.y = Math.abs(v3_0.y);
v3_0.z = Math.abs(v3_0.z);
Vec3.multiplyScalar(this.halfExtent, v, 0.5);
Vec3.multiply(this.box.halfExtents, this.halfExtent, v3_0);
this.box.updateConvexPolyhedronRepresentation();
Expand Down
15 changes: 11 additions & 4 deletions cocos2d/core/3d/physics/cocos/builtin-shared-body.ts
Expand Up @@ -24,10 +24,14 @@
****************************************************************************/
import { BuiltInWorld } from './builtin-world';
import { BuiltinShape } from './shapes/builtin-shape';
import { updateWorldTransform } from "../framework/util"
import { worldDirty } from "../framework/util"

const intersect = cc.geomUtils.intersect;
const fastRemove = cc.js.array.fastRemove;
const v3_0 = new cc.Vec3();
const v3_1 = new cc.Vec3();
const quat_0 = new cc.Quat();


/**
* Built-in static collider, no physical forces involved
Expand Down Expand Up @@ -123,11 +127,14 @@ export class BuiltinSharedBody {

syncSceneToPhysics (force: boolean = false) {
let node = this.node;
let needUpdateTransform = updateWorldTransform(node, force);
if (!force && !needUpdateTransform) return;
let needUpdateTransform = worldDirty(node);
if (!force && !needUpdateTransform) return;

node.getWorldPosition(v3_0);
node.getWorldRotation(quat_0)
node.getWorldScale(v3_1);
for (let i = 0; i < this.shapes.length; i++) {
this.shapes[i].transform(node._worldMatrix, node.__wpos, node.__wrot, node.__wscale);
this.shapes[i].transform(node._worldMatrix, v3_0, quat_0, v3_1);
}
}

Expand Down
3 changes: 3 additions & 0 deletions cocos2d/core/3d/physics/cocos/shapes/builtin-box-shape.ts
Expand Up @@ -56,6 +56,9 @@ export class BuiltinBoxShape extends BuiltinShape implements IBoxShape {
set size (size: cc.Vec3) {
Vec3.multiplyScalar(this.localObb.halfExtents, size, 0.5);
this.collider.node.getWorldScale(_worldScale);
_worldScale.x = Math.abs(_worldScale.x);
_worldScale.y = Math.abs(_worldScale.y);
_worldScale.z = Math.abs(_worldScale.z);
Vec3.multiply(this.worldObb.halfExtents, this.localObb.halfExtents, _worldScale);
}

Expand Down
15 changes: 12 additions & 3 deletions cocos2d/core/3d/physics/framework/util.ts
Expand Up @@ -26,11 +26,11 @@
import { IVec3Like, IQuatLike } from '../spec/i-common';

export function stringfyVec3 (value: IVec3Like): string {
return `(x: ${value.x}, y: ${value.y}, z: ${value.z})`;
return `(x: ${value.x}, y: ${value.y}, z: ${value.z})`;
}

export function stringfyQuat (value: IQuatLike): string {
return `(x: ${value.x}, y: ${value.y}, z: ${value.z}, w: ${value.w})`;
return `(x: ${value.x}, y: ${value.y}, z: ${value.z}, w: ${value.w})`;
}

interface IWrapped<T> {
Expand Down Expand Up @@ -180,4 +180,13 @@ export function updateWorldRT (node: cc.Node, position: cc.Vec3, rotation: cc.Qu
node.setPosition(position);
node.setRotation(rotation);
}
}
}

export function worldDirty (node: cc.Node) {
let cur = node;
while (cur) {
if (cur._worldMatDirty) return true;
cur = cur._parent;
}
return false;
}

0 comments on commit 7a93744

Please sign in to comment.