A bit of coding help

  • plypencil
    5th Aug 2010 Member 0 Permalink
    I'm making another physics toy, this is the loop which transmits air pressure to the surrounding squares. However The whole screen almost instantly fills up with air as soon as I put 1 bit of air into the simulation and the program crashes, the number is too high for the int64. I cannot see where I am going wrong, maybe one of you can see?

    For x As Integer = 1 To 78
    For y As Integer = 1 To 58
    If terrain(x, y).type = "air" Then
    Dim possiblemove As Integer = 1
    Dim pressuretotal As Decimal
    If terrain(x + 1, y).type = "air" Then possiblemove += 1 : pressuretotal += terrain(x + 1, y).pressure
    If terrain(x - 1, y).type = "air" Then possiblemove += 1 : pressuretotal += terrain(x - 1, y).pressure
    If terrain(x, y + 1).type = "air" Then possiblemove += 1 : pressuretotal += terrain(x, y + 1).pressure
    If terrain(x, y - 1).type = "air" Then possiblemove += 1 : pressuretotal += terrain(x, y - 1).pressure
    Dim unitpressure As Decimal = (pressuretotal / possiblemove)
    terrain(x, y).tpressure += unitpressure
    If terrain(x + 1, y).type = "air" Then terrain(x + 1, y).tpressure += unitpressure
    If terrain(x - 1, y).type = "air" Then terrain(x - 1, y).tpressure += unitpressure
    If terrain(x, y + 1).type = "air" Then terrain(x, y + 1).tpressure += unitpressure
    If terrain(x, y - 1).type = "air" Then terrain(x, y - 1).tpressure += unitpressure
    End If
    Next
    Next

    For x As Integer = 1 To 78
    For y As Integer = 1 To 58
    terrain(x, y).pressure = terrain(x, y).tpressure
    terrain(x, y).tpressure = 0
    Next
    Next
  • Felix
    5th Aug 2010 Member 0 Permalink
    I am thinking infinite loops. Add some debugging stuff, like print out variable contents to console and such.
  • plypencil
    5th Aug 2010 Member 0 Permalink
    Its not an infinite loop, the screen does update but because im running 60fps....

    The problem is the math in that block, somehow I am getting MORE out than im putting in Sorry Einstein
  • Felix
    5th Aug 2010 Member 0 Permalink
    terrain(x + 1, y).pressure
    Together with:
    For y As Integer = 1 To 58
    For x As Integer = 1 To 78

    This is a loop, right? I don't know VB. I think that in there is your problem
  • plypencil
    5th Aug 2010 Member 0 Permalink
    Its cycling through the x and y axis of the screen, spreading any air out there is in the simulation.
  • Felix
    5th Aug 2010 Member 0 Permalink
    Then that is probably the problem.

    Then possiblemove += 1
    That also adds one to possiblemove, can't see a reason for this. Without knowing your code atleast.
  • plypencil
    5th Aug 2010 Member 0 Permalink
    Because itself is a position.

    If it can move only right, it can still have air in itself. So its 1 + 1 direction. So if you have 100 it will be split between the 2 with 50 each. if there were 3 possible directions it would be 33 each.

    Its basically air physics but without wind
  • TPTlover1012
    7th Aug 2010 Banned 0 Permalink
    This post is hidden because the user is banned