You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Basic中计算子弹的归一化向量?

VB中子弹归一化向量计算问题及解决过程

在Visual Basic中,X轴向右递增,Y轴向下递增,不确定Point是否能接收双精度值,这可能是个问题。尝试两种计算子弹归一化向量的方法,结果却近乎随机。

第一种尝试方法

Dim mousePosX As Integer
Dim mousePosY As Integer
Dim extAngle As Double
Dim intAngle As Double
Dim xVelo As Double
Dim yVelo As Double

mousePosX = e.X - 385
mousePosY = e.Y - 385

extAngle = Math.Atan(mousePosX / mousePosY) * (180 / Math.PI)
intAngle = Math.Atan(mousePosY / mousePosX) * (180 / Math.PI)

xVelo = ((5 * ((Math.Sin(extAngle) * (180 / Math.PI)))) / (Math.Sin(90) * (180 / Math.PI))) * Math.Sign(mousePosX)
yVelo = ((5 * ((Math.Sin(intAngle) * (180 / Math.PI)))) / (Math.Sin(90) * (180 / Math.PI))) * Math.Sign(mousePosY)

bullets(bulletNum).Location = New Point(385, 385) 'Resets the bullet location to the position of the player

bulletVector(bulletNum) = New Point(xVelo, yVelo)

该方法利用直角三角形运算求内外角度,再用正弦定理计算斜边为5时的X、Y向量长度,结果却近乎随机。

第二种尝试方法

Dim hypo As Double
Dim ratio As Double
Dim mousePosX As Integer
Dim mousePosY As Integer

mousePosX = e.X - 385
mousePosY = e.Y - 385

hypo = Math.Sqrt((mousePosX ^ 2) + (mousePosY ^ 2))
ratio = Math.Abs(5 / hypo)
bullets(bulletNum).Location = New Point(385, 385)

bulletVector(bulletNum) = New Point((mousePosX * ratio), (mousePosY * ratio))

该方法通过实际斜边与目标斜边(5)的比例缩放X、Y长度,但同样无效。

编辑尝试

Dim hypo As Double
Dim mousePosX As Integer
Dim mousePosY As Integer

mousePosX = e.X - 385
mousePosY = e.Y - 385

hypo = Math.Sqrt((mousePosX ^ 2) + (mousePosY ^ 2))
bullets(bulletNum).Location = New Point(385, 385)

'bulletVector(bulletNum) = New Point(((mousePosX / hypo) * 5), ((mousePosY / hypo) * 5))
bulletVector(bulletNum) = New Point(mousePosX / Math.Abs(mousePosX), mousePosY / Math.Abs(mousePosY))

尝试归一化向量,即使仅判断象限也存在问题,推测是对VB机制的误解而非技术bug。

最终解决方法

Dim coordinates As Point

coordinates = Me.PointToClient(MousePosition)
mousePosX = coordinates.X - 385
mousePosY = coordinates.Y - 385

发现无需通过eventargs获取鼠标位置,使用该方法在视差背景下更合适,问题最终解决。

内容的提问来源于stack exchange,提问作者Jack Anderson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:33:24