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: non-trigger script and dynamic collider #680

Merged
merged 4 commits into from
Mar 10, 2022
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: 19 additions & 0 deletions packages/core/src/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ export class Script extends Component {
*/
onTriggerStay(other: ColliderShape): void {}

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

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

/**
* Called when the collision exit.
* @param other ColliderShape
*/
onCollisionStay(other: ColliderShape): void {}

/**
* Called when the pointer is down while over the ColliderShape.
*/
Expand Down
278 changes: 264 additions & 14 deletions packages/core/src/physics/DynamicCollider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,203 @@ import { IDynamicCollider } from "@oasis-engine/design";
import { Entity } from "../Entity";
import { Collider } from "./Collider";
import { PhysicsManager } from "./PhysicsManager";
import { Vector3 } from "@oasis-engine/math";
import { Vector3, Quaternion } from "@oasis-engine/math";

/**
* A dynamic collider can act with self-defined movement or physical force.
*/
export class DynamicCollider extends Collider {
/** The linear velocity vector of the dynamic collider measured in world unit per second. */
linearVelocity: Vector3;
/** The angular velocity vector of the dynamic collider measured in radians per second. */
angularVelocity: Vector3;
/** The linear damping of the dynamic collider. */
linearDamping: number;
/** The angular damping of the dynamic collider. */
angularDamping: number;
/** The mass of the dynamic collider. */
mass: number;
/** Controls whether physics affects the dynamic collider. */
isKinematic: boolean;
private _linearDamping: number = 0;
private _angularDamping: number = 0;
private _linearVelocity = new Vector3();
private _angularVelocity = new Vector3();
private _mass: number = 0;
private _centerOfMass = new Vector3();
private _inertiaTensor = new Vector3();
private _maxAngularVelocity: number = 0;
private _maxDepenetrationVelocity: number = 0;
private _sleepThreshold: number = 0;
private _solverIterations: number = 0;
private _isKinematic: boolean = false;
private _freezeRotation: boolean = false;
private _constraints: DynamicColliderConstraints = 0;
private _collisionDetectionMode: CollisionDetectionMode = CollisionDetectionMode.Discrete;

/**
* The linear damping of the dynamic collider.
*/
get linearDamping(): number {
return this._linearDamping;
}

set linearDamping(value: number) {
this._linearDamping = value;
(<IDynamicCollider>this._nativeCollider).setLinearDamping(value);
}

/**
* The angular damping of the dynamic collider.
*/
get angularDamping(): number {
return this._angularDamping;
}

set angularDamping(value: number) {
this._angularDamping = value;
(<IDynamicCollider>this._nativeCollider).setAngularDamping(value);
}

/**
* The linear velocity vector of the dynamic collider measured in world unit per second.
*/
get linearVelocity(): Vector3 {
return this._linearVelocity;
}

set linearVelocity(value: Vector3) {
if (this._linearVelocity !== value) {
value.cloneTo(this._linearVelocity);
}
(<IDynamicCollider>this._nativeCollider).setLinearVelocity(this._linearVelocity);
}

/**
* The angular velocity vector of the dynamic collider measured in radians per second.
*/
get angularVelocity(): Vector3 {
return this._angularVelocity;
}

set angularVelocity(value: Vector3) {
if (this._angularVelocity !== value) {
value.cloneTo(this._angularVelocity);
}
(<IDynamicCollider>this._nativeCollider).setAngularVelocity(this._angularVelocity);
}

/**
* The mass of the dynamic collider.
*/
get mass(): number {
return this._mass;
}

set mass(value: number) {
this._mass = value;
(<IDynamicCollider>this._nativeCollider).setMass(value);
}
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

/**
* The center of mass relative to the transform's origin.
*/
get centerOfMass(): Vector3 {
return this._centerOfMass;
}

set centerOfMass(value: Vector3) {
if (this._centerOfMass !== value) {
value.cloneTo(this._centerOfMass);
}
(<IDynamicCollider>this._nativeCollider).setCenterOfMass(this._centerOfMass);
}

/**
* The diagonal inertia tensor of mass relative to the center of mass.
*/
get inertiaTensor(): Vector3 {
return this._inertiaTensor;
}

set inertiaTensor(value: Vector3) {
if (this._inertiaTensor !== value) {
value.cloneTo(this._inertiaTensor);
}
(<IDynamicCollider>this._nativeCollider).setInertiaTensor(this._inertiaTensor);
}

/**
* The maximum angular velocity of the collider measured in radians per second. (Default 7) range { 0, infinity }.
*/
get maxAngularVelocity(): number {
return this._maxAngularVelocity;
}

set maxAngularVelocity(value: number) {
this._maxAngularVelocity = value;
(<IDynamicCollider>this._nativeCollider).setMaxAngularVelocity(value);
}

/**
* Maximum velocity of a collider when moving out of penetrating state.
*/
get maxDepenetrationVelocity(): number {
return this._maxDepenetrationVelocity;
}

set maxDepenetrationVelocity(value: number) {
this._maxDepenetrationVelocity = value;
(<IDynamicCollider>this._nativeCollider).setMaxDepenetrationVelocity(value);
}

/**
* The mass-normalized energy threshold, below which objects start going to sleep.
*/
get sleepThreshold(): number {
return this._sleepThreshold;
}

set sleepThreshold(value: number) {
this._sleepThreshold = value;
(<IDynamicCollider>this._nativeCollider).setSleepThreshold(value);
}

/**
* The solverIterations determines how accurately collider joints and collision contacts are resolved.
*/
get solverIterations(): number {
return this._solverIterations;
}

set solverIterations(value: number) {
this._solverIterations = value;
(<IDynamicCollider>this._nativeCollider).setSolverIterations(value);
}

/**
* Controls whether physics affects the dynamic collider.
*/
get isKinematic(): boolean {
return this._isKinematic;
}

set isKinematic(value: boolean) {
this._isKinematic = value;
(<IDynamicCollider>this._nativeCollider).setIsKinematic(value);
}
yangfengzzz marked this conversation as resolved.
Show resolved Hide resolved

/**
* The particular rigid dynamic lock flag.
*/
get constraints(): DynamicColliderConstraints {
return this._constraints;
}

set constraints(value: DynamicColliderConstraints) {
this._constraints = value;
(<IDynamicCollider>this._nativeCollider).setConstraints(value);
}

/**
* The colliders' collision detection mode.
*/
get collisionDetectionMode(): CollisionDetectionMode {
return this._collisionDetectionMode;
}

set collisionDetectionMode(value: CollisionDetectionMode) {
this._collisionDetectionMode = value;
(<IDynamicCollider>this._nativeCollider).setCollisionDetectionMode(value);
}

constructor(entity: Entity) {
super(entity);
Expand All @@ -46,11 +225,48 @@ export class DynamicCollider extends Collider {
(<IDynamicCollider>this._nativeCollider).addTorque(torque);
}

/**
* Moves kinematically controlled dynamic actors through the game world.
* @param position The desired position for the kinematic actor
*/
move(position: Vector3): void;

/**
* Moves kinematically controlled dynamic actors through the game world.
* @param rotation The desired rotation for the kinematic actor
*/
move(rotation: Quaternion): void;

/**
* Moves kinematically controlled dynamic actors through the game world.
* @param position The desired position for the kinematic actor
* @param rotation The desired rotation for the kinematic actor
*/
move(position: Vector3, rotation: Quaternion): void;

move(positionOrRotation: Vector3 | Quaternion, rotation?: Quaternion): void {
(<IDynamicCollider>this._nativeCollider).move(positionOrRotation, rotation);
}

/**
* Forces a collider to sleep at least one frame.
*/
sleep(): void {
(<IDynamicCollider>this._nativeCollider).sleep();
}

/**
* Forces a collider to wake up.
*/
wakeUp(): void {
(<IDynamicCollider>this._nativeCollider).wakeUp();
}

/**
* @override
* @internal
*/
_onLateUpdate() {
_onLateUpdate(): void {
const { transform } = this.entity;
const { worldPosition, worldRotationQuaternion } = transform;
this._nativeCollider.getWorldTransform(worldPosition, worldRotationQuaternion);
Expand All @@ -59,3 +275,37 @@ export class DynamicCollider extends Collider {
this._updateFlag.flag = false;
}
}

/**
* The collision detection mode constants.
*/
export enum CollisionDetectionMode {
/** Continuous collision detection is off for this dynamic collider. */
Discrete,
/** Continuous collision detection is on for colliding with static mesh geometry. */
Continuous,
/** Continuous collision detection is on for colliding with static and dynamic geometry. */
ContinuousDynamic,
/** Speculative continuous collision detection is on for static and dynamic geometries */
ContinuousSpeculative
}

/**
* Use these flags to constrain motion of dynamic collider.
*/
export enum DynamicColliderConstraints {
/** Not Freeze. */
None = 0,
/** Freeze motion along the X-axis. */
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
FreezePositionX = 1,
/** Freeze motion along the Y-axis. */
FreezePositionY = 2,
/** Freeze motion along the Z-axis. */
FreezePositionZ = 4,
/** Freeze rotation along the X-axis. */
FreezeRotationX = 8,
/** Freeze rotation along the Y-axis. */
FreezeRotationY = 16,
/** Freeze rotation along the Z-axis. */
FreezeRotationZ = 32
}
Loading