Problems with precalculated arrays.

Blitz3D Forums/Blitz3D Beginners Area/Problems with precalculated arrays.

Valgar(Posted 2004) [#1]
Hallo to all.
I've a problem related to arrays.
Actually i have a program that move object based on some points that i paint on the screen (something similar to a pathfinder...);after i click the start button the object follow those points.
All goes ok if i use abs,sin,cos,atan "on the fly"....
But i have noticed that after a wile or after more object on screen the speed of the program fall down......
So i have think that if i use precalculated tables of sin and cos may i speed up the program a bit.
But here's the problem.....
i use two array to store the sin and cos in this manner.
Dim Sin2#(360)
Dim Cos2#(360)
For loop=0 To 359
	Sin2#(loop)=Sin(loop)		
	Cos2#(loop)=Cos(loop)
Next

So i know every valor of the 360 possible angolation.
I have inserted this piece of code into my mani in this manner:
a\angolo=ATan2(a\pathy[a\current_path+1]-a\pathy[a\current_path],a\pathx[a\current_path+1]-a\pathx[a\current_path])
a\cx=a\cx+Cos2#(a\angolo)*a\velocita

The angle depends on a previous "atan2" so if i have a decimal angle......the approximation of the angle is calculated by blitz on the fly or i have to precalculate it too?
I tell this because if i start the movement of the object,when he reach some point (depends on the path that i have drawn...)i have a "array index out of bounds" error!
What can i do to resolve this problem?
I can't precalculate ALL the movement calculations because they depends on the path drawn....and the possibility are something infinite!
Many thanks.


Graythe(Posted 2004) [#2]
'Array index out of bounds' means that we are trying to access and array subscript that does not exist...

Dim NumberArray(1000)
Print NumberArray(1001)

Will produce the same error.

So somewhere there is an attempt to access a subscript beyond 360 (or possibly, I suppose, less than zero).


CyberHeater(Posted 2004) [#3]
With the speed of modern computers now. I wouldn't even bother with pre computed cos tables.

Get the computer to do em on the fly.


fredborg(Posted 2004) [#4]
Atan2 returns angles between -180 and 180, so you need to add 180 to a\angolo...Ie:
a\angolo=ATan2(a\pathy[a\current_path+1]-a\pathy[a\current_path],a\pathx[a\current_path+1]-a\pathx[a\current_path])
a\angolo = a\angolo+180.0
a\cx=a\cx+Cos2#(a\angolo)*a\velocita



Valgar(Posted 2004) [#5]
I have tried to add 180.0 at every angles but the object go out of the path.....mmm....don't figured out why i have an out of bounds.....
Another thing...is possible that my speed-down of the program is related to many lines and points drawn at the same time on the screen???


Floyd(Posted 2004) [#6]
Angles are unchanged when you add 360.

So if ATan2 is negative you add 360, e.g. -10 becomes +350.


Valgar(Posted 2004) [#7]
So what can i do is add 360 at every angles?
And make an array of 720 instead of only 360 right?


Valgar(Posted 2004) [#8]
YESSSSSSSSS!
It works!!!
I didn't know that adding 360 the angle did not change so that i didn't have negative numbers!
A simple question....where do you read those information?
Because i'm not much greath at maths,i want to know such things.....some maths books maybe?
Or some internet site?
Many thanks for the solution,the problem it was only negative numbers so if my array start at 0.....negative aren't in.


Valgar(Posted 2004) [#9]
The only problem is that after a while the speed of the movement come down,lookup tables yes or not...
So the problem isn't math....something screw up in the calculation...argh


CyberHeater(Posted 2004) [#10]
Post more of your code. We might be able to help.


Rob Farley(Posted 2004) [#11]
Why not add 360 and mod 360

So you end up with a = (a + 360) mod 360

which would return a value from 0 - 359

However, I would concur with the why bother?


Valgar(Posted 2004) [#12]
Ok,i have tested under another machine and that effect apparently don't happen...(i try to correct it...if i don't succeed i post the code[it's big....])
For the speed of the program...i think that if i store the xcoords and ycoords in an array i spped up a lot the whole procedure.
But it seems that something go wrong.
I have two function:one that create the object and assign it's starting value,and another that calculate the xcoords and ycoords and draw the object.
For storing the x and y coordinates i have dimensioned 2 arrays and i have inserted in the function that draw the object this line:
array(position)=xcoord
array2(position)=ycoord
position=position+1
So i think that the info are coorectly stored because every frame "position"isupgraded to store the new data.
After the animation stop i have a function that write to file the data in this manner
for x=0 to max_array_position
writestring (file,array(position))
writestring (file,array2(position))
position=position+1
next
closefile

But when i open the file in a text editor....i see that only the first x&y coords are written!the other value i see is a bunch of "0,0",something like the array position are empty(i think).
I try to write an example to show the procedure of storing data that i use:

createobject()

function move_object()
*the comand for calculating the coords*
drawimage image,posx,posy
array(array_position)=posx
array2(array_position)=posy
array_position=array_position+1
if animationstop=1 then stop
end function

*main*
create object
if i click button 1 move_object()
if i click button 2 save_coords()
if i click button 3 exit_program
*end main*

function save_coords()
*write file*
for a=0 to array_max_position
writestring (file,array(array_position))
writestring (file,array2(array_position))
array_position=array_position+1
next
closefile file


Someone know where's the error??

(the strange thing is that i use the SAME saving function for storing other coordinates and in this case works well...)


CyberHeater(Posted 2004) [#13]
function save_coords()
*write file*
for a=0 to array_max_position
writestring (file,array(array_position))
writestring (file,array2(array_position))
array_position=array_position+1
next
closefile file


surely you should be using the loop in the writestring function otherwise. Something like this.


function save_coords()
*write file*
for a=0 to array_max_position
    writestring (file,array(a))
    writestring (file,array2(a))
next
closefile file



Valgar(Posted 2004) [#14]
I have changed the function as you suggested but the same effect.....only the first arrays value are writed....the others "0,0"....
Maybe the error is in the moveobject function?
I increment the position of the two array inside the function move.....


Valgar(Posted 2004) [#15]
I think i have discovered the error...correct me if i'm wrong(i have read the output file and it seems to be ok.....this time the file is full of x&y values[don't know if they are correct :P].
The string that increment the array position MUST BE into the main loop!
otherwise only the first position is stored (don't know why.....the function move_object is always active!).


eBusiness(Posted 2004) [#16]
Did you declare array_position global? A new instance is made every time you run the function, unless it's a global.


Valgar(Posted 2004) [#17]
Yes is global.
But i think i drop the idea of precalculated arrays of x&y coords.
What can i do when the object on-screen are many-many more then one?
I can't make precalculated table for every object...it's a lot of work!
Another question(sorry if i bother you...):with the modern pc (from p2 350 to athlon 3000 ecc ecc)is better make calculation on the fly or storing info in memory and read after the calculation???
Example:i make 30 alien for a shoot-em-up....is better that every alien calculate the route when is onscreen or is better that every alien read the data stored in array when onscreen??
(It's simplier to make my code work on the fly rather than pracalculate everything....).
One last question (alleluja!):i have read many times that is better divide the game logic in 2 trunks (or more)
load time
calculation time
display time
Actually i made only 3 trunks but in this manner
1)load time
2)object creation (types and arrays)
3)display and route calculation

Maybe my speed-down of the program is related to this method??


CyberHeater(Posted 2004) [#18]
Example:i make 30 alien for a shoot-em-up....is better that every alien calculate the route when is onscreen or is better that every alien read the data stored in array when onscreen??
(It's simplier to make my code work on the fly rather than pre calculate everything....).


The maths ability of modern cpu are much faster then reading pre stored data in memory. I promise you that the delay in your program is not caused by this aspect.


eBusiness(Posted 2004) [#19]
If you should precalculate or not depends on lots of stuff. But in general, you should only precalculate if it runs too slow on the fly. Your trunk method is not a problem, but statistics show that when a game gets slower with time, it's very likely a memory leak, search trough everything you load, create or copy in the main loop, watch out for handle overwrites.

Edit: By the way, try running your program while monitoring the memory use with taskmanager.


CyberHeater(Posted 2004) [#20]
oldTime#=MilliSecs() 

For loop = 1 To 5000000
	a = Cos(loop)
Next
newtime# = MilliSecs()
dif# = newtime - oldtime 

Print "Still waiting!" + dif


Run this on your system.

My system can do around 5 million calcs a second or 83,000
calcs every frame assumming your game is running at 60 frames a sec.


Valgar(Posted 2004) [#21]
I have 1052.0 as a result with the above code.
I have tried to see the memory usege with task manager....nothing wrong at all!
But still if i wait some seconds (about 1 minute or 2) the speed fall down a lot.....
In the main loop i have many line,point,rect commands.
I run always in debug mode,windowed (not compiled it runs in the editor).
MMM...i try to implement a simple frame limiter (now i don't have one because this program isn't finished yet).


Valgar(Posted 2004) [#22]
I have tried the "createtimer=60 ..... waittimer(60)" but the speed loss is always here....
Another thing:i have noticed that EVERY graphical action slows down,every the mouse movements......if i cancel everything (using the cancel button)types,array,ecc ecc and start again....all go well until the same amount of time.....another thing.
I have two valuse for storing the speed of the object (a\speed for a types,b\speed for b types)and...if i load a path and when the animation of the path is started....if i create a path *BOING* the 2 speed sums itself!!!
But i NEVER make an addiction of the two valor,and they are stored in different variables!!!
Argh......


CyberHeater(Posted 2004) [#23]
Valgar,

You can send the code to me and I'll have a look at it if you want.
I'm no blitz expert buy two sets of eyes are better then one.

Check my profile for my email addy.


eBusiness(Posted 2004) [#24]
Maybe it's about time that you post the source code, unless anybody else have a qualified guess on the slowdown. For the two speeds summing, you shouldn't happen to do something like:
pos=pos+aspeed
pos=pos+bspeed
?

Or wasn't that what happens?


Valgar(Posted 2004) [#25]
I can post the code but it's commented in italian,and not very well written i think....
If the italian commentatios isn't a problem i post the source.
Only make sure to follow those steps.
The program is for creating path nodes (for the follow routine,or for routes..).
So the first time you run it don't load a file...create a new one and save it.
And load a bmp also,because if you don't the animation don't start.
After that you can load the file and if you can see the problem.
(I hope that you have the xlntii include...because i use them and i think that don't work without...)
here's the code:


I hope you don't have problem with the include files....
ps:i have run the debugger a while...but i have don't see anything strange....the speed valor is always correct....ArGh


Valgar(Posted 2004) [#26]
Argh..made an error..sorry.
Make sure that where you run the code you have a file called "puntatore.bmp";it's the mouse pointer and i think that you have a "MAV" if you don't have this in the same directory....


Augen(Posted 2004) [#27]
Valgar, Have you tried running with debug mode off? (You say above you run with debug mode always on.)


Valgar(Posted 2004) [#28]
Yes,i have tried to erase all text,rect,line command:also turned debug mode off.
It seems that the situation is a bit different.
I also have noticed that if i run in windowed mode,and when the speed come down..if i scroll the program window a bit over the monitor...the speed return to normal!


CyberHeater(Posted 2004) [#29]
valgar,

I managed to get it to work but don't experience any slowdown when I use the app for a couple of minutes.

Can you describe exactly the problem.


Valgar(Posted 2004) [#30]
Yes.
I create some object (i use a ball sprite of about 60*60 pixel).
After a wile i see that the balls...start skipping frames!
The effect is similar to the "slow motion" effect in max payne.
And if i load a path,animate it and AFTER i create a new path.....don't you see the speed of the object that grow up(but only when you are placing the nodes...).


CyberHeater(Posted 2004) [#31]
I think I saw your problem.

Delete all occurances of debuglog. I think it's slowing your program down.


Valgar(Posted 2004) [#32]
Yes....i have removed those occurance and it seems that don't slow down....
But i haven't see any relation about the 2 speed.
Strange that if you don't see any slowdown even with debuglog...about the small test that you send me...the valor that i have i correct or do you think is too low?
I have an athlon 2100+ (toroughbed i think....)512 ddr400 ram (cas 2.5) radeon 9700pro 128 meg.


CyberHeater(Posted 2004) [#33]
Not sure about the summing of the 2 speeds. I had to hack your code somewhat because the IO_FileRequest$ caused a error on my machine.


Valgar(Posted 2004) [#34]
The strange thing is that i have traked every variable trough the code with the debugger and the speed variable are always correct! -_-'
(thanks for the help)


CyberHeater(Posted 2004) [#35]
I got this to work but still haven't seen the speed up issue.
Please explain exactly how you created this issue.


Valgar(Posted 2004) [#36]
Ok.(excuse but i'm watching a film on the tv :P ).
Start the program.
Load an image.
Start a new path (a max of 10 points you stop at the 9 because the 10 is the first[for a ciclic route]),then try the new path....and while the object follow the path...try to start another new path......immediately after you hit the start button...you notice that the speed of the object that are onscreen is doubled!
This bug applyies with loaded path also.....after you hit the button for starting the new path......the speed return to normal.


Valgar(Posted 2004) [#37]
(I think that the speed down problem is related to the math formula of the movement.....i have noticed that with some speed value the movement is not affected while with other value is affected.....mmm....complex solution :P )