I have made a 3D renderer for my Arduino nano 33IoT moving works but when i try to rotate the cube gets smaller and smaller until it becomes a line. Can anyone help me?
The code:
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Instanziierung
int point[3] = {2, 1, 1};
int fov = 20;
double pX = 0;
double pY = 0;
int middleX = SCREEN_WIDTH / 2;
int middleY = SCREEN_HEIGHT / 2;
int cube[8][3] = {
{-15, 1, -15},//0
{15, 1, -15},//1
{-15, 1, 15},//2
{15, 1, 15},//3
{-15, 15, -15},//4
{15, 15, -15},//5
{-15, 15, 15},//6
{15, 15, 15}//7
};
int vertex[12][2] = {
{0,1},
{0,2},
{0,4},
{3,2},
{3,1},
{3,7},
{6,2},
{6,4},
{6,7},
{5,1},
{5,4},
{5,7}
};
double points[8][2] = {
{0,0},//0
{0,0},//1
{0,0},
{0,0},
{0,0},
{0,0},
{0,0},
{0,0}
};
void setup() {
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
Serial.begin(9600);
render();
display.display();
delay(1000);
for (int i = 0; i < 10; i++) {
rotateY(0.1);
delay(10);
render();
display.display();
}
}
void loop() {
}
void render() {
display.clearDisplay();
for (int i = 0; i < 8; i++) {
double sum1 = cube[i][0] * fov;
double sum2 = cube[i][1] + fov;
pX = sum1 / sum2 + middleX;
sum1 = cube[i][2] * fov;
sum2 = cube[i][1] + fov;
pY = sum1 / sum2 + middleY;
Serial.print(pX);
Serial.println(pY);
points[i][0] = pX;
points[i][1] = pY;
//display.drawPixel(pX, pY, WHITE);
}
Serial.println(vertex[0][0]);
Serial.println(vertex[0][1]);
Serial.println(points[0][0]);
Serial.println(points[0][1]);
for (int i = 0; i < 12; i++) {
display.drawLine(points[vertex[i][0]][0], points[vertex[i][0]][1], points[vertex[i][1]][0], points[vertex[i][1]][1], WHITE);
}
}
void move(int amount) {
for (int i = 0; i < 8; i++) {
cube[i][2] = cube[i][2] += amount;
}
render();
display.display();
delay(10);
}
void rotateX(double angle) {
double cosTheta = cos(angle);
double sinTheta = sin(angle);
for (int i = 0; i < 8; i++) {
double y = cube[i][1];
double z = cube[i][2];
cube[i][1] = y * cosTheta - z * sinTheta;
cube[i][2] = y * sinTheta + z * cosTheta;
}
}
void rotateY(double angle) {
double cosTheta = cos(angle);
double sinTheta = sin(angle);
for (int i = 0; i < 8; i++) {
double x = cube[i][0];
double z = cube[i][2];
cube[i][0] = x * cosTheta + z * sinTheta;
cube[i][2] = -x * sinTheta + z * cosTheta;
}
}