mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 03:22:39 +08:00
Fix problems where physics and scene synchronization are different (#4224)
This commit is contained in:
committed by
Weiliang Wang
parent
74310477c2
commit
0609c58a9d
@@ -8,7 +8,7 @@ import {
|
||||
import Vec3 from '../../../core/value-types/vec3';
|
||||
import { vec3 } from '../../../core/vmath';
|
||||
import { BoxShapeBase, ShapeBase, SphereShapeBase } from '../../physics/api';
|
||||
import { createBoxShape, createSphereShape, ERigidBodyType } from '../../physics/instance';
|
||||
import { createBoxShape, createSphereShape, ERigidBodyType, ETransformSource } from '../../physics/instance';
|
||||
import { PhysicsBasedComponent } from './detail/physics-based-component';
|
||||
|
||||
export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
@@ -23,13 +23,18 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
set isTrigger (value) {
|
||||
this._triggered = value;
|
||||
|
||||
const type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
|
||||
if (this._body) {
|
||||
this._body.setType(type);
|
||||
const type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.KINEMATIC;
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.body.setType(type);
|
||||
}
|
||||
if (this._shapeBase) {
|
||||
this._shapeBase.setCollisionResponse(!this._triggered);
|
||||
}
|
||||
this._shapeBase.setCollisionResponse(!this._triggered);
|
||||
}
|
||||
|
||||
@property
|
||||
private _center: Vec3 = new cc.Vec3(0, 0, 0);
|
||||
|
||||
/**
|
||||
* The center of the collider, in local space.
|
||||
*/
|
||||
@@ -40,19 +45,21 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
|
||||
set center (value: Vec3) {
|
||||
vec3.copy(this._center, value);
|
||||
this._shapeBase.setCenter(this._center);
|
||||
if (this._shapeBase) {
|
||||
this._shapeBase.setCenter(this._center);
|
||||
}
|
||||
}
|
||||
|
||||
@property
|
||||
private _center: Vec3 = new cc.Vec3(0, 0, 0);
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
super.__preload();
|
||||
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.transfromSource = ETransformSource.SCENE;
|
||||
this.sharedBody.body.setUseGravity(false);
|
||||
}
|
||||
this.center = this._center;
|
||||
this.isTrigger = this._triggered;
|
||||
|
||||
@@ -61,19 +68,12 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
|
||||
}
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
public start () {
|
||||
super.start();
|
||||
}
|
||||
|
||||
public lateUpdate () {
|
||||
if (this.node.hasChanged) {
|
||||
this._shapeBase.setScale(this.node._scale);
|
||||
}
|
||||
}
|
||||
// public lateUpdate () {
|
||||
// TODO : 根据node的scale dirty标记去缩放shape
|
||||
// if (this.node.hasChanged) {
|
||||
// this._shapeBase.setScale(this.node._scale);
|
||||
// }
|
||||
// }
|
||||
|
||||
public onEnable () {
|
||||
if (this.sharedBody) {
|
||||
@@ -120,10 +120,6 @@ export class BoxColliderComponent extends ColliderComponentBase {
|
||||
this._shape.setScale(this.node._scale);
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
/**
|
||||
* The size of the box, in local space.
|
||||
* @note Shall not specify size with component 0.
|
||||
@@ -135,7 +131,9 @@ export class BoxColliderComponent extends ColliderComponentBase {
|
||||
|
||||
set size (value) {
|
||||
vec3.copy(this._size, value);
|
||||
this._shape!.setSize(this._size);
|
||||
if (this._shape) {
|
||||
this._shape.setSize(this._size);
|
||||
}
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.body.commitShapeUpdates();
|
||||
}
|
||||
@@ -157,7 +155,6 @@ export class SphereColliderComponent extends ColliderComponentBase {
|
||||
}
|
||||
|
||||
public __preload () {
|
||||
|
||||
// const scale = this.node._scale;
|
||||
// const max = Math.abs(Math.max(scale.x, Math.max(scale.y, scale.z)));
|
||||
this._shape = createSphereShape(this._radius);
|
||||
@@ -169,10 +166,6 @@ export class SphereColliderComponent extends ColliderComponentBase {
|
||||
this._shape.setScale(this.node._scale);
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
/**
|
||||
* The radius of the sphere.
|
||||
*/
|
||||
@@ -183,7 +176,9 @@ export class SphereColliderComponent extends ColliderComponentBase {
|
||||
|
||||
set radius (value) {
|
||||
this._radius = value;
|
||||
this._shape.setRadius(value);
|
||||
if (this._shape) {
|
||||
this._shape.setRadius(value);
|
||||
}
|
||||
if (this.sharedBody) {
|
||||
this.sharedBody.body.commitShapeUpdates();
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import { Quat, Vec3 } from '../../../../core/value-types';
|
||||
import { quat, vec3 } from '../../../../core/vmath';
|
||||
import { Node } from '../../../../scene-graph/node';
|
||||
import { AfterStepCallback, BeforeStepCallback, ICollisionCallback, ICollisionEvent, PhysicsWorldBase, RigidBodyBase } from '../../../physics/api';
|
||||
import { createRigidBody } from '../../../physics/instance';
|
||||
import { createRigidBody, ERigidBodyType, ETransformSource } from '../../../physics/instance';
|
||||
import { stringfyQuat, stringfyVec3 } from '../../../physics/util';
|
||||
|
||||
export class PhysicsBasedComponent extends Component {
|
||||
|
||||
protected get _body () {
|
||||
return this._sharedBody!.body;
|
||||
return this._sharedBody ? this._sharedBody.body : null;
|
||||
}
|
||||
|
||||
protected get sharedBody () {
|
||||
@@ -26,12 +26,6 @@ export class PhysicsBasedComponent extends Component {
|
||||
this._refSharedBody();
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
}
|
||||
|
||||
public start () {
|
||||
}
|
||||
|
||||
public destroy () {
|
||||
if (this._sharedBody) {
|
||||
this._sharedBody.deref();
|
||||
@@ -39,12 +33,6 @@ export class PhysicsBasedComponent extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
// public syncPhysWithScene () {
|
||||
// if (this.sharedBody) {
|
||||
// this.sharedBody.syncPhysWithScene(this.node);
|
||||
// }
|
||||
// }
|
||||
|
||||
private _refSharedBody () {
|
||||
if (this._sharedBody) {
|
||||
return;
|
||||
@@ -86,6 +74,8 @@ class SharedRigidBody {
|
||||
|
||||
private _transformInitialized: boolean = false;
|
||||
|
||||
private _isShapeOnly: boolean = true;
|
||||
|
||||
constructor (node: Node, world: PhysicsWorldBase) {
|
||||
this._body = createRigidBody({
|
||||
name: node.name,
|
||||
@@ -104,6 +94,15 @@ class SharedRigidBody {
|
||||
return this._body;
|
||||
}
|
||||
|
||||
/** 设置场景与物理之间的同步关系 */
|
||||
public set transfromSource (v: ETransformSource) {
|
||||
if (v === ETransformSource.SCENE) {
|
||||
this._isShapeOnly = true;
|
||||
} else {
|
||||
this._isShapeOnly = false;
|
||||
}
|
||||
}
|
||||
|
||||
public ref () {
|
||||
if (!this._refCount) {
|
||||
this._activeBody();
|
||||
@@ -134,7 +133,7 @@ class SharedRigidBody {
|
||||
this.body.setPosition(p);
|
||||
this.body.setRotation(r);
|
||||
|
||||
// remove to shape, scale belong to shape
|
||||
// scale property should handle by shape, scale belong to shape
|
||||
// node.getWorldScale(this._worldScale);
|
||||
// // Because we sync the scale, we should update shape parameters.
|
||||
// this.body.scaleAllShapes(this._worldScale);
|
||||
@@ -203,18 +202,34 @@ class SharedRigidBody {
|
||||
this._transformInitialized = true;
|
||||
this.syncPhysWithScene(this._node);
|
||||
this._activeBody();
|
||||
} else {
|
||||
// 开始物理计算之前,用户脚本或引擎功能有可能改变节点的Transform,所以需要判断并进行更新
|
||||
if (this._node.hasChanged) {
|
||||
this.syncPhysWithScene(this._node);
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.body.isPhysicsManagedTransform() && this._node.hasChanged) {
|
||||
// d(`Synchronize`);
|
||||
this.syncPhysWithScene(this._node);
|
||||
this._activeBody();
|
||||
}
|
||||
// if (!this.body.isPhysicsManagedTransform() && this._node.hasChanged) {
|
||||
// // d(`Synchronize`);
|
||||
// this.syncPhysWithScene(this._node);
|
||||
// this._activeBody();
|
||||
// }
|
||||
}
|
||||
|
||||
private _afterStep () {
|
||||
if (this.body.isPhysicsManagedTransform()) {
|
||||
// if (this.body.isPhysicsManagedTransform()) {
|
||||
// this._syncSceneWithPhys();
|
||||
// }
|
||||
|
||||
// 物理计算之后,除了只有形状组件的节点,其它有刚体组件的节点,并且刚体类型为DYNAMIC的,需要将计算结果同步到Scene中
|
||||
if (!this._isShapeOnly && this.body.getType() === ERigidBodyType.DYNAMIC) {
|
||||
this._syncSceneWithPhys();
|
||||
} else {
|
||||
// 对于只有形状组件的节点,需要将Scene中节点的Transform同步到Phyisc。
|
||||
// 这是因为物理计算后可能会改变一些节点,这会导致这些子节点的Transform也发生改变。
|
||||
if (this._node.hasChanged) {
|
||||
this.syncPhysWithScene(this._node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,12 @@ import {
|
||||
import { Quat, Vec3 } from '../../../core/value-types';
|
||||
import { vec3 } from '../../../core/vmath';
|
||||
import { PhysicsMaterial } from '../../assets/physics/material';
|
||||
import { ETransformSource } from '../../physics/instance';
|
||||
import { DefaultPhysicsMaterial as DefaultPhysicsMaterial } from './default-material';
|
||||
import { PhysicsBasedComponent } from './detail/physics-based-component';
|
||||
|
||||
const NonRigidBodyProperties = {
|
||||
mass: 0,
|
||||
mass: 10,
|
||||
linearDamping: 0,
|
||||
angularDamping: 0,
|
||||
};
|
||||
@@ -36,10 +37,10 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
private _angularDamping: number = NonRigidBodyProperties.angularDamping;
|
||||
|
||||
@property
|
||||
private _fixedRotation: boolean = true;
|
||||
private _fixedRotation: boolean = false;
|
||||
|
||||
@property
|
||||
private _triggered: boolean = true;
|
||||
private _triggered: boolean = false;
|
||||
|
||||
@property
|
||||
private _isKinematic: boolean = false;
|
||||
@@ -53,11 +54,9 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
super();
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
public __preload () {
|
||||
super.__preload();
|
||||
this.sharedBody!.transfromSource = ETransformSource.PHYSIC;
|
||||
if (this.sharedBody) {
|
||||
this.mass = this._mass;
|
||||
this.linearDamping = this._linearDamping;
|
||||
@@ -65,7 +64,12 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
this.material = this._material;
|
||||
this.useGravity = this._useGravity;
|
||||
this.velocity = this._velocity;
|
||||
this.isKinematic = this._isKinematic;
|
||||
this.fixedRotation = this._fixedRotation;
|
||||
}
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
// if (this.sharedBody) {
|
||||
// this.sharedBody.body.wakeUp();
|
||||
// }
|
||||
@@ -77,13 +81,6 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
// }
|
||||
}
|
||||
|
||||
public start () {
|
||||
super.start();
|
||||
if (this._enabled) {
|
||||
this.onEnable();
|
||||
}
|
||||
}
|
||||
|
||||
@property({
|
||||
type: PhysicsMaterial,
|
||||
})
|
||||
@@ -158,6 +155,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
}
|
||||
}
|
||||
|
||||
@property
|
||||
get fixedRotation () {
|
||||
return this._fixedRotation;
|
||||
}
|
||||
@@ -165,7 +163,7 @@ export class RigidBodyComponent extends PhysicsBasedComponent {
|
||||
set fixedRotation (value) {
|
||||
this._fixedRotation = value;
|
||||
if (this._body) {
|
||||
this._body.setFreezeRotation(!value);
|
||||
this._body.setFreezeRotation(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -166,9 +166,9 @@ function fillRaycastResult (result: RaycastResult, cannonResult: CANNON.RaycastR
|
||||
);
|
||||
}
|
||||
|
||||
enum TransformSource {
|
||||
enum ETransformSource {
|
||||
Scene,
|
||||
Phycis,
|
||||
Physics,
|
||||
}
|
||||
|
||||
export class CannonRigidBody implements RigidBodyBase {
|
||||
@@ -185,7 +185,7 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
private _onWorldPostStepListener: (event: CANNON.ICollisionEvent) => any;
|
||||
private _collisionCallbacks: ICollisionCallback[] = [];
|
||||
private _shapes: CannonShape[] = [];
|
||||
private _transformSource: TransformSource = TransformSource.Scene;
|
||||
private _transformSource: ETransformSource = ETransformSource.Scene;
|
||||
private _userData: any;
|
||||
private _world: CannonWorld | null = null;
|
||||
private _name: string;
|
||||
@@ -196,6 +196,7 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
this._cannonMaterial = defaultCannonMaterial;
|
||||
this._cannonBody = new CANNON.Body({
|
||||
material: this._cannonMaterial,
|
||||
type: ERigidBodyType.DYNAMIC,
|
||||
});
|
||||
setWrap<RigidBodyBase>(this._cannonBody, this);
|
||||
this._cannonBody.allowSleep = true;
|
||||
@@ -259,10 +260,10 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
public setMass (value: number) {
|
||||
this._cannonBody.mass = value;
|
||||
this._cannonBody.updateMassProperties();
|
||||
if (this._cannonBody.type !== CANNON.Body.KINEMATIC) {
|
||||
this._resetBodyTypeAccordingMess();
|
||||
this._onBodyTypeUpdated();
|
||||
}
|
||||
// if (this._cannonBody.type !== CANNON.Body.KINEMATIC) {
|
||||
// // this._resetBodyTypeAccordingMess();
|
||||
// // this._onBodyTypeUpdated();
|
||||
// }
|
||||
}
|
||||
|
||||
public getIsKinematic () {
|
||||
@@ -273,9 +274,10 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
if (value) {
|
||||
this._cannonBody.type = CANNON.Body.KINEMATIC;
|
||||
} else {
|
||||
this._resetBodyTypeAccordingMess();
|
||||
this._cannonBody.type = CANNON.Body.DYNAMIC;
|
||||
// this._resetBodyTypeAccordingMess();
|
||||
}
|
||||
this._onBodyTypeUpdated();
|
||||
// this._onBodyTypeUpdated();
|
||||
}
|
||||
|
||||
public getLinearDamping () {
|
||||
@@ -369,7 +371,7 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
}
|
||||
|
||||
public isPhysicsManagedTransform (): boolean {
|
||||
return this._transformSource === TransformSource.Phycis;
|
||||
return this._transformSource === ETransformSource.Physics;
|
||||
}
|
||||
|
||||
public getPosition (out: Vec3) {
|
||||
@@ -435,9 +437,9 @@ export class CannonRigidBody implements RigidBodyBase {
|
||||
private _onBodyTypeUpdated () {
|
||||
if (this._cannonBody) {
|
||||
if (this._cannonBody.type === CANNON.Body.STATIC) {
|
||||
this._transformSource = TransformSource.Scene;
|
||||
this._transformSource = ETransformSource.Scene;
|
||||
} else {
|
||||
this._transformSource = TransformSource.Phycis;
|
||||
this._transformSource = ETransformSource.Physics;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@ export enum ERigidBodyType {
|
||||
KINEMATIC = 4,
|
||||
}
|
||||
|
||||
export enum ETransformSource {
|
||||
SCENE = 0,
|
||||
PHYSIC = 1,
|
||||
}
|
||||
|
||||
export function createPhysicsWorld (): PhysicsWorldBase {
|
||||
return new PhysicsWorld();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user