Skip to content

Commit

Permalink
Optimize physics unit test (#16262)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrinktofit authored Sep 15, 2023
1 parent f4aa7e9 commit 59c6afd
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 581 deletions.
95 changes: 95 additions & 0 deletions tests/physics/character-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Quat, Vec3 } from "../../cocos/core";
import { director } from "../../cocos/game";
import { Node } from "../../cocos/scene-graph";
import { ICollisionEvent, physics } from "../../exports/physics-framework";
import { PhysicsTestEnv } from "./physics.test";

/**
* This function is used to test the api of the CharacterController
*/
function testCharacterControllerAPIs(cct: physics.CharacterController){
cct.minMoveDistance = 0.001;
expect(cct.minMoveDistance === 0.001).toBe(true);

cct.stepOffset = 0.5;
expect(cct.stepOffset === 0.5).toBe(true);

cct.slopeLimit = 60;
expect(cct.slopeLimit === 60).toBe(true);

cct.skinWidth = 0.01;
expect(cct.skinWidth === 0.01).toBe(true);

cct.center = new Vec3(0, 0.5, 0);
expect(Vec3.equals(cct.center, new Vec3(0, 0.5, 0))).toBe(true);

let targetPos = new Vec3(0, 10, 0);
cct.centerWorldPosition = targetPos;
let newPos = new Vec3();
newPos.set(cct.centerWorldPosition);
expect(Vec3.equals(newPos, targetPos)).toBe(true);

{
cct.move(new Vec3(0, 0, 0));
const dt = physics.PhysicsSystem.instance.fixedTimeStep;
director.tick(dt);
let newPos = new Vec3();
newPos.set(cct.centerWorldPosition);
expect(Vec3.equals(newPos, targetPos)).toBe(true);
}
{
let movement = new Vec3(0, 10, 0);
Vec3.add(targetPos, targetPos, movement);
cct.move(movement);
const dt = physics.PhysicsSystem.instance.fixedTimeStep;
director.tick(dt);
let newPos = new Vec3();
newPos.set(cct.centerWorldPosition);
expect(Vec3.equals(newPos, targetPos)).toBe(true);
}
}

export default function (env: PhysicsTestEnv, _steps = 0) {
//skip builtin and cannon.js for now
if (env.backendId === 'builtin' || env.backendId === 'cannon.js')
return;

describe(`Character controller`, () => {
test(`Box character controller`, () => {
const { rootNode: parent } = env;

const nodeBoxCCT = new Node('BoxCharacterController');
parent.addChild(nodeBoxCCT);
const boxCCT = nodeBoxCCT.addComponent(physics.BoxCharacterController) as physics.BoxCharacterController;
expect(boxCCT.type).toBe(physics.ECharacterControllerType.BOX);

boxCCT.halfForwardExtent = 0.5;
expect(boxCCT.halfForwardExtent === 0.5).toBe(true);

boxCCT.halfSideExtent = 0.5;
expect(boxCCT.halfSideExtent === 0.5).toBe(true);

boxCCT.halfHeight = 0.5;
expect(boxCCT.halfHeight === 0.5).toBe(true);

testCharacterControllerAPIs(boxCCT as physics.CharacterController);
});

test(`Capsule character, controller`, () => {
const { rootNode: parent } = env;

const nodeCapsuleCCT = new Node('CapsuleCharacterController');
parent.addChild(nodeCapsuleCCT);
const capsuleCCT = nodeCapsuleCCT.addComponent(physics.CapsuleCharacterController) as physics.CapsuleCharacterController;
expect(capsuleCCT.type).toBe(physics.ECharacterControllerType.CAPSULE);

capsuleCCT.radius = 0.5;
expect(capsuleCCT.radius === 0.5).toBe(true);

capsuleCCT.height = 1;
expect(capsuleCCT.height === 1).toBe(true);

testCharacterControllerAPIs(capsuleCCT as physics.CharacterController);
});
});
}
97 changes: 0 additions & 97 deletions tests/physics/characterController.ts

This file was deleted.

15 changes: 9 additions & 6 deletions tests/physics/constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { Quat, Vec3 } from "../../cocos/core";
import { director } from "../../cocos/game";
import { Node } from "../../cocos/scene-graph";
import { ICollisionEvent, physics } from "../../exports/physics-framework";
import { PhysicsTestEnv } from "./physics.test";

function testConfigurableConstraintAPIs(child: Node) {
if (physics.selector.id === 'builtin' || physics.selector.id === 'cannon.js') {
return;
}

const cs = child.addComponent(physics.ConfigurableConstraint) as physics.ConfigurableConstraint;

{
Expand Down Expand Up @@ -126,6 +123,12 @@ function testConfigurableConstraintAPIs(child: Node) {
child.removeComponent(cs);
}

export default function (temp0: Node) {
testConfigurableConstraintAPIs(temp0);
export default function (env: PhysicsTestEnv) {
if (env.backendId === 'builtin' || env.backendId === 'cannon.js') {
return;
}

test(`Configurable constraint`, () => {
testConfigurableConstraintAPIs(env.rootNode);
});
}
Loading

0 comments on commit 59c6afd

Please sign in to comment.