01-09-2023, 06:53 PM
(01-09-2023, 05:26 PM)james246 Wrote: Using vector math would really open up some possibilities. Definitely over my head but this seems like the way to go for a good 3d physics engine.
You already are, truth be told. The only difference is your code handles the vectors as individual x/y/z components.
The main possibility is less overall typing and more succinct code. It's just a matter of thinking of those individual components as whole objects and writing some subroutines to handle the individual components of those objects.
I notice your SUB processterrain is doing just that. In the DO...LOOP, it's mostly scalar multiplications of, and additions to vectors, which could be SUB'ed out to something that would handle the grunt work. All that would be needed is a vector TYPE, say...
TYPE Vector
x AS SINGLE
y AS SINGLE
z AS SINGLE
END TYPE
A subroutine to do the multiplications
SUB MultVec (v as Vector, m AS SINGLE)
v.x = v.x * m
v.y = v.y * m
v.z = v.z * m
END SUB
Then just DIM AS Vector vec1, vec2, vec3 ' in the SUB
Suddenly:
x1 = x1 * scale1: y1 = y1 * scale1: 'z1 = z1 * scale1
x2 = x2 * scale1: y2 = y2 * scale1: 'z2 = z2 * scale1
x3 = x3 * scale1: y3 = y3 * scale1: 'z3 = z3 * scale1
becomes:
MultiVec vec1, scale1
MultiVec vec2, scale1
MultiVec vec3, scale1
Then you can do the same to the vector additions. Change shx, shy & shz to a single vector type: sh.x, sh.y & sh.z. Write a sub to add two vectors together:
SUB AddVec (v1 as Vector, v2 AS Vector)
v1.x = v1.x + v2.x
v1.y = v1.y + v2.y
v1.z = v1.z + v2.z
END SUB
Now:
x1 = x1 + shx: y1 = y1 + shy: z1 = z1 + shz
x2 = x2 + shx: y2 = y2 + shy: z2 = z2 + shz
x3 = x3 + shx: y3 = y3 + shy: z3 = z3 + shz
becomes:
AddVec vec1, sh
AddVec vec2, sh
AddVec vec3, sh
A little more upfront work for a big payoff in short code, more descriptive variable naming, etc.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
sha_na_na_na_na_na_na_na_na_na: