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

Add initial sphere-voxel collision detection #262

Merged
merged 4 commits into from
Feb 27, 2023

Conversation

patowen
Copy link
Collaborator

@patowen patowen commented Feb 11, 2023

This PR adds collision detection logic. There is a sphere_cast method in the common::graph_collision module that answers the question of "How far can a sphere of a given radius move in a given direction, up to a given distance, before colliding with a voxel? Does it collide at all?" If there is a collision, it also finds the normal, which can be used for collision resolution.

For testing purposes, a basic form of collision resolution is also included with these changes. As long as noclip is disabled by pressing "V", if a character collides with a wall, it will stop. This has the advantage of simplicity, but it has the disadvantage of making walls "sticky", so smoother movement is planned in a future PR.

@patowen patowen requested a review from Ralith February 11, 2023 06:50
Copy link
Owner

@Ralith Ralith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial incomplete pass; more to come over coming days, but this should get things rolling.

common/src/node.rs Outdated Show resolved Hide resolved
common/src/worldgen.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/character_controller.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
common/src/ray_tracing.rs Outdated Show resolved Hide resolved
@patowen patowen force-pushed the sphere-collider branch 2 times, most recently from d27ef8e to dc9dee4 Compare February 18, 2023 18:04
@patowen
Copy link
Collaborator Author

patowen commented Feb 18, 2023

I've updated this PR and added most of the necessary tests. The main shape casting method in the collision module still needs tests, as I've saved that for last, and it might take some time. I believe there are enough new changes that it's worth a review (especially reviewing the design of the test). When I find time, I'll add the remaining missing tests.

I believe everything other than collision::shape_cast is well-covered.

EDIT: I just added tests for collision::shape_cast, so everything should now be covered, and this PR should now be ready for another review.

Copy link
Owner

@Ralith Ralith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great, thanks!

client/src/graphics/voxels/mod.rs Outdated Show resolved Hide resolved
common/src/collision.rs Outdated Show resolved Hide resolved
common/src/collision.rs Outdated Show resolved Hide resolved
common/src/collision.rs Outdated Show resolved Hide resolved
common/src/collision.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/collision.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
common/src/sphere_collider.rs Outdated Show resolved Hide resolved
@patowen
Copy link
Collaborator Author

patowen commented Feb 20, 2023

Another round of changes has been completed. This time, I have added all the tests that I believe should be added, but I am open to suggestions if you think I missed something important. I also believe all your other suggestions have been addressed.

I made a few other minor changes as well that I noticed while rereading the code. Probably the most important change is that sphere_cast now takes a Position position instead of a matrix start_node_transform and a ChunkId start_chunk. There are at least two benefits to this: (1) The caller doesn't have to awkwardly pass in an arbitrary vertex, as that is handled internally, and (2) It's much easier to explain what the position parameter means in the documentation.

Thank you for reviewing this so carefully! It's been a really helpful learning experience for me.

@patowen patowen requested a review from Ralith February 20, 2023 06:58
@patowen
Copy link
Collaborator Author

patowen commented Feb 23, 2023

@Ralith, looking at this code again, I think it might make more sense to try to make sphere_collider.rs more stand-alone. It doesn't need a ChunkId or a transformation matrix to compute anything internally; that's only used on the result. Is this something worth pursuing in this PR, or would you prefer it as a future refactor in a separate PR?

@Ralith
Copy link
Owner

Ralith commented Feb 24, 2023

That sounds like a nice improvement. I don't think it needs to block this PR, but if you get it into a state you're happy with before we merge this, then you might as well fold them together.

@patowen
Copy link
Collaborator Author

patowen commented Feb 24, 2023

That sounds like a nice improvement. I don't think it needs to block this PR, but if you get it into a state you're happy with before we merge this, then you might as well fold them together.

Sounds good to me. Once you're reviewed it to your satisfaction and believe it is ready to merge as-is, I think the right move is to merge it, with the spirit of not letting "perfect" be the enemy of "good".

However, if I find time to make this adjustment before we reach that point, I'll go ahead and add it to this PR, adjusting the commit history as if this was the plan from the start. If we go this route, it may be a good idea for me to prepare another diff for you to reference so that it's easier for you to see what changed.

@Ralith
Copy link
Owner

Ralith commented Feb 24, 2023

it may be a good idea for me to prepare another diff for you to reference so that it's easier for you to see what changed.

Nah, no need to go that far.

@patowen
Copy link
Collaborator Author

patowen commented Feb 24, 2023

Nah, no need to go that far.

Got it. I won't go that far, especially if you don't have a need for it. The real diff history will be available in https://github.com/patowen/hypermine/tree/collision-wip, since that's the branch I usually work on, but I won't spend any effort to clean that up.

EDIT: Looking at this again, it's definitely not good to merge as-is. Based on the abstraction-level changes that have been made, the collision module names no longer make sense. Since I've started working on the improvement I suggested, I think it makes sense to let it block the PR. I don't think it will take too long.

@patowen
Copy link
Collaborator Author

patowen commented Feb 25, 2023

That sounds like a nice improvement. I don't think it needs to block this PR, but if you get it into a state you're happy with before we merge this, then you might as well fold them together.

@Ralith, I ended up making the changes I proposed. It does make some code more verbose and repetitive (especially chunk_collision::chunk_sphere_cast), but I would say it's an overall win for readability.

Copy link
Owner

@Ralith Ralith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress; loved the refactor!

common/src/node.rs Show resolved Hide resolved
common/src/character_controller.rs Show resolved Hide resolved
common/src/graph_collision.rs Show resolved Hide resolved
common/src/graph_collision.rs Outdated Show resolved Hide resolved
common/src/graph_collision.rs Outdated Show resolved Hide resolved
common/src/graph_collision.rs Outdated Show resolved Hide resolved
@patowen
Copy link
Collaborator Author

patowen commented Feb 26, 2023

Ah, I just today realized that Github has a "Compare" button that works with force pushes. No wonder you didn't need me to prepare a diff.

@patowen patowen requested a review from Ralith February 26, 2023 23:48
@Ralith Ralith merged commit 6d061ac into Ralith:master Feb 27, 2023
@patowen
Copy link
Collaborator Author

patowen commented Feb 27, 2023

Thanks for merging! I'm optimistic that we're past the hardest part of getting player physics that works similarly to the collision demo.

Besides code style, the biggest remaining open questions are how to make sure prediction logic feels right, especially when jumping, but assuming these questions can get resolved, we should be pretty close to being able to close #127. Depending on what criteria we're looking for, we might be already be able to close #34.

EDIT: Based on a discussion on Discord, we should be fine just having jumping activate on the next step, which should be good enough at least for a first draft.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants