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

Bepu Physics - Raycast does not intercept Entity with BodyComponent #2649

Open
luca-domenichini opened this issue Feb 21, 2025 · 11 comments · May be fixed by #2650
Open

Bepu Physics - Raycast does not intercept Entity with BodyComponent #2649

luca-domenichini opened this issue Feb 21, 2025 · 11 comments · May be fixed by #2650
Labels
bug Something isn't working

Comments

@luca-domenichini
Copy link

Release Type: Official Release 4.2.0.2293

Platform(s): Windows

Describe the bug
I have a sample project where I have 2 spheres, using Bepu Physics.
The first one, on the right with gray metal material, has a StaticComponent and a Sphere Collider.
The second one, on the left with golden metal material, has a BodyComponent flagged as Kinematic and a Sphere Collider as well.

I tweaked the default BasicCameraController.cs created by default Game Studio to add a raycast in order to display the hovering entity by using DebugText.Print() method.
✔When hovering on the "Static" sphere, even when it is moved away from its initial position, is working well.
❌When hovering on the "Body" sphere, raycasting returns that entity only when the sphere is in its initial position. If the user left clicks on the screen, the sphere moves to that point and raycasting stops working.

Default initial position:

Image

After moving "Body" sphere away from initial position raycast does not intercept the sphere anymore ("hovering on Ground" tells me that):

Image

If I return the sphere back to origin, raycast starts working ok again.

Raycasting on "Static" sphere always works ok.

To Reproduce
Clone repro repo here https://github.com/luca-domenichini/StrideRaycastIssueRepro

My doubt is that, maybe, I am not moving the "body sphere" the right way.
To move that sphere I am using a MoverScript attached to Ground entity.
In that script I set BodyComponent::Position property instead of the usual Entity.Transform.Position.

This is the line of code where "lerping" the position:
https://github.com/luca-domenichini/StrideRaycastIssueRepro/blob/bcf1c70ceb8cf1ace3fc30bc0fbd856b9765bc38/StrideRaycastIssue/MoverScript.cs#L73

Expected behavior
Raycast should work fine on moved BodyComponent entities.

Additional context
I also tried to move the sphere by setting Entity.Transform.Position, but that seems to be worse since the Entity is moving but position of the BodyComponent stands still at (0, 0.5, 1) insted of following the Entity.. but maybe I am missing something..

Image

@luca-domenichini luca-domenichini added the bug Something isn't working label Feb 21, 2025
@luca-domenichini luca-domenichini changed the title Bepu Physics with BodyComponent missing RayCast when positioning entity Bepu Physics - Raycast does not intercept Entity with BodyComponent Feb 21, 2025
@luca-domenichini
Copy link
Author

Thanks for your answer.
I added Stride.CommunityToolkit.Bepu to csproj, but unfortunately I get the same result:

On default position it works ok:
Image

After moving, same results:
Image

I updated my repro repo to include that.

@luca-domenichini
Copy link
Author

luca-domenichini commented Feb 21, 2025

@VaclavElias I see in your examples that you move BodyComponent by setting AngularVelocity and LinearVelocity.
I also asked that myself and tried to move the sphere that way, but I would really need to set the position as I did.

Also, setting Kinematic = false does not change things.

Is it possible that the issue manifests only when directly positioning the entity?

@VaclavElias
Copy link
Contributor

It should work also your way, but we just need to figure out how to do it. The other thing in my mind is Awake 🤔

@luca-domenichini
Copy link
Author

luca-domenichini commented Feb 21, 2025

Oh yeaaah!! That was it!

✔Setting Awake = true in Update callback of a script, solved that.

❌If I set Awake = true in Start method it does not work indeed. Is it supposed to be like that?

@VaclavElias
Copy link
Contributor

Not sure, we need to learn also how to use Awake, probably adding also some tips to our docs. If I am correct, once the body stops moving, Awake is set to false. Probably, while your sphere is moving the mouse hover might work in your example without Awake = true. Once it stops it doesn't work, unless you awake it again.

Can I use some of your code in our toolkit examples?

@luca-domenichini
Copy link
Author

luca-domenichini commented Feb 21, 2025

Offcourse, it would be a pleasure to contribute someway!

I updated my repo to include in debug output the value of Awake flag.
When body finishes moving, it goes to false and stops there.
While sphere is moving, value is true, but a small flickering can be noticed sometimes, probably indicating that after every frame value is reset to false by the engine.

Image

Probably, while your sphere is moving the mouse hover might work in your example without Awake = true. Once it stops it doesn't work, unless you awake it again.

No, this does not happen. If I dont set Awake = true, I cannot hover on the sphere. Be that standing or moving. It can only be hovered when the sphere is positioned near its initial position.

@Eideren Eideren linked a pull request Feb 22, 2025 that will close this issue
8 tasks
@Eideren
Copy link
Collaborator

Eideren commented Feb 22, 2025

@luca-domenichini could you try #2650 - I used the repro you provided and it seems to work

@Eideren
Copy link
Collaborator

Eideren commented Feb 22, 2025

Just a note, setting the position property itself is not really moving the object, it is teleporting it, the distinction is important as it disregards any obstacle that might be in the way.
The summary for that property mentions this fact:

/// <summary>
/// The position of this body in the physics scene, setting it will teleport this object to the positoin provided.
/// </summary>
/// <remarks>
/// Using this property to move objects around is not recommended,
/// as it disregards any collider that may overlap with the body at this new position,
/// you should make sure the area is clear to ensure this object does not become stuck in the scenery.
/// </remarks>
[DataMemberIgnore]
public Vector3 Position

If you're manipulating the position every frame, that's a good indicator that you should be using linear velocity instead.

@luca-domenichini
Copy link
Author

I have been using "teleporting" because I know the final position I want to reach, so "lerping" the position every frame seemed to me the obvious way-to-go.
I will try to use LinearVelocity and see if it fits my needs. 👍

@luca-domenichini
Copy link
Author

@Eideren I confirm that #2650 solves this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants