[SOLVED] raycastFirst problem

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?

Good question! I actually don’t know that either. But i have no clue how to implant this:

distance(rhs)

Returns the distance between the two specified 3-dimensional vectors.

var v1 = new pc.Vec3(5, 10, 20);
var v2 = new pc.Vec3(10, 20, 40);
var d = v1.distance(v2);
console.log("The between v1 and v2 is: " + d);

Can you give me a single example of a ray with a origin, distance and direction (like 45 degrees)? If that is to much code it means this is not a good solution for my game because i use a lot of rays.

The example code you posted from the docs is straightforward on how to calculate the distance between two points.

But, yes if you aren’t feeling strong with vectors/positions and how to manipulate them I’d say either study some 3D maths / vector course online first or stick with physics and what you are currently doing, finding an alternative to solve your issue.

It takes time and patience to understand the maths of 3D space, though I definitely recommend doing that at some point. It will polish your skills and what you are capable of doing.

I can’t do much with such an answer. Sorry. Thanks anyway.

1 Like

I managed to use the same scripts to solve this problem to.

1 Like