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

Mathematical operations for Vector2 signals #967

Closed
aarthificial opened this issue Feb 26, 2024 · 1 comment
Closed

Mathematical operations for Vector2 signals #967

aarthificial opened this issue Feb 26, 2024 · 1 comment
Assignees
Labels
a-core Relates to the core package b-enhancement New feature or request c-accepted The issue is ready to be worked on

Comments

@aarthificial
Copy link
Contributor

Description
Modifying a vector by applying some operation (addition, subtraction, etc) is currently really verbose.
Imagine you want to animate the position of node by moving it by [200, 400] relative to its current position:

yield* node.position(node.position().add([200, 400]), 0.6);

In general, whenever we want to derive the new value of a signal using its current value things become verbose.

Proposed solution
Introduce a set of methods for Vector2 signals that reflect the operations of the Vector2 class.

// before
yield* node.position(node.position().add([200, 400]), 0.6);
// after
yield* node.position.add([200, 400], 0.6);

These could be used to either tween or immediately modify the signal, just like you normally would:

// set
node.position.add([200, 400]);
// tween
yield* node.position.add([200, 400], 0.6);

On top of that, a new edit method should be introduced to derive the new value in whatever way necessary:

// this:
node.position.add([200, 400]);
// is the same as this:
node.position.edit(current => current.add([200, 400]));

While on itself still pretty verbose, this interface would allow you to create reusable functions for operations that are not natively supported.
Imagine you want to snap the vector to a 20x20 grid:

// define a reusable method:
function snap(grid: PossibleVector2) {
  return (current: Vector2) => current.div(grid).floored.mul(grid);
}

// use it:
node.position.edit(snap(20));
@aarthificial aarthificial added b-enhancement New feature or request c-accepted The issue is ready to be worked on a-core Relates to the core package labels Feb 26, 2024
@mancopp
Copy link
Contributor

mancopp commented Apr 6, 2024

I am working on this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-core Relates to the core package b-enhancement New feature or request c-accepted The issue is ready to be worked on
Projects
None yet
Development

No branches or pull requests

2 participants