给出两个点和矢量长度的x和y的变化

问题描述 投票:-1回答:1

我从第一个点(玩家位置)射击子弹,它需要以给定的速度向第二个点(鼠标点击)移动。

现在我拥有所有这些代码cx和cy标记玩家的中心或点1,而MouseX和MouseY代表mous点击的坐标

if MouseX < cx:              #determines direction of bullet
    direction = 'left'
else:
    direction = 'right'

if (cx-MouseX) != 0:
    new_slope = (cy - MouseY) / (cx - MouseX)
else:
    new_slope = 'undefined'             #determines slope and b value to get the equation of the line
                                        #that the bullet travels on 

if new_slope != 'undefined':    
    b_value = ((-1)*(new_slope)*(cx))+(cy)
else:
    b_value = None

if self.direction == 'right':
        if self.slope != 'undefined':
            if self.slope > 0 or self.slope < 0 and self.slope != 0:
                if self.slope < 1 or self.slope > -1:
                    float(self.bx)
                    float(bullet_speed)
                    self.by = (self.slope*(self.bx+bullet_speed)+self.b_value)
                    self.bx = ((self.by - self.b_value)/self.slope)
                else:
                    float(self.bx)
                    float(bullet_speed)
                    self.bx = (((self.by+bullet_speed) - self.b_value)/self.slope)
                    self.by = (self.slope*(self.bx)+self.b_value)
        else:
            self.bx = self.bx + bullet_speed

    else:
        if self.slope != 'undefined':
            if self.slope > 0 or self.slope < 0:
                if self.slope < 1 or self.slope > -1:
                    self.by = (self.slope*(self.bx-bullet_speed)+self.b_value)
                    self.bx = ((self.by - self.b_value)/self.slope)
                elif self.slope == 0:
                    self.bx -= bullet_speed
                else:
                    self.bx = (((self.by-bullet_speed) - self.b_value)/self.slope)
                    self.by = (self.slope*(self.bx)+self.b_value)
            else:
                self.bx = self.bx - bullet_speed

它非常凌乱,导致我的子弹在向上或向下射击时速度增加,并且在向左或向右射击时变慢。

但是,我无法改变代码,让我的子弹以相同的速度行进,无论角度拍摄如何。如果有擅长数学和tkinter的人可以提供帮助,那将非常感激。

(作为旁注,我的所有对象都是Tkinter画布项,所以它们必须按整数移动)

python-2.7 vector tkinter
1个回答
0
投票

我希望我没有犯错误 - 我不记得太清楚了。

编辑:在atan2(dy, dx)有错误 - 必须是atan2(dx, dy)


如果你有子弹的位置和速度,以及目标的位置,它会计算新的位置(在一帧/移动之后)。

你必须重复它

import math

speed = 10

# bullet current position
x1 = 0
y1 = 0

# taget possition
x2 = 100
y2 = 100

dx = x2 - x1
dy = y2 - y1

angle = math.atan2(dx, dy)
#print(math.degrees(angle))

cx = speed * math.sin(angle)
cy = speed * math.cos(angle)
#print(cx, cy)

# bullet new current position
x1 += cx
y1 += cy

print(x1, y1)

编辑:循环示例

编辑:它需要在abs() if abs(cx) < abs(dx) or abs(cy) < abs(dy):

顺便说一句:如果target不改变位置或bullet不改变angle那么你只能计算cx,cy一次。

import math

def move(x1, y1, x2, y2, speed):

    # distance
    dx = x2 - x1
    dy = y2 - y1

    # angle
    angle = math.atan2(dx, dy)
    #print(math.degrees(angle))

    # 
    cx = speed * math.sin(angle)
    cy = speed * math.cos(angle)
    #print(cx, cy)

    # if distance is smaller then `cx/cy`
    # then you have to stop in target.
    if abs(cx) < abs(dx) or abs(cy) < abs(dy):
        # move bullet to new position
        x1 += cx
        y1 += cy
        in_target = False
    else:
        # move bullet to target
        x1 = x2
        y1 = y2
        in_target = True

    return x1, y1, in_target

#--- 

speed = 10

# bullet position
x1 = 10
y1 = 0

# taget possition
x2 = 120
y2 = 10

print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))

in_target = False

while not in_target:
    x1, y1, in_target = move(x1, y1, x2, y2, speed)
    print('x: {:6.02f} | y: {:6.02f}'.format(x1, y1))

结果

x:  10.00 | y:   0.00
x:  19.96 | y:   0.91
x:  29.92 | y:   1.81
x:  39.88 | y:   2.72
x:  49.84 | y:   3.62
x:  59.79 | y:   4.53
x:  69.75 | y:   5.43
x:  79.71 | y:   6.34
x:  89.67 | y:   7.24
x:  99.63 | y:   8.15
x: 109.59 | y:   9.05
x: 119.55 | y:   9.96
x: 120.00 | y:  10.00

顺便说一句:如果target不改变位置或bullet不改变angle那么你只能计算cx,cy一次。

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