我正在尝试使球在盒子内正确弹跳,特别是在特定角度处理角落并处理角落。我有一个问题,因为我的球不断出现。我有这个功能,告诉我的球是否开箱即可,它可以处理角落和墙壁。代码是这样的:
if ((self._x > self._Grid.getWidth()) or (self._x < 0)):
print("RandomNode:outside paramaters: x! self._x = %s , self._velx = %s" % (self._x , self._velx))
if ((self._y > self._Grid.getLength()) or (self._y < 0)):
print("RandomNode:outside paramaters: y!")
if ((self._velx + self._x) > self._Grid.getWidth()):
diff = self._Grid.getWidth()-self._x
self._velx *= -1
if (diff == 0):
self._x -= self._velx
else:
self._x+= diff
tampered = True
#print("eqn1: self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
if (self._velx + self._x < 0):
diff = self._x
self._velx *= -1
if (diff == 0):
self._x += self._velx
else:
self._x-= diff
tampered = True
#print("eqn2: self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
if ((self._vely + self._y) > self._Grid.getLength()):
diff = self._Grid.getLength()-self._y
self._vely *= -1
if (diff == 0):
self._y -= self._vely
if (tampered == True):
if ((self._velx * -1 == self._vely) or (self._velx == self._vely)):
self._x += self._velx
self._y += self._vely
#print("eqn31:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._x += (self._velx - diff)
self._y += self._vely
#print("eqn32:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
tampered = True
#print("eqn33:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._y+= diff
if (tampered == True):
if ((self._velx * -1 == self._vely) or (self._velx == self._vely)):
self._x += self._velx
self._y += self._vely
#print("eqn31:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._x += (self._velx - diff)
self._y += self._vely
#print("eqn32:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
tampered = True
#print("eqn33:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
if (self._vely + self._y < 0):
diff = self._y
self._vely *= -1
if (diff == 0):
self._y += self._vely
if (tampered == True):
if ((self._velx * -1 == self._vely) or (self._velx == self._vely)):
self._x += self._velx
self._y += self._vely
#print("eqn41:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._x += (self._velx + diff)
self._y += self._vely
#print("eqn42:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
tampered = True
#print("eqn43:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._y-= diff
if (tampered == True):
if ((self._velx * -1 == self._vely) or (self._velx == self._vely)):
self._x += self._velx
self._y += self._vely
#print("eqn41:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
self._x += (self._velx + diff)
self._y += self._vely
#print("eqn42:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
else:
tampered = True
#print("eqn43:self._x = %s , self._y = %s , self._velx= %s, self._vely= %s" % (self._x, self._y, self._velx, self._vely))
return tampered
我不知道为什么它不起作用。 x和y显然是它的坐标。 Velx和Vely是它的x和y速度。篡改是一个布尔值,可以防止球正常移动并且仅在检查中移动。
这是我的问题。这段代码有什么问题?或者....是否有一个模板在网络上的某个地方或任何地方用python编写,或者你使用的代码完成了我正在尝试处理和完成的事情?请随意修改代码,告诉我。任何已经解决的链接都会很好。感谢。
答案 0 :(得分:7)
重写此代码。对于你想要解决的简单问题来说,这太复杂了。
首先,球的2D运动只是2个1D问题。您可以完全分离X和Y.例如,击中一个角完全等同于在X轴上击中墙+在Y轴上击中墙。在X中击中墙只会反转X速度(如果你想模拟它,可能会失去一些能量)。在Y中撞墙可以逆转Y速度。
第二,因为X和Y的处理非常相似,所以从中提取方法
def handle_axis_movement(location, velocity):
"returns updated location and velocity"
...
self.x, self.vel_x = handle_axis_movement(self.x, self.vel_x)
self.y, self.vel_y = handle_axis_movement(self.y, self.vel_y)
这会将代码(和错误)的数量减少一半。
第三次,您不必单独处理diff == 0和diff&lt; 0。这两种情况都意味着球击中墙壁,并且应该反转它的速度。然后你应该纠正这个位置,以解释它无法穿过墙壁的事实。
location += velocity
if location > max_bound:
location = max_bound - (location - max_bound)
velocity *= -1
if location < min_bound:
location = min_bound - (location - min_bound)
velocity *= -1
答案 1 :(得分:1)
当pymunk为你做得很好时,你不应该重新发明这个轮子。 ;)