Mar 27, 2024 17:13:01
Can some help me add better cubes to my 3d engine?
view on scratch
view on legacy ocular
Malicondi
loading
Because this was brought back up, I felt that I should answer this and it's quite simple, it just uses a little trigonometry. The formula to rotate around the y axis(left right) is this: ok but how do I rotate that cube by the x y and z axis
x' = (x * cos of (y dir)) - (z * sin of (y dir))
z' = (x * sin of (y dir)) + (z * cos of (y dir))
for the x axis (up down) it's this:
y' = (y * cos of (x dir)) - (z * sin of (x dir))
z' = (y * sin of (x dir)) + (z * cos of (x dir))
and for the z axis (imagine turning your head to the side) it's this:
x' = (x * cos of (z dir)) - (y * sin of (z dir))
y' = (x * sin of (z dir)) + (y * cos of (z dir))
For these, its best to use a custom block to store the original x, y, and z values or this won't work, as x', y', and z' all stand for the changed variables, but the formulas depend on the original values.
For 3d projects, since sin and cos are computationally expensive, using sin and cos in all of these formulas a total of 12 times for a single object is very slow. So i recommend first getting the cos and sin values at the start of every tick before you any formulas to speed this up greatly, like so: (directions will also need variables)
define calc trigin scratch blocks the way to use these formulas will be as so: (i will be using cos and sin variables for this)
set [cosX v] to ([cos v] of (x dir))
set [sinX v] to ([sin v] of (x dir))
set [cosY v] to ([cos v] of (y dir))
set [sinY v] to ([sin v] of (y dir))
set [cosZ v] to ([cos v] of (z dir))
set [sinZ v] to ([sin v] of (z dir))
for y rotation (left right):
set [x v] to (((x) * (cosY)) - ((z) * (sinYfor x rotation (up down):
set [z v] to (((x) * (sinY)) - ((z) * (sinY
set [y v] to (((y) * (cosX)) - ((z) * (sinXfor z rotation (turning head to side):
set [z v] to (((y) * (sinX)) - ((z) * (sinX
set [x v] to (((x) * (cosZ)) - ((y) * (sinZIf you want a little more explanation, see this tutorial.
set [y v] to (((x) * (sinZ)) - ((y) * (sinZ
Hope this helps!