mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
do not run phy system in editor;support center of phy shape;fix setWorldPosition() bug (#3690)
* * do not run phy system in editor * support center of phy shape * fix setWorldPosition() bug * add interface pullTransform() for sync dynamic phy body's transform
This commit is contained in:
@@ -4,6 +4,7 @@ import { vec3 } from '../../../core/vmath';
|
||||
import Node from '../../../scene-graph/node';
|
||||
import { PhysicsMaterial as PhysicsMaterial } from '../../assets/physics/material';
|
||||
import { setWrap, getWrap } from './util';
|
||||
import { Quat } from '../../../core/value-types';
|
||||
|
||||
export enum DataFlow {
|
||||
PUSHING,
|
||||
@@ -108,13 +109,6 @@ export class PhysicsBody {
|
||||
throw new Error(`shape not found.`);
|
||||
}
|
||||
|
||||
public setCenter(shape: PhysicsShape, center: Vec3) {
|
||||
const iShape = this._cannonBody.shapes.indexOf(shape._getCannonShape());
|
||||
if (iShape >= 0) {
|
||||
this._cannonBody.shapeOffsets[iShape].set(center.x, center.y, center.z);
|
||||
}
|
||||
}
|
||||
|
||||
get velocity() {
|
||||
return this._cannonBody.velocity;
|
||||
}
|
||||
@@ -233,6 +227,26 @@ export class PhysicsBody {
|
||||
this._cannonBody.position.set(position.x, position.y, position.z);
|
||||
}
|
||||
|
||||
public setWorldScale(scale: Vec3) {
|
||||
this._pullScale(scale);
|
||||
}
|
||||
|
||||
public setWorldRotation(rotation: Quat) {
|
||||
this._cannonBody.quaternion.set(rotation.x, rotation.y, rotation.z, rotation.w);
|
||||
}
|
||||
|
||||
public pullTransform() {
|
||||
if (!this._node) {
|
||||
return;
|
||||
}
|
||||
// @ts-ignore
|
||||
this.setWorldPosition(this._node.getWorldPosition());
|
||||
// @ts-ignore
|
||||
this.setWorldScale(this._node.getWorldScale());
|
||||
// @ts-ignore
|
||||
this.setWorldRotation(this._node.getWorldRotation());
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this body currently in contact with the specified body?
|
||||
* @param {CannonBody} body The body to test against.
|
||||
@@ -278,16 +292,30 @@ export class PhysicsBody {
|
||||
return;
|
||||
}
|
||||
|
||||
// @ts-nocheck
|
||||
this._node.getWorldPosition(this._cannonBody.position);
|
||||
// @ts-nocheck
|
||||
this._node.getWorldRotation(this._cannonBody.quaternion);
|
||||
const scale = this._node.getWorldScale();
|
||||
this.pullTransform();
|
||||
}
|
||||
|
||||
private _pullScale(scale: cc.Vec3) {
|
||||
let shapeUpdated = false;
|
||||
this._shapes.forEach((shape) => {
|
||||
let calcShapeOffset = false;
|
||||
if (shape._centerChanged) {
|
||||
shape._centerChanged = false;
|
||||
calcShapeOffset = true;
|
||||
}
|
||||
|
||||
if (!vec3.exactEquals(scale, shape.scale)) {
|
||||
shape.scale = scale;
|
||||
shapeUpdated = true;
|
||||
calcShapeOffset = true;
|
||||
}
|
||||
|
||||
if (calcShapeOffset) {
|
||||
const iShape = this._cannonBody.shapes.findIndex((cannonShape) => cannonShape === shape._getCannonShape());
|
||||
if (iShape >= 0) {
|
||||
const shapeOffset = this._cannonBody.shapeOffsets[iShape];
|
||||
vec3.multiply(shapeOffset, shape.center, scale);
|
||||
}
|
||||
}
|
||||
});
|
||||
if (shapeUpdated) {
|
||||
@@ -333,6 +361,18 @@ export class PhysicsShape {
|
||||
|
||||
private _scale: cc.Vec3 = new cc.Vec3(1.0, 1.0, 1.0);
|
||||
|
||||
private _center: cc.Vec3 = new cc.Vec3(0, 0, 0);
|
||||
|
||||
public _centerChanged = true;
|
||||
|
||||
public get center() {
|
||||
return this._center;
|
||||
}
|
||||
|
||||
public set center(value) {
|
||||
vec3.copy(this._center, value);
|
||||
}
|
||||
|
||||
constructor(private _cannonShape: CANNON.Shape) {
|
||||
setWrap<PhysicsShape>(this._cannonShape, this);
|
||||
}
|
||||
|
||||
@@ -49,9 +49,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
|
||||
set center(value: Vec3) {
|
||||
this._center = new Vec3(value.x, value.y, value.z);
|
||||
if (this._body) {
|
||||
this._body.setCenter(this._shape, this._center);
|
||||
}
|
||||
this._shape.center = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@ export class PhysicsSystem {
|
||||
}
|
||||
|
||||
public update(deltaTime: number) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
this._world.step(deltaTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,12 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
this._body.angularDamping = value;
|
||||
}
|
||||
}
|
||||
|
||||
public pullTransform() {
|
||||
if (this._body) {
|
||||
this._body.pullTransform();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const NonRigidBodyProperties = {
|
||||
|
||||
@@ -304,7 +304,7 @@ class Node extends BaseNode {
|
||||
parent.updateWorldTransform();
|
||||
vec3.sub(local, this._pos, parent._pos);
|
||||
vec3.transformQuat(local, local, quat.conjugate(q_a, parent._rot));
|
||||
vec3.div(cc.v3(), local, parent._scale);
|
||||
vec3.div(local, local, parent._scale);
|
||||
} else {
|
||||
vec3.copy(this._lpos, this._pos);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user