【问题标题】:finding change in x and y given two points and length of vector在给定两个点和向量长度的情况下找到 x 和 y 的变化
【发布时间】:2017-12-20 00:43:10
【问题描述】:

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

现在我拥有所有这些代码。

cx 和 cy 标记玩家或第一点的中心 而 MouseX 和 MouseY 代表鼠标点击的坐标

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 画布项目,因此它们必须移动整数)

【问题讨论】:

  • 这张图片可能对你来说太难了 - Calculate position having angle and speed or distance。如果你有speedangle,那么你必须使用sin()cos()来计算delta_xdelta_y,然后你必须将它添加到当前位置。
  • 太在两点之间得到angle 你可能不得不使用 ``math.atan2(dy, dx)` - 它以弧度返回anglesin()/cos() 也需要弧度。
  • 哦,好吧,这很有意义。谢谢! @furas

标签: python python-2.7 tkinter vector


【解决方案1】:

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

编辑: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() in if abs(cx) &lt; abs(dx) or abs(cy) &lt; 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 一次。

【讨论】:

  • 我用循环添加示例
  • 更正:在if abs(cx) &lt; abs(dx) or abs(cy) &lt; abs(dy):中需要abs()
  • 顺便说一句:如果target 不改变位置或bullet 不改变angle 那么你只能计算cx,cy 一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多