in the program below, i capture xx,yy,zz into the quat. i multiply dice.rotation by the quat.
in rotation land a quat is a rotation. to add two rotations, you multiply by the quat you want to add. so the die rotates. newrot = oldrot*addedrot
uncomment one or the other quat multiply line to switch from local to global rotations.
if you put the new quaternion on the right (old*new) the rotation is local. if you put it the other way, the rotation is global.
by local, i mean the due will always rotate around the same die-relative axis, like the 1-6 axis or the 2-5. by global, i mean it’ll always be screen relative, y always screen vertical, and so on.
with your scheme, if it got to a place where it wouldn’t turn, that’s called gimbal lock, and it’s a thing that happens when you work with x y z angles instead of rotations.
viewer.mode=STANDARD
function setup()
print("x axis = sides 1,6")
print("y axis = sides 3,4")
print("z axis = sides 2,5")
parameter.integer("xx",-1,1,0)
parameter.integer("yy",-1,1,0)
parameter.integer("zz",-1,1,0)
rectMode(CORNER)
scene = craft.scene()
assert(OrbitViewer, "Please include Cameras (not Camera) as a dependency")
viewer = scene.camera:add(OrbitViewer, vec3(0), 10, 0, 2000)
--rx,ry,rz=0,0,0
createImg()
createDice()
pos=1
end
function update(dt)
scene:update(dt)
end
function draw()
background(0)
update(DeltaTime)
scene:draw()
dice.rotation=dice.rotation*quat.eulerAngles(xx,yy,zz) -- local
--dice.rotation = quat.eulerAngles(xx,yy,zz)*dice.rotation -- global
--rx=rx+xx
--ry=ry+yy
--rz=rz+zz
fill(255)
--text(string.format("x =%3d y =%3d z =%3d",rx%360,ry%360,rz%360),WIDTH/2,HEIGHT-50)
end
function createImg()
noSmooth()
img=image(600,100)
setContext(img)
background(0, 0, 0, 255)
fill(255,255,255)
rect(0,0,600,100)
fill(0,0,0)
ellipse(50,50,20)
ellipse(125,75,20) ellipse(175,25,20)
ellipse(525,75,20) ellipse(550,50,20) ellipse(575,25,20)
ellipse(425,75,20) ellipse(425,25,20) ellipse(475,75,20) ellipse(475,25,20)
ellipse(350,50,20) ellipse(325,75,20) ellipse(325,25,20)
ellipse(375,75,20) ellipse(375,25,20)
ellipse(225,75,20) ellipse(250,75,20) ellipse(275,75,20)
ellipse(225,25,20) ellipse(250,25,20) ellipse(275,25,20)
setContext()
end
function createDice(px,py,pz)
dice=scene:entity()
dice.position=vec3(px,py,pz)
dice.model = craft.model.cube(vec3(1,1,1))
dice.material = craft.material(asset.builtin.Materials.Standard)
dice.material.map = img
local temp=dice.model.indices
for z=#dice.model.indices,1,-1 do
table.insert(temp,dice.model.indices[z])
end
dice.model.indices=temp
local uvs1={}
c=0
for x=1,6 do
table.insert(uvs1,vec2(c/6,0))
table.insert(uvs1,vec2((c+1)/6,0))
table.insert(uvs1,vec2((c+1)/6,1))
table.insert(uvs1,vec2(c/6,1))
c=c+1
end
dice.model.uvs=uvs1
end