Visual Studio 的优化问题或我看不到的逻辑错误?

发布于 2024-10-15 08:10:11 字数 2629 浏览 5 评论 0原文

我正在开发一个小型物理玩具应用程序。它工作得很好,除了粒子不会相互推开,只会拉向,我逐个命令地调试了该子程序,并意识到值“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你列表最软的妹 2024-10-22 08:10:11

心灵调试告诉我你有Option Strict Off。我强烈建议您在使用 VB.NET 时始终使用 Option Strict On,除非您确实需要 Off 的后期绑定功能。如果您有Option Strict On,您将得到

错误 BC30512:Option Strict On 不允许从“Double”到“Integer”的隐式转换

错误 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 have Option Strict On, unless you actually need the late binding functionality of Off. If you have Option Strict On, you will get

error BC30512: Option Strict On disallows implicit conversions from 'Double' to 'Integer'

on that assignment of the result of Math.Sqrt to h. As @Marc Gravell alludes to, there is massive potential for a rounding issue here, since h is an Integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文