Unable to raycast to mesh walls

Hi,

I’m trying to raycast from the center of a character to detect distance to walls and prevent collisions. I’ve set both the material to DoubleSide as well as updated MatrixWorld but I’m still getting an empty intersect array.

My code is below:


mesh_parent_object.updateMatrixWorld();
mesh_parent_object.material[0].side   = THREE.DoubleSide;

var raycaster 						= new THREE.Raycaster();

var vector_origin					= new THREE.Vector3(character.position.x, 0, character.position.z);
var vector_target					= new THREE.Vector3(-1.2741646766662598, character.position.y, 1.8035423755645752);	
var vector_direction 				= vector_target.clone().sub(character.position).normalize();

raycaster.set(vector_origin, vector_direction);						
var intersects 						= raycaster.intersectObject(mesh_parent_object, true);

I’ve included a screenshot showing that the raycast intersects the walls and even goes through.

Can someone please help me get the intersect values for meshes like this?

Thank You!

/cc

I don’t get how you come up with these numbers.

If you want to cast a ray along the characters forward/look direction, use:

const direction = new THREE.Vector3();
character.getWorldDirection( direction );

However, this only works if you character looks along (0,0,1) if no rotation is applied to the model.

@Mugen87 Those numbers are just a random vector I used to test to ensure that the raycaster beam worked.

If I place a simple box geometry mesh in the path of the raycaster beam then the box shows up in the intersected array.

The problem I’m facing is that for some unknown reason, the mesh wall just doesn’t show up in the intersected array results even though it should.

Why don’t you calculated distance between character and wall in animation loop.
Using raycaster to detect collision is not the right tool.

1 Like

Does that mean I have to save every single surface wall’s positions into an array or object and compare during every single character transition? In my actual live production link there’s spaces with a lot more rooms and walls than this demo example. So wouldn’t that be really slow to compute in real time?

No just the bounding box or bounding sphere. In this video alot of collision detection is happing.

Video

No ray casting no external lib except for 3JS.
You could also turn the floor to grid, like chess board or maze and move the character from one square to the next.

1 Like

In all of the examples I read, it seems that the containsPoint() function is used to check whether the bounding box intersects with a point. How can I check if the bounding box collides with any wall if I don’t have every single wall position?

Alternatively, is there anyway to check if the bounding box intersects with wireframes instead?