-
Notifications
You must be signed in to change notification settings - Fork 90
3D Ball-Ball Resolution #349
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
Draft
derek-mcblane
wants to merge
3
commits into
ekiefl:main
Choose a base branch
from
derek-mcblane:3d-ball-ball-resolver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import attrs | ||
| import numpy as np | ||
| import quaternion | ||
| from numba import jit | ||
|
|
||
| import pooltool.constants as const | ||
| import pooltool.ptmath as ptmath | ||
| from pooltool.objects.ball.datatypes import Ball, BallState | ||
| from pooltool.objects.ball.datatypes import Ball | ||
| from pooltool.physics.dimensionality import Dim | ||
| from pooltool.physics.resolve.ball_ball.core import CoreBallBallCollision | ||
| from pooltool.physics.resolve.ball_ball.core import ( | ||
| CoreBallBallCollision, | ||
| final_ball_motion_state, | ||
| ) | ||
| from pooltool.physics.resolve.ball_ball.friction import ( | ||
| AlciatoreBallBallFriction, | ||
| BallBallFrictionStrategy, | ||
|
|
@@ -15,18 +19,21 @@ | |
| from pooltool.physics.utils import surface_velocity | ||
|
|
||
|
|
||
| @jit(nopython=True, cache=const.use_numba_cache) | ||
| def _resolve_ball_ball(rvw1, rvw2, R, u_b, e_b): | ||
| unit_x = np.array([1.0, 0.0, 0.0]) | ||
|
|
||
| # rotate the x-axis to be in line with the line of centers | ||
| delta_centers = rvw2[0] - rvw1[0] | ||
| # FIXME3D: this should use quaternion rotation in 3D | ||
| theta = ptmath.angle(delta_centers, unit_x) | ||
| rvw1[1] = ptmath.coordinate_rotation(rvw1[1], -theta) | ||
| rvw1[2] = ptmath.coordinate_rotation(rvw1[2], -theta) | ||
| rvw2[1] = ptmath.coordinate_rotation(rvw2[1], -theta) | ||
| rvw2[2] = ptmath.coordinate_rotation(rvw2[2], -theta) | ||
| frame_rotation = ptmath.quaternion_from_vector_to_vector(delta_centers, unit_x) | ||
| rvw1 = quaternion.rotate_vectors(frame_rotation, rvw1) | ||
| rvw2 = quaternion.rotate_vectors(frame_rotation, rvw2) | ||
| rvw1, rvw2 = _resolve_ball_ball_x_normal(rvw1, rvw2, R, u_b, e_b) | ||
| rvw1 = quaternion.rotate_vectors(frame_rotation.conjugate(), rvw1) | ||
| rvw2 = quaternion.rotate_vectors(frame_rotation.conjugate(), rvw2) | ||
| return rvw1, rvw2 | ||
|
Comment on lines
-18
to
+31
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has some potential to change 2D behavior, so I will:
|
||
|
|
||
|
|
||
| @jit(nopython=True, cache=const.use_numba_cache) | ||
| def _resolve_ball_ball_x_normal(rvw1, rvw2, R, u_b, e_b): | ||
| unit_x = np.array([1.0, 0.0, 0.0]) | ||
|
|
||
| # velocity normal component, same for both slip and no-slip after collison cases | ||
| v1_n_f = 0.5 * ((1.0 - e_b) * rvw1[1][0] + (1.0 + e_b) * rvw2[1][0]) | ||
|
|
@@ -88,22 +95,11 @@ def _resolve_ball_ball(rvw1, rvw2, R, u_b, e_b): | |
| rvw1_f[2][0] = w1_n_f | ||
| rvw2_f[2][0] = w2_n_f | ||
|
|
||
| # rotate everything back to the original frame | ||
| rvw1_f[1] = ptmath.coordinate_rotation(rvw1_f[1], theta) | ||
| rvw1_f[2] = ptmath.coordinate_rotation(rvw1_f[2], theta) | ||
| rvw2_f[1] = ptmath.coordinate_rotation(rvw2_f[1], theta) | ||
| rvw2_f[2] = ptmath.coordinate_rotation(rvw2_f[2], theta) | ||
|
|
||
| # FIXME3D: include z velocity components | ||
| # remove any z velocity components from spin-induced throw | ||
| rvw1_f[1][2] = 0.0 | ||
| rvw2_f[1][2] = 0.0 | ||
|
|
||
| return rvw1_f, rvw2_f | ||
|
|
||
|
|
||
| @attrs.define | ||
| class FrictionalInelastic(CoreBallBallCollision): | ||
| class FrictionalInelastic3D(CoreBallBallCollision): | ||
| """A simple ball-ball collision model including ball-ball friction, and coefficient of restitution for equal-mass balls | ||
|
|
||
| Largely inspired by Dr. David Alciatore's technical proofs | ||
|
|
@@ -115,9 +111,9 @@ class FrictionalInelastic(CoreBallBallCollision): | |
| friction: BallBallFrictionStrategy = AlciatoreBallBallFriction() | ||
|
|
||
| model: BallBallModel = attrs.field( | ||
| default=BallBallModel.FRICTIONAL_INELASTIC, init=False, repr=False | ||
| default=BallBallModel.FRICTIONAL_INELASTIC_3D, init=False, repr=False | ||
| ) | ||
| dim: Dim = attrs.field(default=Dim.TWO, init=False, repr=False) | ||
| dim: Dim = attrs.field(default=Dim.THREE, init=False, repr=False) | ||
|
|
||
| def solve(self, ball1: Ball, ball2: Ball) -> tuple[Ball, Ball]: | ||
| """Resolves the collision.""" | ||
|
|
@@ -130,7 +126,35 @@ def solve(self, ball1: Ball, ball2: Ball) -> tuple[Ball, Ball]: | |
| e_b=(ball1.params.e_b + ball2.params.e_b) / 2, | ||
| ) | ||
|
|
||
| ball1.state = BallState(rvw1, const.sliding) | ||
| ball2.state = BallState(rvw2, const.sliding) | ||
| ball1.state.rvw = rvw1 | ||
| ball2.state.rvw = rvw2 | ||
|
|
||
| ball1.state.s = final_ball_motion_state(rvw1, ball1.params.R) | ||
| ball2.state.s = final_ball_motion_state(rvw2, ball2.params.R) | ||
|
|
||
| return ball1, ball2 | ||
|
|
||
|
|
||
| @attrs.define | ||
| class FrictionalInelastic2D(FrictionalInelastic3D): | ||
| """A simple ball-ball collision model including ball-ball friction, and coefficient of restitution for equal-mass balls | ||
|
|
||
| For details see :class:`FrictionalInelastic3D`. | ||
| """ | ||
|
|
||
| model: BallBallModel = attrs.field( | ||
| default=BallBallModel.FRICTIONAL_INELASTIC_2D, init=False, repr=False | ||
| ) | ||
| dim: Dim = attrs.field(default=Dim.TWO, init=False, repr=False) | ||
|
|
||
| def solve(self, ball1: Ball, ball2: Ball) -> tuple[Ball, Ball]: | ||
| """Resolves the collision.""" | ||
| ball1, ball2 = super().solve(ball1, ball2) | ||
|
|
||
| # remove any z velocity components for 2D | ||
| ball1.state.rvw[1, 2] = 0.0 | ||
| ball1.state.rvw[1, 2] = 0.0 | ||
| ball1.state.s = const.sliding | ||
| ball2.state.s = const.sliding | ||
|
|
||
| return ball1, ball2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really make sense to rotate the
rpart ofrvw. I should just leave r alone here and in the ball-cushion model that does something similar. It might introduce some error by doing a rotation then rotating it back.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And same should be done to the existing ball cushion model