Visual Studio 的优化问题或我看不到的逻辑错误?
我正在开发一个小型物理玩具应用程序。它工作得很好,除了粒子不会相互推开,只会拉向,我逐个命令地调试了该子程序,并意识到值“H”不会改变,无论它在第一次通过时设置为什么sub 是它保留的内容,更改该值的唯一方法是手动设置它,即“h = 1”。一旦对“H”值重新进行计算,它就会重置为之前的值,即使 x1、y1、x2、y2 都不同,因此意味着 H 应该不同。
我认为是我在某个地方犯了数学错误,但我看不出它在哪里。我需要一双新的眼睛来审视我的工作。如果您发现任何东西,请告诉我。
谢谢。
Public Sub movenodes()
For i As Integer = 0 To connectionnumber
If connections(i).exists = True Then
Dim n1x As Integer
Dim n2x As Integer
Dim n1y As Integer
Dim n2y As Integer
Dim h As Integer
n1x = nodes(connections(i).node1).x
n2x = nodes(connections(i).node2).x
n1y = nodes(connections(i).node1).y
n2y = nodes(connections(i).node2).y
h = 1
h = Math.Sqrt(((nodes(connections(i).node1).x + nodes(connections(i).node2).x) ^ 2) + ((nodes(connections(i).node1).y + nodes(connections(i).node2).y) ^ 2))
Me.Text = nodes(connections(i).node1).x & " " & nodes(connections(i).node1).y & " " & h
If h > connections(i).happy Then
If n1x > n2x Then
nodes(connections(i).node1).hspeed -= 0.1
nodes(connections(i).node2).hspeed += 0.1
ElseIf n1x < n2x Then
nodes(connections(i).node1).hspeed += 0.1
nodes(connections(i).node2).hspeed -= 0.1
End If
If n1y > n2y Then
nodes(connections(i).node1).vspeed -= 0.1
nodes(connections(i).node2).vspeed += 0.1
ElseIf n1y < n2y Then
nodes(connections(i).node1).vspeed += 0.1
nodes(connections(i).node2).vspeed -= 0.1
End If
ElseIf h < connections(i).happy Then
MsgBox("")
If n1x > n2x Then
nodes(connections(i).node1).hspeed += 0.5
nodes(connections(i).node2).hspeed -= 0.5
ElseIf n1x < n2x Then
nodes(connections(i).node1).hspeed -= 0.5
nodes(connections(i).node2).hspeed += 0.5
End If
If n1y > n2y Then
nodes(connections(i).node1).vspeed += 0.5
nodes(connections(i).node2).vspeed -= 0.5
ElseIf n1y < n2y Then
nodes(connections(i).node1).vspeed -= 0.5
nodes(connections(i).node2).vspeed += 0.5
End If
End If
End If
Next
End Sub
I have an small physics toy application I am developing. It works fine except the particles will not push each other away, only pull towards, I debugged that sub going through it command by command and realised the value 'H' would not change, what ever it was set to during the first pass through the sub is what it kept, the only way to change this value is to manually set it i.e 'h = 1'. Once the calculation is redone on the 'H' value it resets to what it was previously, even though the x1,y1,x2,y2 are all different, thus meaning H should be different.
I think it is me that has made a mathematical mistake somewhere, but I cannot see where it is. I need a fresh pair of eyes to look over my work. Please let me know if you find anything.
Thanks.
Public Sub movenodes()
For i As Integer = 0 To connectionnumber
If connections(i).exists = True Then
Dim n1x As Integer
Dim n2x As Integer
Dim n1y As Integer
Dim n2y As Integer
Dim h As Integer
n1x = nodes(connections(i).node1).x
n2x = nodes(connections(i).node2).x
n1y = nodes(connections(i).node1).y
n2y = nodes(connections(i).node2).y
h = 1
h = Math.Sqrt(((nodes(connections(i).node1).x + nodes(connections(i).node2).x) ^ 2) + ((nodes(connections(i).node1).y + nodes(connections(i).node2).y) ^ 2))
Me.Text = nodes(connections(i).node1).x & " " & nodes(connections(i).node1).y & " " & h
If h > connections(i).happy Then
If n1x > n2x Then
nodes(connections(i).node1).hspeed -= 0.1
nodes(connections(i).node2).hspeed += 0.1
ElseIf n1x < n2x Then
nodes(connections(i).node1).hspeed += 0.1
nodes(connections(i).node2).hspeed -= 0.1
End If
If n1y > n2y Then
nodes(connections(i).node1).vspeed -= 0.1
nodes(connections(i).node2).vspeed += 0.1
ElseIf n1y < n2y Then
nodes(connections(i).node1).vspeed += 0.1
nodes(connections(i).node2).vspeed -= 0.1
End If
ElseIf h < connections(i).happy Then
MsgBox("")
If n1x > n2x Then
nodes(connections(i).node1).hspeed += 0.5
nodes(connections(i).node2).hspeed -= 0.5
ElseIf n1x < n2x Then
nodes(connections(i).node1).hspeed -= 0.5
nodes(connections(i).node2).hspeed += 0.5
End If
If n1y > n2y Then
nodes(connections(i).node1).vspeed += 0.5
nodes(connections(i).node2).vspeed -= 0.5
ElseIf n1y < n2y Then
nodes(connections(i).node1).vspeed -= 0.5
nodes(connections(i).node2).vspeed += 0.5
End If
End If
End If
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
心灵调试告诉我你有
Option Strict Off
。我强烈建议您在使用 VB.NET 时始终使用Option Strict On
,除非您确实需要Off
的后期绑定功能。如果您有Option Strict On
,您将得到错误 BC30512:Option Strict On 不允许在将
Math.Sqrt
的结果赋值给h
时 。正如 @Marc Gravell 提到的,这里存在巨大的舍入问题的可能性,因为h
是一个Integer
。Psychic debugging tells me you have
Option Strict Off
. I would urge you when using VB.NET to always haveOption Strict On
, unless you actually need the late binding functionality ofOff
. If you haveOption Strict On
, you will geton that assignment of the result of
Math.Sqrt
toh
. As @Marc Gravell alludes to, there is massive potential for a rounding issue here, sinceh
is anInteger
.