开发者

Numpy position and vector arrays - implement collision detection

开发者 https://www.devze.com 2023-01-11 03:11 出处:网络
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.

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 fee开发者_开发知识库d 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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消