Coordinates question

I think I wrote a really poor piece of code (though it seems to work just fine). Can anyone tell me if there is a better way to do this?

[code]
'find what direction (dir) the NPC needs to face to be facing the hero
Dim dir As Integer (0 = right, 1 = left, 2 = up, 3 = down)

Dim distanceX = Hero.CurrentX - NPC(i).CurrentX
Dim distanceY = Hero.CurrentY - NPC(i).CurrentY

If Math.Abs(distanceX) < Math.Abs(distanceY) Then
If Math.Sign(distanceX) = -1 Then
dir = 1
ElseIf Math.Sign(distanceX) = 1 Then
dir = 0
ElseIf Math.Sign(distanceX) = 0 Then
If Math.Sign(distanceY) = -1 Then
dir = 1
ElseIf Math.Sign(distanceY) = 1 Then
dir = 0
End If
End If
Else
If Math.Sign(distanceY) = -1 Then
dir = 3
ElseIf Math.Sign(distanceY) = 1 Then
dir = 2
ElseIf Math.Sign(distanceY) = 0 Then
If Math.Sign(distanceX) = -1 Then
dir = 2
ElseIf Math.Sign(distanceX) = 1 Then
dir = 3
End If
End If
End If

ChangeNPCFacing(i, dir)
[/code]

Comments

  • seancampbellseancampbell Pennsylvania, USA
    First off, I think you're going to get way more quality help if you start posting your code questions in a game development forum.

    http://www.gamedev.net/ is a preferred site for me. Their Forums are top quality.

    When you write your logic for your game, you want to do things in as little if statements, and as little calculations as possible to speed up the processing time. In this case, you could simply compare distanceX and Y against 0 to check if they are positive or negative, rather than use Math.Sign(). Also, I don't think you needed all of those extra if statements that were there so I removed it and cleaned up the logic. I feel this would work.

    One last thing, make sure you declare types for your variables (DistanceX and Y don't have types, they are declared as Variants). Variants are very slow to process values since they are made to handle any datatype, and have extra processor checks whenever they are used. I used Double since I didn't know if X and Y were ints or doubles.

    [code]
    'find what direction (dir) the NPC needs to face to be facing the hero
    Dim dir As Integer = 0 '(0 = right, 1 = left, 2 = up, 3 = down)

    Dim distanceX As Double = Hero.CurrentX - NPC(i).CurrentX
    Dim distanceY As Double = Hero.CurrentY - NPC(i).CurrentY

    If Math.Abs(distanceX) < Math.Abs(distanceY) Then
    'if the distance of X is less than Y then face up or down
    If distanceY > 0 Then
    'if it's positive face down
    dir = 3
    Else
    'if it's negative or 0 then face up
    dir = 2
    End If
    Else
    'if distance of X is more than or equal to Y then face left or right
    If distanceX > 0 Then
    'if its positive, face right
    dir = 0
    Else
    'if its negative or 0, face left
    dir = 1
    End If
    End If

    ChangeNPCFacing(i, dir)
    [/code]
  • Thanks Sean,

    That sloppy programming (not declaring those variables as Integers) was the result of me hurriedly copying the code as my GF showed up unexpectedly at my front door. I'm normally very good about my explicit declarations, especially considering my QBasic background :)

    That said, I'm still going to look through the 2400+ lines of code I've written to make sure I don't have any unexpected variants running around slowing down my program's execution.

    The code changes you made make a lot of sense. I could tell I was using far too many if statements to do something that shouldn't have been as hard as I was making it.

    I'm subscribed to GameDev, but, to be honest, I was worried I'd get laughed out of there and called a newb (which I kinda am). I'll pop over there again and take a fresh look at the site.

    Also, I'm only using an 11x11 game grid. Do you think it would be in my interest to change those Xs and Ys to shorts instead of ints?

    -Gavin
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories