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

PhysX based raycast and collider #489

Closed
wants to merge 18 commits into from
Closed
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
22 changes: 22 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";

/**
* 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) {
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,13 @@ export class ComponentsManager {
}
}

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

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 { IPhysicsEngine } from "@oasis-engine/design";
import { PhysicsManager } from "./physics";

/** TODO: delete */
const engineFeatureManager = new FeatureManager<EngineFeature>();
Expand All @@ -38,10 +39,11 @@ 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;
_physicsEngine: IPhysicsEngine;
_lastRenderState: RenderState = new RenderState();
_renderElementPool: ClassPool<RenderElement> = new ClassPool(RenderElement);
_spriteElementPool: ClassPool<SpriteElement> = new ClassPool(SpriteElement);
Expand Down Expand Up @@ -157,11 +159,16 @@ export class Engine extends EventDispatcher {
* Create engine.
* @param canvas - The canvas to use for rendering
* @param hardwareRenderer - Graphics API renderer
* @param physicsEngine - Physics Engine
*/
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer) {
constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, physicsEngine?: IPhysicsEngine) {
super(null);
this._hardwareRenderer = hardwareRenderer;
this._hardwareRenderer.init(canvas);
this._physicsEngine = physicsEngine;
if (this._physicsEngine) {
this.physicsManager = new PhysicsManager(this);
}
this._canvas = canvas;
// @todo delete
engineFeatureManager.addObject(this);
Expand Down Expand Up @@ -242,6 +249,10 @@ export class Engine extends EventDispatcher {
const scene = this._sceneManager._activeScene;
const componentsManager = this._componentsManager;
if (scene) {
if (this._physicsEngine) {
componentsManager.callColliderOnUpdate();
this.physicsManager.update();
}
componentsManager.callScriptOnStart();
componentsManager.callScriptOnUpdate(deltaTime);
componentsManager.callAnimationUpdate(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 {}
}
27 changes: 23 additions & 4 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 { Collider, Collision } from "./physics";

/**
* Script class, used for logic writing.
*/
Expand Down Expand Up @@ -66,24 +67,42 @@ export class Script extends Component {
*/
onEndRender(camera: Camera): void {}

/**
* OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
* @param other The Collision data associated with this collision event.
*/
onCollisionEnter(other: Collision): void {}

/**
* OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
* @param other The Collision data associated with this collision event.
*/
onCollisionStay(other: Collision): void {}

/**
* OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
* @param other The Collision data associated with this collision event.
*/
onCollisionExit(other: Collision): void {}

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

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

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

/**
* Called when the pointer is down while over the Collider.
Expand Down
Loading