Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(physics): add RopeSoftBody, rigidbody dragger, and enhance collisionShapeUtil #448

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/physics/Physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TempPhyMath } from './utils/TempPhyMath';
import { Rigidbody } from './rigidbody/Rigidbody';
import { PhysicsDebugDrawer } from './visualDebug/PhysicsDebugDrawer';
import { DebugDrawerOptions } from './visualDebug/DebugDrawModeEnum';
import { PhysicsDragger } from './utils/PhysicsDragger'

class _Physics {
private _world: Ammo.btDiscreteDynamicsWorld | Ammo.btSoftRigidDynamicsWorld;
Expand All @@ -14,6 +15,7 @@ class _Physics {
private _gravity: Vector3 = new Vector3(0, -9.8, 0);
private _worldInfo: Ammo.btSoftBodyWorldInfo | null = null;
private _debugDrawer: PhysicsDebugDrawer;
private _physicsDragger: PhysicsDragger;
private _physicBound: BoundingBox;
private _destroyObjectBeyondBounds: boolean;

Expand All @@ -33,24 +35,37 @@ class _Physics {
return this._debugDrawer;
}

/**
* 物理拖拽器
*/
public get physicsDragger() {
if (!this._physicsDragger) {
console.warn('To enable the dragger, set useDrag: true in Physics.init() during initialization.');
}
return this._physicsDragger;
}

public TEMP_TRANSFORM: Ammo.btTransform; // Temp cache, save results from body.getWorldTransform()

/**
* 初始化物理引擎和相关配置。
*
* @param options - 初始化选项参数对象。
* @param options.useSoftBody - 是否启用软体模拟,目前仅支持布料软体类型。
* @param options.useSoftBody - 是否启用软体模拟。
* @param options.useDrag - 是否启用刚体拖拽功能。
* @param options.physicBound - 物理边界,默认范围:2000 2000 2000,超出边界时将会销毁该刚体。
* @param options.destroyObjectBeyondBounds - 是否在超出边界时销毁3D对象。默认 `false` 仅销毁刚体。
*/
public async init(options: { useSoftBody?: boolean, physicBound?: Vector3, destroyObjectBeyondBounds?: boolean } = {}) {
public async init(options: { useSoftBody?: boolean, useDrag?: boolean, physicBound?: Vector3, destroyObjectBeyondBounds?: boolean } = {}) {
await Ammo.bind(window)(Ammo);

TempPhyMath.init();

this.TEMP_TRANSFORM = new Ammo.btTransform();
this.initWorld(options.useSoftBody);

if (options.useDrag) this._physicsDragger = new PhysicsDragger();

this._isInited = true;
this._destroyObjectBeyondBounds = options.destroyObjectBeyondBounds;
this._physicBound = new BoundingBox(new Vector3(), options.physicBound || new Vector3(2000, 2000, 2000));
Expand Down
1 change: 1 addition & 0 deletions packages/physics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './rigidbody/RigidbodyEnum';
export * from './rigidbody/Rigidbody';
export * from './rigidbody/GhostTrigger';
export * from './softbody/ClothSoftbody';
export * from './softbody/RopeSoftbody';
export * from './constraint/ConeTwistConstraint';
export * from './constraint/FixedConstraint';
export * from './constraint/Generic6DofConstraint';
Expand Down
6 changes: 4 additions & 2 deletions packages/physics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orillusion/physics",
"version": "0.3.2",
"version": "0.3.3",
"author": "Orillusion",
"description": "Orillusion Physics Plugin, Powerd by Ammo.js",
"main": "./dist/physics.umd.js",
Expand All @@ -20,8 +20,10 @@
"type": "git",
"url": "git+https://github.com/Orillusion/orillusion.git"
},
"dependencies": {
"@orillusion/ammo": ">=0.2.1"
},
"peerDependencies": {
"@orillusion/ammo": ">=0.2.1",
"@orillusion/core": ">=0.8.0"
}
}
8 changes: 5 additions & 3 deletions packages/physics/rigidbody/Rigidbody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export class Rigidbody extends ComponentBase {
*/
public set velocity(value: Vector3) {
this._velocity.copyFrom(value);
this._btRigidbody?.applyForce(TempPhyMath.toBtVec(value), TempPhyMath.zeroBtVec(TempPhyMath.tmpVecB));
this.wait().then(rb => rb.applyForce(TempPhyMath.toBtVec(this._velocity), TempPhyMath.zeroBtVec(TempPhyMath.tmpVecB)));
}

/**
Expand All @@ -468,7 +468,8 @@ export class Rigidbody extends ComponentBase {
* Set the angular velocity value of current object
*/
public set angularVelocity(value: Vector3) {
this._btRigidbody?.setAngularVelocity(TempPhyMath.toBtVec(value))
this._angularVelocity.copyFrom(value)
this.wait().then(rb => rb.setAngularVelocity(TempPhyMath.toBtVec(this._angularVelocity)));
}
/**
* Get the linear velocity value of current object
Expand All @@ -483,7 +484,8 @@ export class Rigidbody extends ComponentBase {
* Set the linear velocity value of current object
*/
public set linearVelocity(value: Vector3) {
this._btRigidbody?.setLinearVelocity(TempPhyMath.toBtVec(value))
this._linearVelocity.copyFrom(value)
this.wait().then(rb => rb.setLinearVelocity(TempPhyMath.toBtVec(this._linearVelocity)));
}
/**
* Get mass value
Expand Down
Loading
Loading