Skip to content

Commit

Permalink
PhysX based raycast and collider (#505)
Browse files Browse the repository at this point in the history
* feat: add physx version raycast and trigger
* feat: add  trigger event in script
  • Loading branch information
yangfengzzz authored Sep 24, 2021
1 parent 986cbc4 commit fa92e1c
Show file tree
Hide file tree
Showing 65 changed files with 2,760 additions and 809 deletions.
29 changes: 29 additions & 0 deletions packages/core/src/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Script } from "./Script";
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection";
import { RenderContext } from "./RenderPipeline/RenderContext";
import { Vector3 } from "@oasis-engine/math";
import { Collider } from "./physics/Collider";

/**
* The manager of the components.
Expand All @@ -30,6 +31,9 @@ export class ComponentsManager {
// Delay dispose active/inActive Pool
private _componentsContainerPool: Component[][] = [];

// Physics
private _colliders: DisorderedArray<Collider> = new DisorderedArray();

addRenderer(renderer: Renderer) {
renderer._rendererIndex = this._renderers.length;
this._renderers.add(renderer);
Expand All @@ -52,6 +56,17 @@ export class ComponentsManager {
script._onStartIndex = -1;
}

addCollider(collider: Collider) {
collider._index = this._colliders.length;
this._colliders.add(collider);
}

removeCollider(collider: Collider): void {
const replaced = this._colliders.deleteByIndex(collider._index);
replaced && (replaced._index = collider._index);
collider._index = -1;
}

addOnUpdateScript(script: Script) {
script._onUpdateIndex = this._onUpdateScripts.length;
this._onUpdateScripts.add(script);
Expand Down Expand Up @@ -224,6 +239,20 @@ export class ComponentsManager {
}
}

callColliderOnUpdate() {
const elements = this._colliders._elements;
for (let i = this._colliders.length - 1; i >= 0; --i) {
elements[i]._onUpdate();
}
}

callColliderOnLateUpdate() {
const elements = this._colliders._elements;
for (let i = this._colliders.length - 1; i >= 0; --i) {
elements[i]._onLateUpdate();
}
}

getActiveChangedTempList(): Component[] {
return this._componentsContainerPool.length ? this._componentsContainerPool.pop() : [];
}
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import { ShaderPool } from "./shader/ShaderPool";
import { ShaderProgramPool } from "./shader/ShaderProgramPool";
import { RenderState } from "./shader/state/RenderState";
import { Texture2D, TextureCubeFace, TextureCubeMap, TextureFormat } from "./texture";
import { PhysicsManager } from "./PhysicsManager";
import { ModelMesh, PrimitiveMesh } from "./mesh";
import { CompareFunction } from "./shader";
import { IPhysics } from "@oasis-engine/design";
import { PhysicsManager } from "./physics";

/** TODO: delete */
const engineFeatureManager = new FeatureManager<EngineFeature>();
Expand All @@ -38,7 +39,7 @@ ShaderPool.init();
*/
export class Engine extends EventDispatcher {
/** Physics manager of Engine. */
readonly physicsManager: PhysicsManager = new PhysicsManager(this);
readonly physicsManager: PhysicsManager;

_componentsManager: ComponentsManager = new ComponentsManager();
_hardwareRenderer: IHardwareRenderer;
Expand Down Expand Up @@ -157,11 +158,16 @@ export class Engine extends EventDispatcher {
* Create engine.
* @param canvas - The canvas to use for rendering
* @param hardwareRenderer - Graphics API renderer
* @param physics - native physics Engine
*/
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer) {
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, physics?: IPhysics) {
super(null);
this._hardwareRenderer = hardwareRenderer;
this._hardwareRenderer.init(canvas);
if (physics) {
PhysicsManager._nativePhysics = physics;
this.physicsManager = new PhysicsManager();
}
this._canvas = canvas;
// @todo delete
engineFeatureManager.addObject(this);
Expand Down Expand Up @@ -243,6 +249,11 @@ export class Engine extends EventDispatcher {
const componentsManager = this._componentsManager;
if (scene) {
componentsManager.callScriptOnStart();
if (this.physicsManager) {
componentsManager.callColliderOnUpdate();
this.physicsManager._update(deltaTime);
componentsManager.callColliderOnLateUpdate();
}
componentsManager.callScriptOnUpdate(deltaTime);
componentsManager.callAnimationUpdate(deltaTime);
componentsManager.callScriptOnLateUpdate(deltaTime);
Expand Down
16 changes: 0 additions & 16 deletions packages/core/src/HitResult.ts

This file was deleted.

135 changes: 0 additions & 135 deletions packages/core/src/PhysicsManager.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,4 @@ export class Scene extends EngineObject {
}

features: SceneFeature[] = [];

/**
* Raycast.
* @deprecated
* @param ray
*/
public raycast(ray: { origin: Vector3; direction: Vector3 }, outPos?: Vector3, tag?: Layer): any {}
}
25 changes: 13 additions & 12 deletions packages/core/src/Script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Camera } from "./Camera";
import { ignoreClone } from "./clone/CloneManager";
import { Component } from "./Component";
import { ACollider } from "./collider";
import { ColliderShape } from "./physics";

/**
* Script class, used for logic writing.
*/
Expand Down Expand Up @@ -68,30 +69,30 @@ export class Script extends Component {

/**
* Called when the collision enter.
* @param other Collider
* @param other ColliderShape
*/
onTriggerEnter(other: ACollider): void {}
onTriggerEnter(other: ColliderShape): void {}

/**
* Called when the collision stay.
* @remarks onTriggerStay is called every frame while the collision stay.
* @param other Collider
* @param other ColliderShape
*/
onTriggerStay(other: ACollider): void {}
onTriggerExit(other: ColliderShape): void {}

/**
* Called when the collision exit.
* @param other Collider
* @param other ColliderShape
*/
onTriggerExit(other: ACollider): void {}
onTriggerStay(other: ColliderShape): void {}

/**
* Called when the pointer is down while over the Collider.
* Called when the pointer is down while over the ColliderShape.
*/
onPointerDown(): void {}

/**
* Called when the pointer is up while over the Collider.
* Called when the pointer is up while over the ColliderShape.
*/
onPointerUp(): void {}

Expand All @@ -101,17 +102,17 @@ export class Script extends Component {
onPointerClick(): void {}

/**
* Called when the pointer is enters the Collider.
* Called when the pointer is enters the ColliderShape.
*/
onPointerEnter(): void {}

/**
* Called when the pointer is no longer over the Collider.
* Called when the pointer is no longer over the ColliderShape.
*/
onPointerExit(): void {}

/**
* Called when the pointer is down while over the Collider and is still holding down.
* Called when the pointer is down while over the ColliderShape and is still holding down.
* @remarks onPointerDrag is called every frame while the pointer is down.
*/
onPointerDrag(): void {}
Expand Down
Loading

0 comments on commit fa92e1c

Please sign in to comment.