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

constrain() returns zero-dimensional array instead of float #319

Closed
smlpt opened this issue Jul 17, 2023 · 3 comments
Closed

constrain() returns zero-dimensional array instead of float #319

smlpt opened this issue Jul 17, 2023 · 3 comments

Comments

@smlpt
Copy link

smlpt commented Jul 17, 2023

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:

maxvel = 3 
velx = constrain(mouse_x-pmouse_x, -maxvel, maxvel)
vely = constrain(mouse_y-pmouse_y, -maxvel, maxvel)
vel = Py5Vector2D(velx, vely)

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.

@hx2A
Copy link
Collaborator

hx2A commented Jul 17, 2023

@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.

@smlpt
Copy link
Author

smlpt commented Jul 17, 2023

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.

hx2A added a commit to hx2A/py5generator_fork that referenced this issue Jul 17, 2023
hx2A added a commit that referenced this issue Jul 17, 2023
improve Py5Vector constructor to address #319
@hx2A
Copy link
Collaborator

hx2A commented Jul 17, 2023

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.

And thank you also for developing this awesome piece of software, it's much appreciated.

You are welcome! And thank you for contributing to making py5 even better!

@hx2A hx2A closed this as completed Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants