- Joined
- May 20, 2023
- Messages
- 465
I coded 95 percent of @torus and this is how I coded it -
Trigger the code by the player message event block and you can decide the command by yourself. For now I name the command "@torus" Now let's code the parameters of the torus.
At first, you should set variable for the parameters of torus. Create a variable named `%player%-radius and for the radius, it completely depends on how big you want the torque to be. For now, let's set the radius to 30. Now create a variable named `%player%radius-2` and set it to the desired radius of the tube. Next, You want 2 variables for `theta` and `phi` and set them both to as they will control the angles used to generate torus.
Now, time for the main part. Start with a loop that runs as long as `theta` is less than or equal to 2 times pi (2π) or simply, a full circle This loop will iterate over the theta angle. Inside this loop, start another while loop that runs as long as `phi` is less than or equal to 2 times pi or 2π. This loop will iterate over the phi angle.
Within the loop, calculate the X, Y, and Z coordinates of each torus point using the provided trigonometric formulas. you can use Taylor series to approximate the trigonometric functions needed to calculate the coordinates of the torus shape, it goes like this -
If message equation - @torus command handler
function handleTorusCommand(player) {
For the parameters,
var Radius = 5; // Radius of the torus
var Radius = 2; // Radius of the torus
To Generate the torus shape -
var thetaStep = 0. Step size for theta, var = 0.1; // Step size for phi
The theta is converted and is put on the loop.
Set variable sin(x). For Sin - x**3/6 + x**5/120 - x**7/5040 + x**9/362880;}
function cos(x)
for cosine -
Return 1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320;
for (var theta = 0; theta <= 2 * Math.PI; theta += thetaStep)
for (var phi = 0; phi <= 2 * Math.PI; phi += phiStep) {
var x = (R + r * cos(theta)) * cos(phi);
var y = (R + r * cos(theta)) * sin(phi);
var z = r * sin(theta);
Make sure to convert x into radian by multiplying it by π/180 and to get rid of negative values, you can add negative x with x+x
Spawn a block at each torus point
Game action ("SetBlock", x, y, z, "any block you want")
Delay between block placements for animation effect
GAME action wait (0.1 ticks); Adjust as needed
When loop stops........ -
player.action("call function") "torus_generation_complete");
} Use the "SetBlock" block to spawn a block at each torus point with the calculated coordinates x y z and inside the chest, you can put any blocks you want. This will create the torus shape block by block. Now, add a game action wait block to adjust the delay time as needed to achieve the desired animation speed. For now I'll set it to 5 ticks. You better code the que system in order to get rid of bugs which you might face from the code.
For the loop to work properly, ensure that after the inner phi loop completes, you increment the `theta` variable by 0.1. This will move the loop to the next theta angle iteration and set the `phi` variable back to 0 after each theta iteration to start iterating over the phi angle from the beginning again.
After the loops finishes.... Stop the loop and add a "call function" block which will trigger the function which I'm going to explain.
Put a function block, then if variable equals i.e. the variable you used for the angles of the torus then wait and call recursive function and then stop it until the loop is finished
Trigger the code by the player message event block and you can decide the command by yourself. For now I name the command "@torus" Now let's code the parameters of the torus.
At first, you should set variable for the parameters of torus. Create a variable named `%player%-radius and for the radius, it completely depends on how big you want the torque to be. For now, let's set the radius to 30. Now create a variable named `%player%radius-2` and set it to the desired radius of the tube. Next, You want 2 variables for `theta` and `phi` and set them both to as they will control the angles used to generate torus.
Now, time for the main part. Start with a loop that runs as long as `theta` is less than or equal to 2 times pi (2π) or simply, a full circle This loop will iterate over the theta angle. Inside this loop, start another while loop that runs as long as `phi` is less than or equal to 2 times pi or 2π. This loop will iterate over the phi angle.
Within the loop, calculate the X, Y, and Z coordinates of each torus point using the provided trigonometric formulas. you can use Taylor series to approximate the trigonometric functions needed to calculate the coordinates of the torus shape, it goes like this -
If message equation - @torus command handler
function handleTorusCommand(player) {
For the parameters,
var Radius = 5; // Radius of the torus
var Radius = 2; // Radius of the torus
To Generate the torus shape -
var thetaStep = 0. Step size for theta, var = 0.1; // Step size for phi
The theta is converted and is put on the loop.
Set variable sin(x). For Sin - x**3/6 + x**5/120 - x**7/5040 + x**9/362880;}
function cos(x)
for cosine -
Return 1 - x**2/2 + x**4/24 - x**6/720 + x**8/40320;
for (var theta = 0; theta <= 2 * Math.PI; theta += thetaStep)
for (var phi = 0; phi <= 2 * Math.PI; phi += phiStep) {
var x = (R + r * cos(theta)) * cos(phi);
var y = (R + r * cos(theta)) * sin(phi);
var z = r * sin(theta);
Make sure to convert x into radian by multiplying it by π/180 and to get rid of negative values, you can add negative x with x+x
Spawn a block at each torus point
Game action ("SetBlock", x, y, z, "any block you want")
Delay between block placements for animation effect
GAME action wait (0.1 ticks); Adjust as needed
When loop stops........ -
player.action("call function") "torus_generation_complete");
} Use the "SetBlock" block to spawn a block at each torus point with the calculated coordinates x y z and inside the chest, you can put any blocks you want. This will create the torus shape block by block. Now, add a game action wait block to adjust the delay time as needed to achieve the desired animation speed. For now I'll set it to 5 ticks. You better code the que system in order to get rid of bugs which you might face from the code.
For the loop to work properly, ensure that after the inner phi loop completes, you increment the `theta` variable by 0.1. This will move the loop to the next theta angle iteration and set the `phi` variable back to 0 after each theta iteration to start iterating over the phi angle from the beginning again.
After the loops finishes.... Stop the loop and add a "call function" block which will trigger the function which I'm going to explain.
Put a function block, then if variable equals i.e. the variable you used for the angles of the torus then wait and call recursive function and then stop it until the loop is finished
Last edited: