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
constrain() returns zero-dimensional array instead of float #319
Comments
@smlpt , thank you for the bug report. I looked at the Py5Vector constructor and I see what the problem is. There's a place where it is checking that the argument is a numpy array but it should also check the numpy array's shape. Right now it is assuming the dimensionality is 1. It also needs to handle dimensions of 0 and >= 2. I intend to do a release this week and will get this fix in. Your workaround is effective. I can also suggest trying something like this: vel = Py5Vector2D(constrain(np.array([mouse_x - pmouse_x, mouse_y - pmouse_y]), -maxvel, maxvel)) Similar to your example, this constrains each of the x and y velocities to 3, which is like constraining the velocity to a square. The diagonal velocity can be 3 * 1.414. Depending on your use case, you might like this: vel = Py5Vector2D(mouse_x - pmouse_x, mouse_y - pmouse_y).set_limit(3) That limits the vector's magnitude to 3, constraining the velocity to 3 for any direction, including the diagonals. |
Good to see you could reproduce it. Yours is of course a way more elegant solution, thank you very much! And thank you also for developing this awesome piece of software, it's much appreciated. |
improve Py5Vector constructor to address #319
I just merged a PR to fix this. While investigating the problem you found, I also discovered that these don't work correctly either: Py5Vector(np.arange(4).reshape(2, 2))
Py5Vector(np.arange(2).reshape(2, 1, 1), np.array(2), np.array([3])) I fixed this also.
You are welcome! And thank you for contributing to making py5 even better! |
So I wanted to use constrain() to limit the possible mouse speed that is passed to a 2D vector. However, I always get the error saying that the object is not iterable. Minimal example:
The error seems to come from the fact that constrain() returns an NDArray instead of just an int or float (see this line). It seems to work in your example, but it doesn't work in my case. I'm not sure if this is a bug or if this is expected behavior. I can circumvent it by calling
velx.item()
instead.The text was updated successfully, but these errors were encountered: