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

PR #3/5 Astolfo feature/builtin-vector #67

Merged
merged 1 commit into from
Feb 1, 2023

Conversation

RealAstolfo
Copy link
Contributor

@RealAstolfo RealAstolfo commented Jan 16, 2023

This PR builds upon ttencates macro and my math functions to create a rust implementation of what godot has in its C++ as close as i think, meant to be merged after #66

@Bromeon
Copy link
Member

Bromeon commented Jan 16, 2023

Thanks a lot!

As mentioned here:

I think we should provide x and y as public fields, like in GDNative. There are no invariants to upheld on a vector and dealing with setters/getters makes user code extremely verbose. Then, we can have two private functions that would mem::transmute() between godot-rust and glam representations. Of course Vector2 needs to be #[repr(C)] for that.

@Bromeon Bromeon added feature Adds functionality to the library c: core Core components labels Jan 16, 2023
@ttencate
Copy link
Contributor

Oh wow, I didn't realize how far you'd gotten with this.

If #71 goes through instead of #66, you'll have some nice entry points for deduplication. All functions that work directly on glam types and don't access individual components can be moved into vector_macros.rs, into these macros:

  • impl_common_vector_fns if it works for float and int vectors
  • impl_float_vector_fns if it only works for float vectors

I hope you'll find that these macros actually reduce the cognitive burden, since they don't have a lot of parameters.

@Bromeon
Copy link
Member

Bromeon commented Jan 21, 2023

Thanks a lot for this great collection of methods!

To progress here, could you maybe:

  1. make PR #1/5 Astolfo feature/builtin-scalar #65 ready (only license header missing)
  2. rebase this PR onto master (which includes Implement the basics of built-in vector types #71 now)

@RealAstolfo RealAstolfo force-pushed the astolfo-feature/builtin-vector branch 2 times, most recently from dc81475 to 6560738 Compare January 28, 2023 00:33
@RealAstolfo
Copy link
Contributor Author

clippy complains about having too many parameters. i think this is a "too bad, so sad" situation. since all of these parameters are required to emulate the C++ godot equivelent

@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

@RealAstolfo yeah, later we can think about providing some parameters as ranges, e.g. a..=b.

For now, just add a #[allow::clippy(...)] with ... being the ID of the clippy error you get.

@RealAstolfo
Copy link
Contributor Author

didnt know about that. ill add the change then upload it

@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

Don't worry about ..= now, it's not yet definitive. I would just add the #[allow::clippy(...)], so we can finally merge this PR 🙂

Maybe you can squash all the commits to one? I think you can remove the co-author, as ttencate's additions have been merged in a separate PR in the meantime.

@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

bors try

@RealAstolfo
Copy link
Contributor Author

added the clippy change. no idea why vector2 wanted to revert itself but i handled it

bors bot added a commit that referenced this pull request Jan 28, 2023
@bors
Copy link
Contributor

bors bot commented Jan 28, 2023

try

Build succeeded:

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this huge list of vector functions!

Gave some first feedback 🙂

bors try

Comment on lines 254 to 262
pub fn sign(self) -> Self {
-self
}
Copy link
Member

Choose a reason for hiding this comment

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

This is not the sign...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

apologies i should have looked closer. was origonally under the guise that it was a sign flip, not extracting 1 or -1

Self::from_glam(glam::Affine2::from_angle(angle).transform_vector2(self.to_glam()))
}

#[cfg(not(any(gdext_test, doctest)))]
Copy link
Member

Choose a reason for hiding this comment

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

The #[cfg(not(any(gdext_test, doctest)))] is no longer necessary (as of today, if you rebase on master) 🙂

Comment on lines 163 to 169
pub fn distance_squared_to(self, to: Self) -> f32 {
self.to_glam().distance_squared(to.to_glam())
}

pub fn distance_to(self, to: Self) -> f32 {
self.to_glam().distance(to.to_glam())
}
Copy link
Member

Choose a reason for hiding this comment

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

Would be easier to just use (to - self).length_squared() and (to - self).length().

Comment on lines 213 to 239
pub fn max_axis_index(self) -> i32 {
let me = self.to_glam().max_element();
if me == self.x {
0
} else if me == self.y {
1
} else {
2
}
}

pub fn min_axis_index(self) -> i32 {
let me = self.to_glam().min_element();
if me == self.x {
0
} else if me == self.y {
1
} else {
2
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should implement it like that (first computing the maximum, then comparing it to each axis).
Maybe check the gdnative implementation.

We should also make use of the Vector3Axis type, defined further below in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

copy, fixing to use the given type

Comment on lines +258 to +276
pub fn slerp(self, to: Self, weight: f32) -> Self {
let start_length_sq = self.length_squared();
let end_length_sq = to.length_squared();
if start_length_sq == 0.0 || end_length_sq == 0.0 {
return self.lerp(to, weight);
}
let start_length = start_length_sq.sqrt();
let result_length = lerp(start_length, end_length_sq.sqrt(), weight);
let angle = self.angle_to(to);
self.rotated(angle * weight) * (result_length / start_length)
}
Copy link
Member

Choose a reason for hiding this comment

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

The gdnative implementation is significantly easier; I wonder if it's correct, too?

    /// Returns the result of spherical linear interpolation between this vector and b, by amount t.
    /// t is on the range of 0.0 to 1.0, representing the amount of interpolation.
    ///
    /// **Note**: Both vectors must be normalized.
    #[inline]
    pub fn slerp(self, b: Self, t: f32) -> Self {
        let theta = self.angle_to(b);
        self.rotated(self.cross(b).normalized(), theta * t)
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

perhaps the gdnative implementation is valid as well, i simply looked at upstream godot and converted the language. up to you if you prefer the gdnative version as opposed to upstream.

Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
	real_t start_length_sq = length_squared();
	real_t end_length_sq = p_to.length_squared();
	if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
		// Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
		return lerp(p_to, p_weight);
	}
	real_t start_length = Math::sqrt(start_length_sq);
	real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
	real_t angle = angle_to(p_to);
	return rotated(angle * p_weight) * (result_length / start_length);
}

Copy link
Member

Choose a reason for hiding this comment

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

Maybe keep your implementation for now. In the first line, can you add this comment?

// TODO compare with gdnative implementation:
// https://github.com/godot-rust/gdnative/blob/master/gdnative-core/src/core_types/vector3.rs#L335-L343

Then we can check it later. We anyway need to work out a testing strategy at some point 🙂

bors bot added a commit that referenced this pull request Jan 28, 2023
@bors
Copy link
Contributor

bors bot commented Jan 28, 2023

try

Build succeeded:

@RealAstolfo RealAstolfo force-pushed the astolfo-feature/builtin-vector branch 2 times, most recently from 861506c to 39d8022 Compare January 28, 2023 23:02
@RealAstolfo
Copy link
Contributor Author

btw i tried squashing any two commits but the github client refuses too for everything i try. so unless you have a good workaround we might have to just live with it

@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

You should be able to combine commits with git rebase -i, and then mark all except the oldest as "squash".
Then, you can force-push using git push --force-with-lease.

If you can't get it to work, I can also do it before the merge.

bors try

bors bot added a commit that referenced this pull request Jan 28, 2023
@RealAstolfo
Copy link
Contributor Author

You should be able to combine commits with git rebase -i, and then mark all except the oldest as "squash". Then, you can force-push using git push --force-with-lease.

If you can't get it to work, I can also do it before the merge.

i might just need you to tell me how you did it later, because all i get is
Unable to squash. Squashing replays all commits up to the last one required for the squash. A merge commit cannot exist among those commits. (of which many of the commits were)

@bors
Copy link
Contributor

bors bot commented Jan 28, 2023

try

Build succeeded:

@Bromeon Bromeon force-pushed the astolfo-feature/builtin-vector branch from 292cada to c9996c3 Compare February 1, 2023 18:52
@Bromeon
Copy link
Member

Bromeon commented Feb 1, 2023

Thanks for the contribution! 👍

Squashed and rebased onto master. Would be nice if you could do that in the future 🙂 maybe search StackOverflow etc. for necessary git workflows.

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 1, 2023

Build succeeded:

  • full-ci

@bors bors bot merged commit 895283f into godot-rust:master Feb 1, 2023
@RealAstolfo RealAstolfo deleted the astolfo-feature/builtin-vector branch February 5, 2023 03:07
bors bot added a commit that referenced this pull request Feb 12, 2023
68: PR #4/5 Astolfo feature/builtin-quaternion r=Bromeon a=RealAstolfo

Co-Authored-By: Thomas ten Cate <ttencate@gmail.com>

Implemented Quaternion to the best of my current ability to resemble godot's, meant to be merged after #67 

Co-authored-by: RealAstolfo <astolfo.gman@gmail.com>
Co-authored-by: Jan Haller <bromeon@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants