Weird Problem - Stuck and Desperate!

Blitz3D Forums/Blitz3D Beginners Area/Weird Problem - Stuck and Desperate!

Spomo(Posted 2004) [#1]
Hello folks,

I'm working on a space shooter; and here's my problem.

I am basically throwing some alien ships on the screen at random X and Y locations. Now, we all know that this method can cause some of the images to overlap. However, I am trying to write a small routine that checks each alien against one another and if they are within 50 pixels of each other on the Y plane, then one of them gets moved.

I wrote the function which *should* work. In fact, when I execute it and use some "print" statements to check my variables to make sure the math and comparisons are working properly, all is well. The code works as I intended.

HOWEVER.....

When I transfer the code into my Space Game engine, overlap does occur. Can someone explain to me why it works fine when I use "print" to display the co-ordinates but it misbehaves when I'm blitting to the screen?

Here is the code:

;----------
Function Initialize_Aliens ()

Alien_Count = 0
SeedRnd MilliSecs()

For Alien_Index = 1 To NUM_ALIENS
Aliens(Alien_Index)=New Alien
Aliens(Alien_Index)\Alien_X=800+(Rnd(0,500))
Aliens(Alien_Index)\Alien_Y=100+(Rnd(0,500))
Aliens(Alien_Index)\Alive=1
Alien_Count = Alien_Count+1
Next

;Below is the routine that is giving me the problems!!;

For Alien_Index = 1 To NUM_ALIENS
For Alien_Work_Counter= 1 To NUM_ALIENS

Alien_Math=Aliens(Alien_Index)\Alien_Y-Aliens (Alien_Work_Counter)\Alien_Y

(NOTE: That line above is all on one line in my program. This website won't let me post it as one line...)

Alien_Math=Abs(Alien_Math)


If Alien_Math <= 50 And Alien_Index <> Alien_Work_Counter Then Aliens(Alien_Index)\Alien_Y=Aliens(Alien_Index)\Alien_Y+50

(Please note that the above line beginning with "If Alien_Math <= 50" is also all on one line in my editor.)

Next
Next

End Function

;--------
As you can see, I am subtracting one Y position from another and then using the ABS command to turn it positive. If that number is less than or equal to 50, I increase the value of the Y position of the Alien Craft by 50.

Again, it works using "print" to check the variables. It does not work correctly when I blit my bitmaps to the screen.

So....

What am I doing wrong?

Thanks to all in advance.

-Spo-

PS: I do have all my variables defined as global, so I can access them throughout the program. (Yes, I know this is sloppy..... )


soja(Posted 2004) [#2]
Well, it seems to me that when you change the position of an alien, it's possible that you're moving it onto another alien that you already checked (and won't check again). Of course I could be getting mixed up, but it seems like that's probably what's happening.

Another method you might try (off the top of my head) is to use a linked list representing pixels on the Y axis, and delete the appropriate list nodes when their "pixels" become unavailable. Something like this:
Type y_pixel
  Field y
End Type

For i = 0 To MAX_Y
  temp.y_pixel = New y_pixel
  temp\y = i
  available% = available + 1
Next

; then...
; for each alien ship...
; Pick a random number from 0 to "available" (we'll call it N)
; iterate from First y_pixel to Nth y_pixel
; create the alien ship at that y_pixel\y value
; delete that type and the 50 types Before and After (or until start or end of list)
; for each type deleted, subtract 1 from "available"

The advantage to this method is that you don't have to "keep trying" to find the right coordinate, and also you know that every type that's in the list is available.