I found that using Octree would penetrate inside TubeGeometry

for example file games_fps.html.
I created a TubeGeometry. and set worldOctree.fromGraphNode()

This is the code I added

// other code
container.appendChild( stats.domElement );

// line 87
let points = [new THREE.Vector3(0, 0, 0 ), new THREE.Vector3(10, 0, 0), new THREE.Vector3(30, 0, 2)]
let curvePoints = new THREE.CatmullRomCurve3(points)

const tubeGeometry = new THREE.TubeGeometry(curvePoints, 64, 2, 8, false)
const tubeMaterial = new THREE.MeshBasicMaterial({
    color: 0xffffff,
    side: THREE.DoubleSide 

const tubeMesh = new THREE.Mesh(tubeGeometry, tubeMaterial)

const GRAVITY = 30;
// other code

// other code
loader.load('collision-world.glb', (gltf) => {
   scene.add( gltf.scene );
   worldOctree.fromGraphNode( gltf.scene );

Actually, when I am inside the tube, it can pass through the tube and when I press A or D.

May I ask if this is the correct expectation。

I have no idea what is your expectation. Octrees do not represent a shape precisely, they are generally used to split the space into smaller fragments, so that searching/raycasting/processing becomes much faster.

My suggestion is to visualize the octree and see whether it matches your expectation.

import { OctreeHelper } from "three/addons/helpers/OctreeHelper.js";

// other code

loader.load('collision-world.glb', (gltf) => {
   // other code
   scene.add( new OctreeHelper( worldOctree, 'crimson' ) ); // octree visualization

In the video I uploaded, you can see that you can pass through it while moving.

If I want to stay inside the tubegeometry without passing through, how do I achieve it.

I have tried using canon.js, but it cannot fully handle my geometry.

One way to do this is with raytracing. You have an invisible short ray, and you move in the direction of the ray only when there is no obstacle. I tried with a tube geometry and this approach works fine from both outside and inside the tube. Most likely there is some bug in your implementation. WIth proper debugging and tracing you may find it.

Also, I believe octree can speed up raycast intersection. I have never used it, so the video above is with raycasting without octrees.

1 Like

I have written three implementation methods (Octree, Raycaster, CANNON), among which Raycaster is also implemented because I need to set the movement speed. When I use Raycaster, if the speed is too fast, it will also pass through。

I will share the editable code later

i have to same problem, when i shoot the ball with high velocity, the ball pass through the object (raycasting)
but i wanna use Octree, i don’t know is it good for large scale project or not?

Octree can work pretty reliably… especially if you are raycasting your projectile paths. If you are relying on the sphere shapecasting, then you will have to limit the per frame velocity of the sphere to half it’s radius or less, in order to not miss collisions.

If you instead, use raycasting to cast trajectories, then you can get robust collision because you’re casting from previous position to current position… but then you lose the radius of the sphere… because the projectile is treated as a ray.

You might be able to shapecast the capsule shape formed by the spheres previous → current position to get something completely robust.

Here’s a picture describing these scenarios… the top row is how you’re collisions are (not) working right now… the sphere is moving fast enough that it goes past the collider…

The second row is what I believe pavel is describing… using a raycast… the raycast will catch the collision but then you need to back up along the ray to find where to place the sphere… The ray will also miss collisions if the ray itself doesn’t hit the wall… so you may not get the sphere hitting corners…

the last row is what I mean by casting the capsule shape… this should truly catch the moving sphere collision, but then you’ll have to figure out where to place the sphere.

These problems aren’t specific to the octree physics in the fps demo, but pretty much all physics engines will fail in one of these ways, unless you use what is called “CCD” or continuous collision detection… which basically does the capsule shape cast like i show in the bottom row…
Most physics engines have a substep parameter than you can adjust until your projectile motion on a given substep is small enough that you don’t miss collisions with your max projectile velocity… but more substeps results in more computation so it’s a balancing act.