[SOLVED] boxCast detecting player and ground entities

Hello

I did not want to continue writing my problems inside the https://forum.playcanvas.com/t/playcanvas-physics-extension/13737 topic to not make it dirty.

I am trying to use the physics extension above to create a boxCast underneath the player which I could get it to work however there is a problem with the raycast detecting the player entity constantly.

code of the isGrounded function:

PlayerJumping.prototype.isGrounded = function () {
    let halfExtents = this.playerCollision.collision.halfExtents;
    let start = this.playerCollision.getPosition();
    let end = this.entity.getPosition();

    this.jumpRaycastStart.copy(start);
    this.jumpRaycastEnd.copy(end);

    // make adjustment to start and end points
    //this.jumpRaycastStart.y += -;
    this.jumpRaycastEnd.y += 0;

    const result = this.app.systems.rigidbody.boxCast(halfExtents, this.jumpRaycastStart, this.jumpRaycastEnd);

    let colliding = false;

    if (result && result.entity.rigidbody && result.entity !== this.entity) {
        colliding = true;
    }
    else {
        colliding = false;
        //return;
    }

    console.log(result.entity.name);
    console.log(result);

    // debug
    this.app.drawLine(this.jumpRaycastStart, this.jumpRaycastEnd, colliding ? pc.Color.GREEN : pc.Color.RED);

    return colliding;
};

As recommended in the topic of Mr LeXXik I made sure the player only jumps if the raycast result does not return the player entity which is the current entity (this.entity) but the player entity is detected by the convexcast and because of that sometimes the player is not able to jump when pressing space. I do not want the player entity being detected by the raycast. I tried to debug the issue but I do not know what I am looking for to fix this.

Below is a video of what I mean: How do I eliminate the player being detected by the convexcast which makes the player not able to jump when pressing space?

Project link: https://playcanvas.com/project/926851/overview/jumpcast

Thanks

Hi @nasjarta! If the extension is like raycastFirst, your result will be unreliable if you want to filter the player entity, because it only checks for the first collision (even if the result is the player entity). What you might do is execute the ground check again if the result is the player entity.

2 Likes

i recommend doing result.entity.name !== "player_name"; instead of doing result.entity !== this.entity, this is the case because result.entity returns a object not a string. so its pretty much hard to filter by just doing the entity

1 Like

Just for my own knowledge; is the result different?

1 Like

im pretty sure i gotta test that out but i always use the name

1 Like

ok so nevermind the result is not different its actually the same

2 Likes

It works fine when I use raycastFirst but the same logic does not work for boxCast

Tested this already also but its the same

I think only the size is different?

Do you understand my suggestion?

1 Like

Thanks I tried this and the same issue happened

So instead of returning a result you execute the function again until the result is not the player entity?

if (result.entity === this.entity) {
    this.isGrounded();
    return;
}
1 Like

I tested my suggestion myself and I get an Maximum call stack size exeeded error with this. Not sure why, but you can skip the suggestion.

Apart from that, I don’t really understand why your player entity is detected at all. I tried also raycastFirst and raycastAll with your setup and with this it works indeed fine. I hope @LeXXik knows the answer.

1 Like

I will do some more debug today hope I can fix it or find out why it is detecting the player entity.

Right, so in order to avoid the player rigidbody, you should use filter masks. I didn’t add support for it in the original script, but you can do it like this:

Edit convex-cast.js:

// after this line
var convexCallback = new Ammo.ClosestConvexResultCallback(data.ammoPosFrom, data.ammoPosTo);
// add this one:
convexCallback.set_m_collisionFilterMask(pc.BODYMASK_ALL ^ pc.BODYGROUP_USER_1);

This will do a sweep test against all bodies in the physics world, except those that belong to user group 1.

And somewhere in your script, update your player rigidbody by assigning it to the first user group:

playerEntity.rigidbody.group = pc.BODYGROUP_USER_1;

Your convex cast should ignore the player capsule now, as it belongs to pc.BODYGROUP_USER_1.

3 Likes

Thank you mr LeXXik!!!

The filter mask is very useful to exclude certain entities not being detected by the convex cast.

Would anybody know is it possible to get the individual sides of a collider (e.g. cube)? I want to include them in the filter as well? Example of what I mean:

12121

Thanks

You can add a child entity with a thin collision component on top.

2 Likes

Hi Albertos

unfortunately I test that already it detects even collisions that are 0 on y axis. For example in the above cube the red filter areas make it multi-jump even when the collision is thin. I am trying to find a way to filter out the other sides of the cube apart from the top side.

Sorry, I don’t understand what you try to achieve. Red part is with filter so no ground detection here? Green part is without filter so ground detection here?

2 Likes

yes so when player touches the red parts the raycast does not detect it so they cannot jump, but can jump when touching the green side

That is not possible if you assigned the player layer to this entity.

Not sure what you mean with this.

What about making the boxCast a little smaller than the actual collision component of the player? Then it’s almost impossible for the boxCast to have a collision with the sides of the box.

I put a thin collider (plane) on top of the cube and added the cube to the filter so only the plane is detected by the raycast. But once you are next to the cube on the side and spam jump the raycast still detects the plane collision and you can multi-jump:

Tried this also before but the problem is the boxcast will not detect some parts as it is smaller:

1212

Because this topic is solved already this one can be closed thanks everybody for their help.