Files
cocos-engine/tests/physics/characterController.ts
Ling Zhan 9cd5212d18 Character Controller (#14848)
* back.boxCCT works now. capsuleCCT not collider correctly

* refactor

* cct 事件系统初步可用。todo 碰撞filter

* init cct group and mask

* queryFilterCB

* this the first working version

* tweaks

* init bullet character controller. todo: events, autoStep bug, maxSlope bug

* bullet cct onShapehit

* physx native works now. todo: BoxCharacterController

* add physx-native BoxCharacterController

* cct filters redundant events

* cct velocity

* cct detectCollision not working

* cct detectCollisions works in physx

* Character controller supports setDetectCollisions: collision detection without calling move()

* cct center

* bt.CharacterController_setContactOffset,CharacterController_setStepOffset,CharacterController_setSlopeLimit

* bullet cct collision event use last shoot

* tweaks

* cct doc

* fix bullet box-cct hit callback

* add cct export

* feat cct setOverlapRecovery

* cct delete useless code in native physics

* tweaks: cct remove useless code

* fix cct bullet group mask: update group and mask by re-adding cct to physics world. not tested

* tweaks

* tweaks

* fix bullet-cct not emit collision: add bullet updateNeedEmitCCTEvents

* tweaks

* fix cct supports enable/disable

* add cct imp null check

* cct not to expose detectCollisions and OverlapRecovery at this moment

* physx/bullet cct支持节点缩放.todo: bullet cct(setRadius/Height)还有bug 无法运行时改变,只能在初始化的时候设定。physx c++待支持。

* c++ physx cct supports node scale

* fix cct physics c++ forgets to remove cct from scene when destroyed

* [fix] physx cct set climbing mode to PxCapsuleClimbingMode.eCONSTRAINED

* add cct unit test and multiple tweaks

* multiple tweaks and add cct center for physx c++

* tweak

* tweaks

* tweaks

* external version: develop-20

* tweaks
2023-05-16 10:03:13 +08:00

97 lines
3.2 KiB
TypeScript

import { Quat, Vec3 } from "../../cocos/core";
import { director } from "../../cocos/game";
import { Node } from "../../cocos/scene-graph";
import { ICollisionEvent, physics } from "../../exports/physics-framework";
/**
* 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.contactOffset = 0.01;
expect(cct.contactOffset === 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.setPosition(targetPos);
let newPos = new Vec3();
cct.getPosition(newPos);
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();
cct.getPosition(newPos);
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();
cct.getPosition(newPos);
expect(Vec3.equals(newPos, targetPos)).toBe(true);
}
}
export default function (parent: Node, _steps = 0) {
//skip builtin and cannon.js for now
if (physics.selector.id === 'builtin' || physics.selector.id === 'cannon.js')
return;
//box character controller
{
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);
parent.destroyAllChildren();
parent.removeAllChildren();
}
//capsule character controller
{
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);
parent.destroyAllChildren();
parent.removeAllChildren();
}
}