Handling Variables...

Advanced game related topics
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Handling Variables...

Post 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?
User avatar
Azul
Enthusiast
Enthusiast
Posts: 109
Joined: Fri Dec 29, 2006 9:50 pm
Location: Finland

Post 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)
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Code: Select all

angle = 370
debug angle % 360
I love Purebasic.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Heathen wrote:

Code: Select all

angle = 370
debug angle % 360
Doesn't work if angle goes below 0!

try

Code: Select all

angle = -1
Debug (angle+360) % 360
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Derek wrote:
Heathen wrote:

Code: Select all

angle = 370
debug angle % 360
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

Last edited by Heathen on Thu Jan 25, 2007 5:51 am, edited 2 times in total.
I love Purebasic.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

I use WrapValue
Please correct my english
http://purebasic.developpez.com/
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Dang Kaeru! I am learning things everytime I read but you have it down! :shock:
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

hu :oops: thanx for the flowers...
oh... and have a nice day.
Post Reply