As I go through GLBasic, trying to see to what limits BASIC can represent OO principles, I am discovering amazing things about the TYPE datatype.

Since I have not seen any documentation or references to the self referencing of TYPE, I have decided to share this little code snippit.

Take special note of the line

FUNCTION add: vec AS VECTOR3

which is a function within the declaration of the VECTOR3 TYPE definition. This is so much like a class or a structure within c++ or c referencing itself.

I certainly hope that this type of feature gets documented soon, since a newcomer to GLBasic has no way of knowing that these sort of things are possible.

`// --------------------------------- //`

// Project: vector3

// Author : jestermon

// --------------------------------- //

TYPE VECTOR3

x#

y#

z#

length#

FUNCTION set: x#, y#, z#

self.x# = x#

self.y# = y#

self.z# = z#

self.getLength()

ENDFUNCTION

FUNCTION getLength:

self.length# = SQR((self.x# * self.x#) + (self.y# * self.y#) + (self.z# * self.z#))

ENDFUNCTION

FUNCTION normalize:

self.getLength()

IF self.length# < 0.0000001 THEN self.length# = 1

self.x# = self.x#/self.length#

self.y# = self.y#/self.length#

self.z# = self.z#/self.length#

self.getLength()

ENDFUNCTION

FUNCTION setLength: newLength#

self.normalize()

self.x# = self.x# * newLength#

self.y# = self.y# * newLength#

self.z# = self.z# * newLength#

self.getLength()

ENDFUNCTION

FUNCTION add: vec AS VECTOR3

self.x# = self.x# + vec.x#

self.y# = self.y# + vec.y#

self.z# = self.z# + vec.z#

self.getLength()

ENDFUNCTION

ENDTYPE

LOCAL v AS VECTOR3

LOCAL v2 AS VECTOR3

v.set(2,4,3)

PRINT v.length, 0,0

v2.set(3,2,5)

PRINT v2.length, 0,20

v.add(v2)

PRINT v.length, 0,40

SHOWSCREEN

MOUSEWAIT

EDIT:

I have completed a TYPE encapsulation of the VECTOR3 methods (functions), which make like a heck of a lot easier (for me at least), by everything as a member function of the TYPE (class), and having it all in one place. I've shared the VECTOR3 TYPE here, for anyone else who might find it helpful

`// --------------------------------- //`

// Project: vector3

// Author : jestermon

// --------------------------------- //

TYPE VECTOR3

x#

y#

z#

length#

FUNCTION set: x#, y#, z#

self.x# = x#

self.y# = y#

self.z# = z#

self.getLength()

ENDFUNCTION

FUNCTION getLength#:

self.length# = SQR((self.x# * self.x#) + (self.y# * self.y#) + (self.z# * self.z#))

RETURN self.length#

ENDFUNCTION

FUNCTION normalize:

self.getLength()

IF self.length# < 0.0000001 THEN self.length# = 1

self.x# = self.x#/self.length#

self.y# = self.y#/self.length#

self.z# = self.z#/self.length#

self.getLength()

ENDFUNCTION

FUNCTION setLength: newLength#

self.normalize()

self.x# = self.x# * newLength#

self.y# = self.y# * newLength#

self.z# = self.z# * newLength#

self.getLength()

ENDFUNCTION

FUNCTION add: vec AS VECTOR3

self.x# = self.x# + vec.x#

self.y# = self.y# + vec.y#

self.z# = self.z# + vec.z#

self.getLength()

ENDFUNCTION

FUNCTION subtract: vec AS VECTOR3

self.x# = self.x# - vec.x#

self.y# = self.y# - vec.y#

self.z# = self.z# - vec.z#

self.getLength()

ENDFUNCTION

FUNCTION invert:

self.x# = - self.x#

self.y# = - self.y#

self.z# = - self.z#

self.getLength()

ENDFUNCTION

FUNCTION multiply: vec AS VECTOR3

self.x# = self.x# * vec.x#

self.y# = self.y# * vec.y#

self.z# = self.z# * vec.z#

self.getLength()

ENDFUNCTION

FUNCTION divide: vec AS VECTOR3

self.x# = self.x# / vec.x#

self.y# = self.y# / vec.y#

self.z# = self.z# / vec.z#

self.getLength()

ENDFUNCTION

FUNCTION dot#: vec AS VECTOR3

LOCAL res# = self.x#*vec.x#+self.y#*vec.y#+self.z#*vec.z#

RETURN res#

ENDFUNCTION

FUNCTION cross: vec AS VECTOR3

LOCAL v AS VECTOR3

v.x# = self.y#*vec.z#

v.y# = self.z#*vec.x#

v.z# = self.x#*vec.y#

self.x# = v.x#

self.y# = v.y#

self.z# = v.z#

self.getLength()

ENDFUNCTION

FUNCTION distance#: vec AS VECTOR3

self.subtract(vec)

RETURN self.length#

ENDFUNCTION

FUNCTION direction: vec AS VECTOR3

LOCAL v AS VECTOR3

v.x# = self.x#

v.y# = self.y#

v.z# = self.z#

vec.subtract(v)

self.x# = vec.x#

self.y# = vec.y#

self.z# = vec.z#

self.getLength()

ENDFUNCTION

FUNCTION scalar#: scale#

self.x# = self.x# * scale#

self.y# = self.y# * scale#

self.z# = self.z# * scale#

self.getLength()

RETURN self.length#

ENDFUNCTION

FUNCTION angle#: vec AS VECTOR3

LOCAL div, dotv ,l1,l2

l1 = self.getLength()

l2 = vec.getLength()

div = l1 * l2

IF div = 0 THEN RETURN 0

dotv = self.dot(vec) / div

IF ABS(dotv) > 1 THEN RETURN 0

RETURN ACOS(dotv)

ENDFUNCTION

FUNCTION reflect: normal AS VECTOR3

LOCAL dotv#

self.normalize()

normal.normalize()

dotv# = self.dot(normal)

normal.scalar(dotv#)

normal.scalar(2.0)

self.subtract(normal)

ENDFUNCTION

ENDTYPE