fixes applyForce and applyImpulse function call error, modify shape and body construct order (#4195)

This commit is contained in:
jiaxin.lai
2019-04-12 09:49:44 +08:00
committed by Weiliang Wang
parent 295d74416c
commit 5a0b49ac25
4 changed files with 94 additions and 27 deletions
@@ -12,19 +12,22 @@ import { createBoxShape, createSphereShape, ERigidBodyType } from '../../physics
import { PhysicsBasedComponent } from './detail/physics-based-component';
export class ColliderComponentBase extends PhysicsBasedComponent {
protected _shapeBase: ShapeBase | null = null;
protected _shapeBase!: ShapeBase;
@property
private _triggered: boolean = false;
@property
get isTrigger () { return this._triggered; }
set isTrigger (value) {
this._triggered = value;
const type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
if (this._body) {
const type = this._triggered ? ERigidBodyType.DYNAMIC : ERigidBodyType.STATIC;
this._body.setType(type);
this._body.setIsTrigger(value);
}
this._shapeBase.setCollisionResponse(!this._triggered);
}
/**
@@ -37,7 +40,7 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
set center (value: Vec3) {
vec3.copy(this._center, value);
this._shapeBase!.setCenter(this._center);
this._shapeBase.setCenter(this._center);
}
@property
@@ -47,17 +50,29 @@ export class ColliderComponentBase extends PhysicsBasedComponent {
super();
}
public __preload () {
super.__preload();
this.center = this._center;
this.isTrigger = this._triggered;
if (this._enabled) {
this.onEnable();
}
}
public onLoad () {
super.onLoad();
this.center = this._center;
}
public start () {
super.start();
if (this._enabled) {
this.onEnable();
}
public lateUpdate () {
if (this.node.hasChanged) {
this._shapeBase.setScale(this.node._scale);
}
this.isTrigger = this._triggered;
}
public onEnable () {
@@ -88,18 +103,25 @@ export class BoxColliderComponent extends ColliderComponentBase {
@property
private _size: Vec3 = new Vec3(0, 0, 0);
private _shape: BoxShapeBase;
private _shape: BoxShapeBase | undefined;
constructor () {
super();
}
public __preload () {
// 父类在__preload要对_shapeBase进行操作,需要保证其已经构造完成
this._shape = createBoxShape(this._size);
this._shape.setUserData(this);
this._shapeBase = this._shape;
super.__preload();
this._shape.setScale(this.node._scale);
}
public onLoad () {
super.onLoad();
this.size = this._size;
}
/**
@@ -113,7 +135,7 @@ export class BoxColliderComponent extends ColliderComponentBase {
set size (value) {
vec3.copy(this._size, value);
this._shape.setSize(this._size);
this._shape!.setSize(this._size);
if (this.sharedBody) {
this.sharedBody.body.commitShapeUpdates();
}
@@ -128,18 +150,27 @@ export class SphereColliderComponent extends ColliderComponentBase {
@property
private _radius: number = 0;
private _shape: SphereShapeBase;
private _shape!: SphereShapeBase;
constructor () {
super();
}
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);
this._shape.setUserData(this);
this._shapeBase = this._shape;
super.__preload();
this._shape.setScale(this.node._scale);
}
public onLoad () {
super.onLoad();
this.radius = this._radius;
}
/**
@@ -10,13 +10,13 @@ import { stringfyQuat, stringfyVec3 } from '../../../physics/util';
export class PhysicsBasedComponent extends Component {
protected get _body () {
return this._sharedBody ? this._sharedBody.body : null;
return this._sharedBody!.body;
}
protected get sharedBody () {
return this._sharedBody;
}
private _sharedBody: SharedRigidBody | null = null;
private _sharedBody!: SharedRigidBody | null;
constructor () {
super();
@@ -39,11 +39,11 @@ export class PhysicsBasedComponent extends Component {
}
}
public syncPhysWithScene () {
if (this.sharedBody) {
this.sharedBody.syncPhysWithScene(this.node);
}
}
// public syncPhysWithScene () {
// if (this.sharedBody) {
// this.sharedBody.syncPhysWithScene(this.node);
// }
// }
private _refSharedBody () {
if (this._sharedBody) {
@@ -127,17 +127,18 @@ class SharedRigidBody {
}
public syncPhysWithScene (node: Node) {
this._transformInitialized = true;
// sync position rotation
const p = node.getWorldPosition();
const r = node.getWorldRotation();
this.body.setPosition(p);
this.body.setRotation(r);
node.getWorldScale(this._worldScale);
// Because we sync the scale, we should update shape parameters.
this.body.scaleAllShapes(this._worldScale);
this.body.commitShapeUpdates();
// remove to shape, scale belong to shape
// node.getWorldScale(this._worldScale);
// // Because we sync the scale, we should update shape parameters.
// this.body.scaleAllShapes(this._worldScale);
// this.body.commitShapeUpdates();
}
/**
@@ -199,9 +200,12 @@ class SharedRigidBody {
if (!this._transformInitialized) {
// d(`Initialize`);
this._transformInitialized = true;
this.syncPhysWithScene(this._node);
this._activeBody();
} else if (!this.body.isPhysicsManagedTransform() && this._node.hasChanged) {
}
if (!this.body.isPhysicsManagedTransform() && this._node.hasChanged) {
// d(`Synchronize`);
this.syncPhysWithScene(this._node);
this._activeBody();
+4
View File
@@ -146,6 +146,10 @@ export class ShapeBase {
getUserData (): any;
setUserData (data: any): void;
getCollisionResponse():boolean;
setCollisionResponse(value:boolean):void;
}
export class SphereShapeBase extends ShapeBase {
+29 -1
View File
@@ -535,6 +535,14 @@ export class CannonShape implements ShapeBase {
}
public getCollisionResponse (): boolean {
return this.impl.collisionResponse;
}
public setCollisionResponse (v: boolean): void {
this.impl.collisionResponse = v;
}
public _devStrinfy () {
if (!this._body) {
return `<NotAttached>`;
@@ -594,7 +602,8 @@ export class CannonBoxShape extends CannonShape implements BoxShapeBase {
constructor (size: Vec3) {
super();
vec3.scale(this._halfExtent, size, 0.5);
this._cannonBox = new CANNON.Box(this._halfExtent);
// attention : here should use clone
this._cannonBox = new CANNON.Box(this._halfExtent.clone());
setWrap<ShapeBase>(this._cannonBox, this);
setWrap<ShapeBase>(this._cannonBox.convexPolyhedronRepresentation, this);
this._cannonShape = this._cannonBox;
@@ -603,6 +612,25 @@ export class CannonBoxShape extends CannonShape implements BoxShapeBase {
public setScale (scale: Vec3): void {
super.setScale(scale);
this._recalcExtents();
// Another implementation method
// see https://github.com/schteppe/cannon.js/issues/270
// if (this._body) {
// const body = this._body;
// const shape = this._cannonBox;
// const iShape = body.shapes.indexOf(shape);
// if (iShape >= 0) {
// body.shapes.splice(iShape, 1);
// body.shapeOffsets.splice(iShape, 1);
// body.shapeOrientations.splice(iShape, 1);
// body.updateMassProperties();
// body.updateBoundingRadius();
// body.aabbNeedsUpdate = true;
// (shape as any).body = null;
// }
// }
// vec3.multiply(this._cannonBox.halfExtents, this._cannonBox.halfExtents, scale);
// if (this._body) { this._body.addShape(this._cannonBox); }
}
public setSize (size: Vec3) {