Repository navigation
chore: Write the Bézier math with vector operations and atan2 - #125
Merged
Merged
Conversation
- BezierSegment.angle gets the angle with atan2 instead of atan and its own cases, and says that it is from -Math.PI to Math.PI. It is also right when dz is -0.0, where the atan version turned +x into -x. - The direction is the derivative divided by 3: the steps between the points weighted by the quadratic Bernstein polynomials, instead of the expanded polynomial in t. - The weights are named after their degree, cubic0 - cubic3 in point and quadratic0 - quadratic2 in angle. - Vector3.multiply lets point and angle add weighted vectors. The sums are made in the same order as before.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What and why
BezierSegment.anglefound the direction of the curve with an expanded polynomial int, written out once for x and once for z, and turned it into an angle withatanand its own cases for the quadrants. This makes the math read like the formulas it comes from.Math.atan2(dx, dz)instead ofatan(dx / dz)with cases for NaN anddz < 0. A zero vector still gives 0, as the Javadoc says; atan2 alone would giveMath.PIor-Math.PIthere when dz is-0.0. The Javadoc now gives the range,-Math.PItoMath.PI.step0tostep2) weighted by the quadratic Bernstein polynomials, instead of the expanded polynomial.cubic0tocubic3inpoint(bernstein0tobernstein3before) andquadratic0toquadratic2inangle.Vector3.multiply(double)letspointandangleadd weighted vectors instead of writing each coordinate.Behavior:
pointgives the same values: the products and the sums are made in the same order.anglediffers only in the last bits, and for directions towards -x and -z, where it now gives -π to -π/2 instead of π to 3π/2. The only caller,CurveLayout, uses its cosine and sine, which differ by at most about 7e-16. Compared with the previous code on 10 million random segments, the angle differs by at most 9.1e-13 radians where the direction is longer than 0.001.-0.0, the previous code gave the opposite direction along x (-π/2 for +x); atan2 gives the right one. dz is-0.0only when every term of it is.How it was tested
./gradlew build :curvebuilding-fabric:26.3:runGameTest checkCodeStyle --warning-mode all: the build and unit tests pass, with no Gradle, javac or Checkstyle warnings. The game tests pass on the six versions thatbuildruns them on.BezierSegmentTestchecks the angle towards -x and -z (-3π/4), andVector3Testchecksmultiply.//bcand the preview in the game, and the cost of the extraVector3objects inpoint, which the Fabric preview calls every frame.Notes