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: modify physics-related class for editor #547

Merged
merged 3 commits into from
Oct 25, 2021
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
1 change: 1 addition & 0 deletions packages/core/src/physics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export { SphereColliderShape } from "./shape/SphereColliderShape";
export { PlaneColliderShape } from "./shape/PlaneColliderShape";
export { CapsuleColliderShape } from "./shape/CapsuleColliderShape";

export { Collider } from "./Collider";
export { StaticCollider } from "./StaticCollider";
export { DynamicCollider } from "./DynamicCollider";
6 changes: 5 additions & 1 deletion packages/loader/src/scene-loader/AbilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { pluginHook } from "./plugins/PluginManager";
import { scriptAbility } from "./resources";
import { ComponentConfig, Props } from "./types";
import { switchElementsIndex } from "./utils";
import { colliderConfigure } from "./ColliderConfigure";

export class AbilityManager {
private abilityMap: { [id: string]: Component } = {};

Expand All @@ -18,7 +20,7 @@ export class AbilityManager {
const node = this.oasis.nodeManager.get(nodeId);
const AbilityConstructor = this.getCompConstructor(type);
if (!AbilityConstructor) {
Logger.error(`${type} abiltiy is not defined`);
Logger.error(`${type} ability is not defined`);
return;
}

Expand All @@ -38,6 +40,8 @@ export class AbilityManager {
if (abilityProps.material) {
(ability as any).material = abilityProps.material;
}
} else if (type === "StaticCollider" || type === "DynamicCollider") {
colliderConfigure(ability as any, abilityProps);
} else {
for (let k in abilityProps) {
if (abilityProps[k] !== null) {
Expand Down
80 changes: 80 additions & 0 deletions packages/loader/src/scene-loader/ColliderConfigure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
BoxColliderShape,
CapsuleColliderShape,
Collider,
ColliderShapeUpAxis,
DynamicCollider,
PlaneColliderShape,
SphereColliderShape
} from "@oasis-engine/core";
import { Vector3 } from "@oasis-engine/math";

// 根据Schema构造Component
export function colliderConfigure(collider: Collider, props: any) {
(<any>collider).isShowCollider = props.isShowCollider;

const shapes = props.colliderShapes;
for (let i = 0; i < shapes.length; i++) {
const shape = shapes[i];
switch (shape._shapes) {
case "BoxColliderShape": {
const box = new BoxColliderShape();
shape.size && box.setSize(shape.size[0], shape.size[1], shape.size[2]);
shape.position && box.setPosition(shape.position[0], shape.position[1], shape.position[2]);
shape.isTrigger && (box.isTrigger = shape.isTrigger);
collider.addShape(box);
break;
}
case "CapsuleColliderShape": {
const capsule = new CapsuleColliderShape();
shape.radius && (capsule.radius = shape.radius);
shape.height && (capsule.height = shape.height);
if (shape.upAxis) {
switch (shape.upAxis) {
case "X":
capsule.upAxis = ColliderShapeUpAxis.X;
break;
case "Y":
capsule.upAxis = ColliderShapeUpAxis.Y;
break;
case "Z":
capsule.upAxis = ColliderShapeUpAxis.Z;
break;
}
}
shape.position && capsule.setPosition(shape.position[0], shape.position[1], shape.position[2]);
shape.isTrigger && (capsule.isTrigger = shape.isTrigger);
collider.addShape(capsule);
break;
}
case "PlaneColliderShape": {
const plane = new PlaneColliderShape();
shape.rotation && plane.setRotation(shape.rotation[0], shape.rotation[1], shape.rotation[2]);
shape.position && plane.setPosition(shape.position[0], shape.position[1], shape.position[2]);
shape.isTrigger && (plane.isTrigger = shape.isTrigger);
collider.addShape(plane);
break;
}
case "SphereColliderShape": {
const sphere = new SphereColliderShape();
shape.radius && (sphere.radius = shape.radius);
shape.position && sphere.setPosition(shape.position[0], shape.position[1], shape.position[2]);
shape.isTrigger && (sphere.isTrigger = shape.isTrigger);
collider.addShape(sphere);
break;
}
}
}

if (collider instanceof DynamicCollider) {
const force = props.force;
if (force) {
(<DynamicCollider>collider).applyForce(new Vector3(force[0], force[1], force[2]));
}

const torque = props.torque;
if (torque) {
(<DynamicCollider>collider).applyTorque(new Vector3(torque[0], torque[1], torque[2]));
}
}
}
6 changes: 5 additions & 1 deletion packages/oasis-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
PointLight,
SpriteRenderer,
SpriteMask,
Animator
Animator,
StaticCollider,
DynamicCollider
} from "@oasis-engine/core";
import { GLTFModel, Parser, Model } from "@oasis-engine/loader";

Expand All @@ -26,6 +28,8 @@ Parser.registerComponents("o3", {
Camera,
Model,
Component,
StaticCollider,
DynamicCollider,
Animator
});

Expand Down