我想用pygame做一个2D游戏,要这么做,我想用数学矩阵。虽然我想得对,但我有个问题。这里是:我做一个随机点,我计算它的向量长度(从0,0开始),然后,通过矩阵变换方程(2D),我想移动那个点,比如说+1度。我得到了一个结果,但是等等,为什么传输向量的长度和另一个不同?所有答案将不胜感激,谢谢:)
这是我的密码:
import numpy
import math
import random
#length of wector
def R(w0):
#start point coordinates
x0 = y0 = 0
#new point coordinates
x1 = w0[0][0]
y1 = w0[1][0]
r = math.sqrt((x1 - x0)**2 + (y1-y0)**2)
return r
#matrix rotation by 1 deg
rad = math.pi/180
Tm = numpy.matrix([[math.acos(rad),-math.asin(rad)], [math.asin(rad), math.acos(rad)]])
#w0
x0 = random.randint(1, 5)
y0 = random.randint(1, 5)
w0 = numpy.matrix([[x0],[y0]])
r0 = R(w0)
print "x0: ",w0[0][0], "y0: ",w0[1][0], "r0: ", r0
#w0'
w1 = Tm * w0
r1 = R(w1)
print "x1: ",w1[0][0], "y1: ",w1[1][0], "r1: ", r1
如你所见,r0和r1是不同的:/
您使用的是
arccos
和arcsin
,而不是旋转矩阵中的cos
和sin
。修正这一点,两个向量就应该有相同的长度。你知道吗也可以使用
np.linalg.norm
作为向量长度。你知道吗相关问题 更多 >
编程相关推荐