Can some help me add better cubes to my 3d engine?

id: 746994

category: Help with Scripts

posts: 10

Coding3dnow Coding3dnow loading
hello i am currently making a 3d engine I have all the hard parts down I just need help adding a cube that you can rotate
https://scratch.mit.edu/projects/972151946/

Coding3dnow wrote:

hello i am currently making a 3d engine I have all the hard parts down I just need help adding a cube that you can rotate
https://scratch.mit.edu/projects/972151946/

Sorry sir, but 3d Engines are fairly hard to make. Searching “3D Engine” may help (look at the code)
Coding3dnow Coding3dnow loading

Create-Scratch101 wrote:

Coding3dnow wrote:

hello i am currently making a 3d engine I have all the hard parts down I just need help adding a cube that you can rotate
https://scratch.mit.edu/projects/972151946/

Sorry sir, but 3d Engines are fairly hard to make. Searching “3D Engine” may help (look at the code)
oh I already have a 3d engine I just need to add stuff in it like cubes but it is already working
To create a cube, you need 6 faces. Each face is made up of either 1 quad or 2 triangles. For the sake of this example I will use quads.
At the very basics, a cube goes like this:
EXAMPLE P1(x, y, z), P2(x, y, z), P3(x, y, z) | P = Triangle Vertice

Tri 1: (0, 0, 0), (0, 1, 0) (1, 0, 0)
Tri 2: (1, 1, 0), (0, 1, 0) (1, 0, 0)
Tri 3: (0, 0, 0), (0, 1, 0), (0, 0, 1)
Tri 4: (0, 1, 1), (0, 1, 0), (0, 0, 1)
Tri 5: (1, 0, 0), (1, 1, 0), (1, 0, 1)
Tri 6: (1, 1, 1), (1, 1, 0), (1, 0, 1)
Tri 7: (0, 0, 1), (0, 1, 1) (1, 0, 1)
Tri 8: (1, 1, 1), (0, 1, 1) (1, 0, 1)
Tri 9: (0, 0, 0), (0, 0, 1) (1, 0, 0)
Tri 10: (1, 0, 1), (0, 0, 1) (1, 0, 0)
Tri 11: (0, 1, 0), (0, 1, 1) (1, 1, 0)
Tri 12: (1, 1, 1), (0, 1, 1) (1, 1, 0)

You can multiply every number by the desired size of the square
Coding3dnow Coding3dnow loading

10SmartGaming wrote:

To create a cube, you need 6 faces. Each face is made up of either 1 quad or 2 triangles. For the sake of this example I will use quads.
At the very basics, a cube goes like this:
EXAMPLE P1(x, y, z), P2(x, y, z), P3(x, y, z) | P = Triangle Vertice

Tri 1: (0, 0, 0), (0, 1, 0) (1, 0, 0)
Tri 2: (1, 1, 0), (0, 1, 0) (1, 0, 0)
Tri 3: (0, 0, 0), (0, 1, 0), (0, 0, 1)
Tri 4: (0, 1, 1), (0, 1, 0), (0, 0, 1)
Tri 5: (1, 0, 0), (1, 1, 0), (1, 0, 1)
Tri 6: (1, 1, 1), (1, 1, 0), (1, 0, 1)
Tri 7: (0, 0, 1), (0, 1, 1) (1, 0, 1)
Tri 8: (1, 1, 1), (0, 1, 1) (1, 0, 1)
Tri 9: (0, 0, 0), (0, 0, 1) (1, 0, 0)
Tri 10: (1, 0, 1), (0, 0, 1) (1, 0, 0)
Tri 11: (0, 1, 0), (0, 1, 1) (1, 1, 0)
Tri 12: (1, 1, 1), (0, 1, 1) (1, 1, 0)

You can multiply every number by the desired size of the square
ok but how do I rotate that cube by the x y and z axis
MineTurte MineTurte loading

Coding3dnow wrote:

10SmartGaming wrote:

To create a cube, you need 6 faces. Each face is made up of either 1 quad or 2 triangles. For the sake of this example I will use quads.
At the very basics, a cube goes like this:
EXAMPLE P1(x, y, z), P2(x, y, z), P3(x, y, z) | P = Triangle Vertice

Tri 1: (0, 0, 0), (0, 1, 0) (1, 0, 0)
Tri 2: (1, 1, 0), (0, 1, 0) (1, 0, 0)
Tri 3: (0, 0, 0), (0, 1, 0), (0, 0, 1)
Tri 4: (0, 1, 1), (0, 1, 0), (0, 0, 1)
Tri 5: (1, 0, 0), (1, 1, 0), (1, 0, 1)
Tri 6: (1, 1, 1), (1, 1, 0), (1, 0, 1)
Tri 7: (0, 0, 1), (0, 1, 1) (1, 0, 1)
Tri 8: (1, 1, 1), (0, 1, 1) (1, 0, 1)
Tri 9: (0, 0, 0), (0, 0, 1) (1, 0, 0)
Tri 10: (1, 0, 1), (0, 0, 1) (1, 0, 0)
Tri 11: (0, 1, 0), (0, 1, 1) (1, 1, 0)
Tri 12: (1, 1, 1), (0, 1, 1) (1, 1, 0)

You can multiply every number by the desired size of the square
ok but how do I rotate that cube by the x y and z axis


You'd just gradually change it's generation x, y, and z I believe (idk much about how 3D projects work lol)

Example:
Stamp 1: Tri 1: (0,1,1), (0,1,0),(0,0,1)
Stamp 2: Tri 1: (0,1,2), (0,1,1),(0,0,2)

Coding3dnow wrote:

10SmartGaming wrote:

To create a cube, you need 6 faces. Each face is made up of either 1 quad or 2 triangles. For the sake of this example I will use quads.
At the very basics, a cube goes like this:
EXAMPLE P1(x, y, z), P2(x, y, z), P3(x, y, z) | P = Triangle Vertice

Tri 1: (0, 0, 0), (0, 1, 0) (1, 0, 0)
Tri 2: (1, 1, 0), (0, 1, 0) (1, 0, 0)
Tri 3: (0, 0, 0), (0, 1, 0), (0, 0, 1)
Tri 4: (0, 1, 1), (0, 1, 0), (0, 0, 1)
Tri 5: (1, 0, 0), (1, 1, 0), (1, 0, 1)
Tri 6: (1, 1, 1), (1, 1, 0), (1, 0, 1)
Tri 7: (0, 0, 1), (0, 1, 1) (1, 0, 1)
Tri 8: (1, 1, 1), (0, 1, 1) (1, 0, 1)
Tri 9: (0, 0, 0), (0, 0, 1) (1, 0, 0)
Tri 10: (1, 0, 1), (0, 0, 1) (1, 0, 0)
Tri 11: (0, 1, 0), (0, 1, 1) (1, 1, 0)
Tri 12: (1, 1, 1), (0, 1, 1) (1, 1, 0)

You can multiply every number by the desired size of the square
ok but how do I rotate that cube by the x y and z axis
I don't know the specifics, but you would definitely use some trig.
Malicondi Malicondi loading

Coding3dnow wrote:

ok but how do I rotate that cube by the x y and z axis
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:

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 trig
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))
in scratch blocks the way to use these formulas will be as so: (i will be using cos and sin variables for this)

for y rotation (left right):
set [x v] to (((x) * (cosY)) - ((z) * (sinY
set [z v] to (((x) * (sinY)) - ((z) * (sinY
for x rotation (up down):
set [y v] to (((y) * (cosX)) - ((z) * (sinX
set [z v] to (((y) * (sinX)) - ((z) * (sinX
for z rotation (turning head to side):
set [x v] to (((x) * (cosZ)) - ((y) * (sinZ
set [y v] to (((x) * (sinZ)) - ((y) * (sinZ
If you want a little more explanation, see this tutorial.
Hope this helps!
Coding3dnow Coding3dnow loading

Malicondi wrote:

Coding3dnow wrote:

ok but how do I rotate that cube by the x y and z axis
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:

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 trig
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))
in scratch blocks the way to use these formulas will be as so: (i will be using cos and sin variables for this)

for y rotation (left right):
set [x v] to (((x) * (cosY)) - ((z) * (sinY
set [z v] to (((x) * (sinY)) - ((z) * (sinY
for x rotation (up down):
set [y v] to (((y) * (cosX)) - ((z) * (sinX
set [z v] to (((y) * (sinX)) - ((z) * (sinX
for z rotation (turning head to side):
set [x v] to (((x) * (cosZ)) - ((y) * (sinZ
set [y v] to (((x) * (sinZ)) - ((y) * (sinZ
If you want a little more explanation, see this tutorial.
Hope this helps!
ya your a little work I already added this it was simpler than I thought but thank you for the help