Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raycast ignores RigidBody's children when colliding with bodies #83732

Closed
Xstyler85 opened this issue Oct 21, 2023 · 16 comments · Fixed by #83735
Closed

Raycast ignores RigidBody's children when colliding with bodies #83732

Xstyler85 opened this issue Oct 21, 2023 · 16 comments · Fixed by #83735

Comments

@Xstyler85
Copy link

Godot version

4.1.1

System information

WIndows 11

Issue description

I have a rigid body with multiple child nodes - each od them has mash and collider.
I am using a raycast to detect a child inside that rigid body.
But, it only detects the rigid body (parent) and ignores its children.
I tried to change raycast collision mask and children’s collision layer, but it didn’t work - it works if I set “Collide with: Areas”, but it doesn’t work on “Collide with: Bodies”. I think that is a bug.

Steps to reproduce

  1. Open attached project
  2. Run the game and nothing will be printed in the console (children ignored)
  3. On player raycast switch "Collide with: Bodies" to "Collide with: Areas"
  4. Run the game and child area name with be printed (children not ignored)

Minimal reproduction project

CollisionIssue.zip

@AThousandShips

This comment was marked as off-topic.

@AThousandShips

This comment was marked as off-topic.

@Xstyler85
Copy link
Author

This is not a problem, collider is directly under the RigidBody3D. The issue is that raycast is not detecting it, but can only detect Areas.

@AThousandShips
Copy link
Member

AThousandShips commented Oct 21, 2023

Does it work if you change collision_layer of the RigidBody to 1? Or change the collision_mask of the ray cast to 2?

The collision_layer of RigidBody3D is set to 2, but the collision_mask of the RayCast3D is set to the default of 1. They have to match, even though you said you tested changing them your PR does have unmatching layers

@AThousandShips
Copy link
Member

After updating the layers to be correct it works fine for me

@Xstyler85
Copy link
Author

I tried differant combinations. If they metch like you wrote the RigidBody is detected.
But, I'm trying to detect it's children (Child1, Child2). If I match Raycast mask with children layers it still doesn't work.

@AThousandShips
Copy link
Member

AThousandShips commented Oct 21, 2023

What children layers? The detection is the body, not the collision shapes. You can access the shape index, but the target is the body, not the shape

See the documentation, it states that it collides with a CollisionObject3D, not the shapes

However to get access to the shape you can do:

var target = raycast.get_collider()
var shape_id = raycast.get_collider_shape()
var shape = target.shape_owner_get_owner(shape_id)

@AThousandShips
Copy link
Member

The specifics here could be documented more clearly, but there's no bug here

@Xstyler85
Copy link
Author

Ok thank you! The code you provided is working, so it's not a bug. I didn't know that CollisionShape3D is not recognized as a body. Can I add any other node inside the RigidBody that will be recognized as a body?

@AThousandShips
Copy link
Member

You can, but it kind of defeats the purpose, you are colliding with bodies, will make a documentation update to help with this.

@Xstyler85
Copy link
Author

So, for example if I'm shooting RigidBody3D car and I want to know that I hit the "door", not just a "car" I have to use your 3 lines of code and there is no other simpler way? Sorry I'm coming from Unity3D, maybe I'm thinking too much from the Unity user prespective :)

@AThousandShips
Copy link
Member

AThousandShips commented Oct 21, 2023

That's the simple way :)

We could add a method to the ray cast to access this directly, but it's very specific use, most of the time you'd use the physics server part to do ray-casts

@AThousandShips
Copy link
Member

You can open a proposal if one isn't opened already to suggest adding this method to RayCast2D/3D as a convenience

@AThousandShips
Copy link
Member

Correction, the full code is:

var target = raycast.get_collider()
var shape_id = raycast.get_collider_shape()
var owner_id = target.shape_find_owner(shape_id)
var shape = target.shape_owner_get_owner(owner_id)

largely the same I believe, but to be fully clear

@Xstyler85
Copy link
Author

Great, thank you again for your time and all the help!
I apologize for misunderstanding and reporting it as a bug.

@AThousandShips
Copy link
Member

No worries! It brought to attention the limited documentation

@AThousandShips AThousandShips modified the milestones: 4.2, 4.3 Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants