Files
cocos4/tests/physics/sweep.ts
T
Ling Zhan 1032bd9508 Sweep Test (#15068)
* init

# Conflicts:
#	cocos/physics/physx/physx-adapter.ts

# Conflicts:
#	cocos/physics/physx/physx-adapter.ts

* init sweepBoxClosest

# Conflicts:
#	cocos/physics/physx/physx-world.ts

* back

* physx boxCastClosest works now.

* add physx sweepAll

* init bullet sweepBoxClosest. not test yet

* add bullet sweepBox

* bullet add internal functions: sweep/sweepClosest

* init physx c++ sweepBox/sweepBoxClosest

* add sweepSphere, sweepCapsule; add unit test.

* add sweep api doc

* revert physx asm/wasm loading func

* tweaks

* add error log for builtin and cannon.js

* builtin and cannon.js use warnid when calling sweep

* tweak api doc
2023-05-17 18:37:03 +08:00

53 lines
1.9 KiB
TypeScript

import { geometry, Quat, Vec3 } from "../../cocos/core";
import { physics } from "../../exports/physics-framework";
import { Node } from "../../cocos/scene-graph";
import { director } from "../../cocos/game";
/**
* This function is used to test the raycast
*/
export default function (parent: Node) {
//skip builtin and cannon.js for now
if (physics.selector.id === 'builtin' || physics.selector.id === 'cannon.js')
return;
const nodeSphere = new Node('sphere');
const sphere = nodeSphere.addComponent(physics.SphereCollider) as physics.SphereCollider;
parent.addChild(nodeSphere);
nodeSphere.worldPosition = new Vec3(10, 0, 0);
sphere.setGroup(physics.PhysicsGroup.DEFAULT);
const nodeBox = new Node('box');
const box = nodeBox.addComponent(physics.BoxCollider) as physics.BoxCollider;
parent.addChild(nodeBox);
nodeBox.worldPosition = new Vec3(5, 0, 0);
box.setGroup(physics.PhysicsGroup.DEFAULT);
let isHit = false;
const ray_t = new geometry.Ray(0,0,0,1,0,0);
director.tick(physics.PhysicsSystem.instance.fixedTimeStep);
// all test
{
const hits = physics.PhysicsSystem.instance.sweepCastResults;
const boxHalfExtents = new Vec3(0.5, 0.5, 0.5);
const orientation = new Quat();
isHit = physics.PhysicsSystem.instance.sweepBox(ray_t, boxHalfExtents, orientation);
expect(isHit).toBe(true);
expect(hits.length).toBe(2);
}
// closest test
{
const hitClosest = physics.PhysicsSystem.instance.sweepCastClosestResult;
const boxHalfExtents = new Vec3(0.5, 0.5, 0.5);
const orientation = new Quat();
isHit = physics.PhysicsSystem.instance.sweepBoxClosest(ray_t, boxHalfExtents, orientation);
expect(isHit).toBe(true);
expect(hitClosest.collider.uuid).toBe(box.uuid);
}
parent.destroyAllChildren();
parent.removeAllChildren();
}