Page 1 of 1
Handling Variables...
Posted: Wed Jan 24, 2007 8:55 am
by GBeebe
I know that this isn't necessarily a Game Programming question, but since i'm using it in my game i'll ask here.
In VB there was a way to handle all changes to a Variable, basically by defining the variable as a new type and that had code to handle all the changes to it... It's been a while since i've used VB so I don't remember what it was called, but I seem to remember that I had Get and Set functions.
Anywho, I want to make an Angle variable type, and when there's a change to a variable of that type, the Procedure checks to see if it is > 360 or < 0 and fixes it so it falls within 0 to 360.
so it would work like this:
Gun.Angle = 300 + 70
Debug Gun.Angle ; would display 10
Any ideas?
Posted: Wed Jan 24, 2007 9:03 am
by Azul
One way would be make procedure which will return checked value
Code: Select all
Procedure r360(angle)
While angle<0 Or angle>359
If angle<0 : angle+360 : EndIf
If angle>359 : angle-360 : EndIf
Wend
ProcedureReturn angle
EndProcedure
Debug r360(300+70)
Debug r360(300+1340)
update: it does not matter if you try r360(20+6804)
Posted: Wed Jan 24, 2007 9:50 am
by Heathen
Posted: Wed Jan 24, 2007 10:09 am
by Derek
Doesn't work if angle goes below 0!
try
Code: Select all
angle = -1
Debug (angle+360) % 360
Posted: Wed Jan 24, 2007 10:23 am
by Heathen
Derek wrote:
Doesn't work if angle goes below 0!
try
Code: Select all
angle = -1
Debug (angle+360) % 360
That wont work for really low numbers, try this.
Code: Select all
angle = -9999
debug (angle % 360 + 360) % 360
Posted: Wed Jan 24, 2007 10:26 am
by Derek
Depends how much the angle is being changed. During a game you wouldn't expect it to change by much more than a degree or two so it would work fine.
If it were being changed by more than 360 degrees at a time then you're right it wouldn't work. Just depends on the situation.
Posted: Wed Jan 24, 2007 11:52 am
by Comtois
Posted: Wed Jan 24, 2007 2:47 pm
by Kaeru Gaman
some root-tip:
if you change the degrees of the full circle to some 2^n value,
you can easily work with & to cut the wrappings.
normal:
Code: Select all
Angle = 372
Angle % 360
VectX = Sin( Angle * #PI / 180 )
VectY = Cos( Angle * #PI / 180 )
BinBaseAngle - 4096° is a full circle
Code: Select all
Angle = 372 * 4096 / 360
Angle & 4095
VectX = Sin( Angle * #PI / 2048 )
VectY = Cos( Angle * #PI / 2048 )
of course you can take any 2^n value you like.
don't try to precalculate from classic degrees like i did in the first line,
it's just a waste of performance.
better think in 4096°-circles right from the start.
additionally, you can speed your calculations up using an Array for precalculated Sin-Values...
http://www.purebasic.fr/german/viewtopic.php?t=5884
PS:
and this is even more precise.
since you cant use floats with modulo (%) or bitwise and (&),
you'll have to use Longs, and then you would have only 1-degree-steps.
so 4096 or even 65536 degrees for a full circle are better than only 360.
Posted: Fri Feb 02, 2007 12:48 am
by Rook Zimbabwe
Dang Kaeru! I am learning things everytime I read but you have it down!

Posted: Fri Feb 02, 2007 2:46 pm
by Kaeru Gaman
hu

thanx for the flowers...