本文介绍了给定两个点和向量长度,找到 x 和 y 的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a bullet shot from the first point(the player location), and it needs to travel towards the second point(the mouse click), at a given speed.

现在我有所有这些代码.

Right now I have all this code.

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

cx and cy marks center of player or point onewhile MouseX and MouseY represent the cordinates of the mous click

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

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

It is extremely messy and causes my bullets to increase in speed when shot up or down, and go slow when shot to the left or right.

但是,我不知道我应该怎么做才能更改代码以允许我的子弹以相同的速度行进,而不管射击的角度如何.如果有人擅长数学和 tkinter 等可以提供帮助,那将不胜感激.

However, i am at a loss at what i should do to change the code to allow my bullets to travel at the same speed regardless of the angle shot. If someone good at math and tkinter and such could help that would so greatly appreciated.

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

(As a side note , all my objects are Tkinter canvas items so they must move by whole integers)

推荐答案

希望我没有弄错 - 我不太记得了.

I hope I didn't make mistake - I don't remeber it too well.

atan2(dy, dx) 有错误 - 必须是 atan2(dx, dy)

there was mistake in atan2(dy, dx) - has to be atan2(dx, dy)

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

It calculate new position (after one frame/move) if you have bullet's position and speed, and target'sposition.

你必须重复一遍

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)

顺便说一句:如果 target 没有改变位置或者 bullet 没有改变 angle 那么你可以只计算一次 cx,cy.

BTW: if target doesn't change place or bullet doesn't change angle then you can calculate cx,cy only once.

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.

BTW: if target doesn't change place or bullet doesn't change angle then you can calculate cx,cy only once.

这篇关于给定两个点和向量长度,找到 x 和 y 的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:06