Blitz3D-mimicking matrix code

Blitz3D Forums/Blitz3D Programming/Blitz3D-mimicking matrix code

JoshK(Posted 2005) [#1]
Duplicates Blitz3D matrix results, with position, rotation, scale. The only thing I worry about is the intermediate quaternions could be wrong, and I don't have any way to test.

Global VectorX#
Global VectorY#
Global VectorZ#
Global VectorW#

Function VectorX#()
Return VectorX#
End Function

Function VectorY#()
Return VectorY#
End Function

Function VectorZ#()
Return VectorZ#
End Function

Function VectorW#()
Return VectorW#
End Function

Function EulerAsQuat(pitch#,yaw#,roll#)
cr#=Cos(-roll#/2.0)
cp#=Cos(pitch#/2.0)
cy#=Cos(yaw#/2.0)
sr#=Sin(-roll#/2.0)
sp#=Sin(pitch#/2.0)
sy#=Sin(yaw#/2.0)
cpcy#=cp#*cy#
spsy#=sp#*sy#
spcy#=sp#*cy#
cpsy#=cp#*sy#
VectorW#=cr#*cpcy#+sr#*spsy#
VectorX#=sr#*cpcy#-cr#*spsy#
VectorY#=cr#*spcy#+sr#*cpsy#
VectorZ#=cr#*cpsy#-sr#*spcy#
End Function

Graphics3D 640,480,16,2

p=CreatePivot()

x#=1
y#=2
z#=3
pitch#=20
yaw#=60
roll#=10
scalex#=2
scaley#=1
scalez#=1

PositionEntity p,x,y,z
RotateEntity p,pitch,yaw,roll
ScaleEntity p,scalex,scaley,scalez

Print GetMatElement(p,0,0)+",     "+GetMatElement(p,0,1)+",     "+GetMatElement(p,0,2)
Print GetMatElement(p,1,0)+",     "+GetMatElement(p,1,1)+",     "+GetMatElement(p,1,2)
Print GetMatElement(p,2,0)+",     "+GetMatElement(p,2,1)+",     "+GetMatElement(p,2,2)
Print GetMatElement(p,3,0)+",     "+GetMatElement(p,3,1)+",     "+GetMatElement(p,3,2)

EulerAsQuat(pitch,yaw,roll)
qx#=VectorX()
qy#=VectorY()
qz#=VectorZ()
qw#=VectorW()

mat00#=(1.0-2.0*qx#*qx#-2.0*qz#*qz#)*scalex
mat10#=-(2.0*qz#*qy#-2.0*qw#*qx#)*scaley
mat20#=-(2.0*qx#*qy#+2.0*qw#*qz#)*scalez
mat30#=x
mat01#=-(2.0*qz#*qy#+2.0*qw#*qx#)*scalex
mat11#=(1.0-2.0*qy#*qy#-2.0*qx#*qx#)*scaley
mat21#=-(2.0*qw#*qy#-2.0*qx#*qz#)*scalez
mat31#=y
mat02#=(2.0*qw#*qz#-2.0*qx#*qy#)*scalex
mat12#=(2.0*qx#*qz#+2.0*qw#*qy#)*scaley
mat22#=(1.0-2.0*qz#*qz#-2.0*qy#*qy#)*scalez
mat32#=z
mat03#=0
mat13#=0
mat23#=0
mat33#=0

Print ""

Print mat00#+",     "+mat01#+",     "+mat02#
Print mat10#+",     "+mat11#+",     "+mat12#
Print mat20#+",     "+mat21#+",     "+mat22#
Print mat30#+",     "+mat31#+",     "+mat32#

WaitKey



puki(Posted 2005) [#2]
"Tom" ("Scouse") was dicking about with this sort of stuff with his BMax 3D engine he was writing. He might know.


JoshK(Posted 2005) [#3]
I'm pretty sure the quats are right. At least, it uses the same code as the one in the archives

When I use the code in "3D Math Primer" I get bad results, so I had to change it up a bit.