Numpy 位置和向量数组 - 实现碰撞检测
我在 PyOpenGL 中使用点精灵与 numpy 和 glDrawArrays。所以我有两个数组,一个用于点,一个用于向量。
r = lambda: random.random()
self.pts = numpy.zeros((2000,2), dtype=numpy.uint16)
for pt in self.pts:
pt[0] = 300*r()
pt[1] = 200*r()
self.vectors = numpy.zeros((2000,2), dtype=numpy.uint16)
for vec in self.vectors:
vec[0] = 3*r()
vec[1] = 3*r()
现在我需要根据与屏幕边框的碰撞来更新位置和向量数组。例如,
if pt[0][0]-width < 0: pt[0][0] = width; vec[0][0] *= -1
最后我必须在屏幕上有一个 2000x2 的 pt 数组来提供给 opengl。
编辑 - 将点保留在边界框内的当前解决方案(0,0,宽度,高度)
points [:,0][points[:,0] > width] = width
vectors[:,0][points[:,0] > width] *= -1
points [:,0][points[:,0] < 0] = 0
vectors[:,0][points[:,0] < 0] *= -1
points [:,1][points[:,1] > height] = height
vectors[:,1][points[:,1] > height] *= -1
points [:,1][points[:,1] < 0] = 0
vectors[:,1][points[:,1] < 0] *= -1
I'm using point sprites in PyOpenGL with numpy and glDrawArrays. So I have two arrays, one for the points and one for the vectors.
r = lambda: random.random()
self.pts = numpy.zeros((2000,2), dtype=numpy.uint16)
for pt in self.pts:
pt[0] = 300*r()
pt[1] = 200*r()
self.vectors = numpy.zeros((2000,2), dtype=numpy.uint16)
for vec in self.vectors:
vec[0] = 3*r()
vec[1] = 3*r()
Now I need to update the position and vector arrays based on collision with the screen borders. So for example
if pt[0][0]-width < 0: pt[0][0] = width; vec[0][0] *= -1
In the end I must have a 2000x2 arrays of pts on the screen to feed to opengl.
EDIT - Current solution to keeping points inside a bounding box (0,0,width,height)
points [:,0][points[:,0] > width] = width
vectors[:,0][points[:,0] > width] *= -1
points [:,0][points[:,0] < 0] = 0
vectors[:,0][points[:,0] < 0] *= -1
points [:,1][points[:,1] > height] = height
vectors[:,1][points[:,1] > height] *= -1
points [:,1][points[:,1] < 0] = 0
vectors[:,1][points[:,1] < 0] *= -1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解你的问题,你想做这样的事情:
(试图在这里说明几种不同的方法)
If I understand your question, you want to do something like this:
(Trying to illustrate a few different methods here)