Hi all, i’m quite a beginner, more in 3d level math than in coding (i’m not so confident in my english too), and i’m trying to build from scratch a working flight simulator, not with amazing graphics, just for now a 3d model of a plane and pitch black background…but the first problem i have to solve, and on which i’m spending weeks, are the controls. i have 3 angles for the plane, and i’m applying the rotation in this order, Z, X, Y, the equivalent to roll, pitch and yaw, i already tryied yaw, pitch and roll, but still nothing…i want that whenever i press one of the 6 control keys (2 for each axis) the angle of that axis changes relative to the already changed others, if i move one and then reset all works, but if i want to change multiple angles at once, the all thing go crazy! I just wanted relative rotations…
Here is my code for the plane object, full with commented failed attempts!
function Plane(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.upX = 0;
this.upY = 0;
this.rot = 0;
this.dim = 50;
this.color = color(255, 255, 255);
this.show = function () {
push();
ambientMaterial(this.color);
translate(this.x, this.y, this.z);
rotateZ(this.rot); //never works if not centered
rotateX(this.upY); //works only after roll-rotateZ-this.rot
rotateY(this.upX); //works
///rotateX(this.upY);
///rotateY(this.upX);
///rotateZ(this.rot);
//rotateY(this.upX);
//rotateX(this.upY);
//rotateZ(this.rot);
ellipsoid(this.dim/2, this.dim/5, this.dim, 10, 10);
translate(0, -20, 40);
ellipsoid(2, 20, 15, 5, 5);
pop();
}
this.move = function () {
this.x += cos(-this.upY)*cos(this.upX);
this.y += sin(-this.upY);
this.z += cos(-this.upY)*sin(this.upX);
}
this.update = function () {
if(keyIsDown(65)) {
this.rot -= QUARTER_PI / 32;
}
if(keyIsDown(68)) {
this.rot += QUARTER_PI / 32;
}
if(keyIsDown(69)) {
this.upX -= (QUARTER_PI / 32);
//this.upY += (QUARTER_PI / 32)*sin(this.rot);
//this.upX -= (QUARTER_PI / 32)*cos(this.upY)*cos(this.rot);
}
if(keyIsDown(81)) {
this.upX += (QUARTER_PI / 32);
//this.upY += (QUARTER_PI / 32)*sin(this.rot);
//this.upX += (QUARTER_PI / 32)*cos(this.upY)*cos(this.rot);
}
if(keyIsDown(83)) {
this.upY -= (QUARTER_PI / 32); //relative somehow to upX!
//this.upY += (QUARTER_PI / 32)*cos(this.upX)*cos(this.rot);
//this.upX -= (QUARTER_PI / 32)*sin(this.rot)*cos(this.upY);
//this.rot += (QUARTER_PI / 32)*sin(this.upX)*sin(this.upY);
}
if(keyIsDown(87)) {
this.upY += (QUARTER_PI / 32)*cos(this.upX); //relative somehow to upX!
this.rot -= (QUARTER_PI / 32)*sin(this.upX);
}
}
if(this.upY <= 0)
this.upY = TWO_PI;
else if(this.upY >= TWO_PI)
this.upY = 0;
if(this.upX <= 0)
this.upX = TWO_PI;
else if(this.upX >= TWO_PI)
this.upX = 0;
if(this.rot <= 0)
this.rot = TWO_PI;
else if(this.rot >= TWO_PI)
this.rot = 0;
}
NB: upX is the angle moving on X and Z axis, used for the Y axis rotation, usually called PHI, upY is THETA, the elevation above the XZ plane, which rotate on X axis, and i think should do so also on the Z one…and in the end rot is the angle moving on the XY plane which cause the Z axis rotation!
If you want to correlate them to yaw, pitch and roll:
upX -> yaw
upY -> pitch
rot -> roll
Help pleas!