[SOLVED] raycastFirst problem

I notice that this.app.systems.rigidbody.raycastFirst also responds to a collider without a rigidbody. How is that possible?

@Albertos it’s because the function looks for a collision component.

Ah thats bad! So where is that .rigidbody for?

Is there a way to ignore a collision component, or using layers?

My bad. Didn’t really say it properly. Confused it with smtg else, really sorry. Here https://developer.playcanvas.com/en/user-manual/physics/ray-casting/

This should explain it. But I’ve taken a look on Github, it does look for a collision component.

Sorry, but I don’t see a answer on my questions on that page.

I imagine it might work with physics layers, but I don’t have an answer on that. Maybe someone else that has done it can contribute to this thread.

As an alternative, if you are using simple shapes (box or sphere) and you want control over the list of items you are raycasting on you can use the pc.BoundingBox and pc.BoundingSphere classes. Those classes have to be setup manually in code and don’t take part in the physics simulation (good for performance as well). Here is an example on how to use them:

https://developer.playcanvas.com/en/tutorials/entity-picking-without-physics/

I use rays to detect obstacles in front of the player (and enemies). The problem is that it detect a non-obstacle object (like a trigger) to.

You can either setup a system of bounding boxes per obstacle/enemies and use something similar to the example I posted. It will work and detect only entities that you pass to the raycasting loop.

But if you have already a physics simulation in place and use rigidbodies it makes sense to use the system you have in place and try physics layers. Sorry I can’t help you on that though.

Maybe it’s a good option. I go to look how to do it. I think this option is not possible in combination with raycastFirst?

Yes, you will be using a different method to raycast through bounding boxes:

https://developer.playcanvas.com/en/api/pc.BoundingBox.html#intersectsRay

Is this the same layer system as i know from @yaustar? (Ammo.js).

No, that is something different, this is coming directly from the Playcanvas engine and allows you to create simple shapes (box or sphere) and detect if they intersect or cast a ray from a point towards them.

You will have to create them in code and also update their position to match the entity they should be monitoring. The example is quite good I think to what you want to achieve (casting a ray against a list of shapes).

You mean https://playcanvas.com/project/436809/overview/entity-picking-without-physics?

Yes, it uses the bounding box/sphere classes to do raycasting and check if there is a shape found.

I have to change a lot in code but thats no problem.

    this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction); 
    this.ray.origin.copy(this.cameraEntity.getPosition());
    this.ray.direction.sub(this.ray.origin).normalize();

How do i set this with the player entity as orgin and a specific direction and length as direction?

Something like this, I think will work, where this.playerEntity is your player’s entity.

    this.ray.origin.copy(this.playerEntity.getPosition());
    this.ray.direction.copy(this.playerEntity.forward);

The length of the ray will be infinite but if you would like it to search for entities up to a distance, you should filter your list of shapes you will be raycasting on by distance.

So if an object has been found as a result of the raycast, check the distance from the ray origin, if it is more than your threshold then discard it.

The pc.Vec3 class has recently got a useful method for this:

https://developer.playcanvas.com/en/api/pc.Vec3.html#distance

If you can set an entity as origin maybe you can set an entity as direction as well? That will make it a little bit easier. (But it will also be infinite i’m affraid?)

The pc.Ray class used to to define the ray used requires both an origin and a direction, which aren’t the same thing.

https://developer.playcanvas.com/en/api/pc.Ray.html

To make it non infinite, you can use what I wrote above and check the distance from any object that is found.

Sorry but the information is confusing me. Is it the same as i do now?
this.toFront.copy(this.fromFront.forward).scale(0.6).add(this.fromFront.getPosition());

When you say is it the same, what do you mean, which two things are the same?