角度反思了一圈反弹球

问题描述 投票:3回答:2

我做了一些反弹的元素一场游戏圈(我使用pygame的),我的元素有两个属性,一个是角度,一个是速度这里是如何移动的元素:

mvx = math.sin(self.angle) * self.speed  
mvy = -math.cos(self.angle) * self.speed  
self.x += mvx  
self.y += mvy

我的问题是这样的:我知道在顶部(99.6°),我有碰撞点(x和y)的角度,但我无法找到底部(42.27°)的角度某人是否可以做一个关系所述第一角度和所述第二间?

图片比较好...

python pygame geometry angle bounce
2个回答
2
投票

我建议做计算reflection矢量圆形表面上的事件向量。 在下列公式中N是圆的法线矢量,I是入射矢量(反弹球的当前方向向量)和R是反射向量(反弹球的出射方向矢量):

R = I - 2.0 * dot(N, I) * N.

使用pygame.math.Vector2

为了计算法向量,你已经知道‘打’点(POI qazxsw,qazxsw POI)和圆(dvxdvy)的中心点:

cptx

计算反射:

cpty

新的角可以通过circN = (pygame.math.Vector2(cptx - px, cpty - py)).normalize() 计算:

vecR = vecI - 2 * circN.dot(vecI) * circN

代码:

math.atan2(y, x)
self.angle  = math.atan2(vecR[1], vecR[0])

0
投票

三角形的内角需要总结到import math import pygame 。另外,该角度是px = [...] # x coordinate of the "hit" point on the circle py = [...] # y coordinate of the "hit" point on the circle cptx = [...] # x coordinate of the center point of the circle cpty = [...] # y coordinate of the center point of the circle circN = (pygame.math.Vector2(cptx - px, cpty - py)).normalize() vecI = pygame.math.Vector2(math.cos(self.angle), math.sin(self.angle)) vecR = vecI - 2 * circN.dot(vecI) * circN self.angle = math.pi + math.atan2(vecR[1], vecR[0]) 补充三角形的角旁边(由180°调用它),即99.96°所以A。调用99.96° + A = 180°底角的。而对于最后一个角度A = 180° - 99.96°,我们可以使用它通过与等于B = 42.27°另一角顶点反对。然后:

C

P.S:我知道值不完全相等,但它一定是因为小数四舍五入

© www.soinside.com 2019 - 2024. All rights reserved.