Numpy 位置和向量数组 - 实现碰撞检测

发布于 2024-09-14 03:06:35 字数 1008 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二货你真萌 2024-09-21 03:06:35

如果我理解你的问题,你想做这样的事情:
(试图在这里说明几种不同的方法)

import numpy as np

numpoints, numdimensions = 2000, 2

# Generate random points
points = np.random.random((numpoints, numdimensions))
points[:,0] *= 300
points[:,1] *= 200
points = points.astype(np.int16)

# Generate random vectors
vectors = np.random.randint(-3, 3, size=(numpoints, numdimensions)).astype(np.int16)

# Update points and vectors where point x-coords are > width 
points[:,0][points[:,0] > width] = width
vectors[:,0][points[:,0] > width] = -1

If I understand your question, you want to do something like this:
(Trying to illustrate a few different methods here)

import numpy as np

numpoints, numdimensions = 2000, 2

# Generate random points
points = np.random.random((numpoints, numdimensions))
points[:,0] *= 300
points[:,1] *= 200
points = points.astype(np.int16)

# Generate random vectors
vectors = np.random.randint(-3, 3, size=(numpoints, numdimensions)).astype(np.int16)

# Update points and vectors where point x-coords are > width 
points[:,0][points[:,0] > width] = width
vectors[:,0][points[:,0] > width] = -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文