Handling Variables...
Handling Variables...
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?
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?
One way would be make procedure which will return checked value
update: it does not matter if you try r360(20+6804)
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)
Doesn't work if angle goes below 0!Heathen wrote:Code: Select all
angle = 370 debug angle % 360
try
Code: Select all
angle = -1
Debug (angle+360) % 360
That wont work for really low numbers, try this.Derek wrote:Doesn't work if angle goes below 0!Heathen wrote:Code: Select all
angle = 370 debug angle % 360
tryCode: Select all
angle = -1 Debug (angle+360) % 360
Code: Select all
angle = -9999
debug (angle % 360 + 360) % 360
Last edited by Heathen on Thu Jan 25, 2007 5:51 am, edited 2 times in total.
I love Purebasic.
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
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:
BinBaseAngle - 4096° is a full circle
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.
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 )Code: Select all
Angle = 372 * 4096 / 360
Angle & 4095
VectX = Sin( Angle * #PI / 2048 )
VectY = Cos( Angle * #PI / 2048 )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.
oh... and have a nice day.
- Rook Zimbabwe
- Addict

- Posts: 4322
- Joined: Tue Jan 02, 2007 8:16 pm
- Location: Cypress TX
- Contact:
- Kaeru Gaman
- Addict

- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
