Hello! I’m facing some logic issue here.
I’m currently doing a raycast through a list of meshes (collidable list) to add some kind of effect (hitTrajectory) at the hit position. At the moment, everything is working fine. However, I noticed that the effect is only positionned on the latest object in the array that the ray collides with.
For example, the effect will always be on the ball even if a boundery is closer because the balls are the latest meshes added to the array. is there something I’m missing in my conditions? I also took a look at the documentation and the playground related to the rays but I did not find anything. Thanks!
/**
* Create a list of collidable object that any ray can collide with
*/
private createCollidableList (): void {
// Add holes to the list of collidables
this.table.getHoles().forEach((hole: Hole) => {
this.collidables.push(hole.getMesh())
})
// Add bounderies to the list of collidables
this.table.getBounderies().forEach((boundery: Boundery) => {
this.collidables.push(boundery.getMesh())
})
// Add balls to the list of collidables
this.ballManager.getBalls().forEach((ball: Ball) => {
if (ball.getBallType() !== BallType.White) {
this.collidables.push(ball.getMesh())
}
})
}
/**
* Check if a ray collide with an object contained in the collidable object list
* @param ray CustomRay
* @param hitTrajectory HitTrajectory
*/
private checkCollisionWithCollidables (ray: CustomRay, hitTrajectory: HitTrajectory): void {
this.collidables.forEach((collidable: any) => {
const hitInfo = ray.intersectsMeshes([collidable])
if (this.rayHitSomething(hitInfo)) {
// Get the first hit point
if (hitInfo[0].pickedPoint != null) {
// get dray direction
const rayVector = hitInfo[0].pickedPoint.subtract(ray.origin)
// Set hit at the hit position with a certain offset
hitTrajectory.setHitPosition(this.getPointAlongVector(ray.origin, hitInfo[0].pickedPoint, rayVector.length() - 0.35))
}
}
})
}
/**
* Check if a ray hit something
* @param hitInfo PickingInfo[]
* @returns boolean
*/
private rayHitSomething (hitInfo: PickingInfo[]): boolean {
let hitSomething = false
for (let i = 0; i < hitInfo.length; i++) {
if (hitInfo[i].pickedPoint != null) {
hitSomething = true
}
}
return hitSomething
}