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

vec2 truncate function #308

Open
mreinstein opened this issue May 17, 2018 · 13 comments
Open

vec2 truncate function #308

mreinstein opened this issue May 17, 2018 · 13 comments

Comments

@mreinstein
Copy link
Contributor

Shortens a vector to maxLength if it is longer.

export function truncate(out, maxLength) {
  if (vec2.length(out) > maxLength) {
    setLength(out, maxLength)
  }
  return out
}
@andrevenancio
Copy link
Contributor

Hey @mreinstein can you share scenarios where this might be useful as part of the vec2, vec3 or vec4 classes instead of a utils folder in your project or an util folder on the src/common.js ?

@sistr22
Copy link

sistr22 commented Jul 28, 2018

I wanted multiple time to have a way to build a vec3 out of a vec4 or a vec2 out of a vec4 or vec 3.
Specially when using typescript it would be nice to have such methods.
I really like how glm handle it (the constructor of vec2 accept a vec3 or vec4 and only take the first 2 values).

@stefnotch
Copy link
Collaborator

@WhiteSEEKER Please open a new issue for that.

@sistr22
Copy link

sistr22 commented Jul 28, 2018

Oh I see, I miss understand this feature request. I though it was about truncating in dimensions, not in length ... my bad.

@mreinstein
Copy link
Contributor Author

mreinstein commented Jul 28, 2018

@andrevenancio I can't enumerate all possible use cases, but one example is boids/flocking:
A boid might have a follow leader behavior, + an avoid obstacles behavior + a group cohesion behavior. These behaviors are usually achieved by combining and blending forces and calculating velocity. These systems build up forces and then truncate to maxLength to ensure they don't go over some max value (maxSteering force, maxAcceleration force, etc.)

https://github.com/hughsk/boids/blob/master/index.js#L146-L153

instead of a utils folder in your project or an util folder on the src/common.js

util and common folders are the digital equivalent of a junk drawer. I don't like this pattern. It tends to be a garbage heap where people throw things.

@stefnotch
Copy link
Collaborator

Here would be a (untested) implementation for vec2.

export function truncate(out, maxLength) {
  let x = a[0],
      y = a[1];

  let len = Math.sqrt(x*x + y*y);

  if (len > maxLength) {
     let scaleDown = maxLength / len;
     a[0] = x * scaleDown;
     a[1] = y * scaleDown;
  }

  return out
}

@andrevenancio
Copy link
Contributor

Yeah, something like that might work.

I do understand some frustrations from a user point of view, me, for example, I would love if gl-matrix could be used as vectors are used in shaders :p .x [0] or .xyz or even .zyx who knows, but that's a major refactor on the library with potential performance issues.

I think it's better to have a math util folder on your project specifically. Otherwise, gl-matrix starts to be a swiss army knife that tried to do everything and perhaps not everything right.

@stefnotch
Copy link
Collaborator

stefnotch commented Sep 24, 2018

I couldn't have expressed my thoughts better than andrevenancio did.

I'm starting to think that having a second repository with all sorts of extra functions for gl-matrix might be a decent idea. 🤔

@mreinstein
Copy link
Contributor Author

mreinstein commented May 21, 2020

I've recently come back to some projects that require vector math, and I'm again finding a need for setLength(out, vec2, length) (for example, this is used when doing triangle/swept-sphere collision handling)

I can (and do) maintain a set of math utility functions for these unordained vector functions, but now I have 4 different projects with these identical modules in them. so the next logical thought is "hey I could publish these in a module and cut down on duplication". And then the following thought is "wait, the world already has a polished vector/matrix math module, why am I making a new one?"

I can totally understand and appreciate the desire to avoid bloating gl-matrix with a ton of stuff.
But on the other hand, one of the great things about gl-matrix is that it's got a large, consistent set of operations that can be applied across vectors and matrices.

Can we please re-consider our stance on some of these vector additions? 🙏

@stefnotch
Copy link
Collaborator

Which functions would you be interested in? And, are there any relevant feature requests?

@mreinstein
Copy link
Contributor Author

at the moment, the most common one I'm looking for is setLength(out, vec2, length). I believe there is an implementation mentioned in #217 but I haven't evaluated it's quality. Here is my current implementation: https://github.com/mreinstein/vec2-gap/blob/master/set-length.js

I'm happy to open a separate issue for this if that's helpful (I don't think we should let people create issues that lump together like 8 different function requests in one. :) )

@stefnotch
Copy link
Collaborator

Yes please, do open a new issue with that feature request!

@mreinstein
Copy link
Contributor Author

related PR: #394

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

No branches or pull requests

4 participants