Hi!
I’m creating simple solar system where you can moving around using spaceship. I have problem with camera. When i’m going right or left everythink is fine, but when move my mouse to far on top or down there is a camera jump. For example when planet is on left upper corner after this ‘jump’ will be on right down corner. Do you know how to fix it?
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.InputEvent;
PShape mars;
PShape sun;
PShape venus;
PShape earth;
PShape moon1;
PShape ship;
PImage mars_txt;
PImage sun_txt;
PImage ven_txt;
PImage earth_txt;
PImage moon1_txt;
PImage bg_txt;
float theta;
float rotX;
float rotY;
float fov = PI / 3.0; // Początkowa wartość pola widzenia
PVector shipPosition = new PVector(0, 0, 1500); // Pozycja statku
PVector shipDirection = new PVector(0, 0, -1); // Kierunek lotu statku
float shipSpeed = 5; // Prędkość statku
boolean movingForward = false;
boolean movingBackward = false;
Robot robot;
void setup() {
size(1920, 1080, P3D);
noCursor(); // Ukryj kursor
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
bg_txt = loadImage("background.jpeg");
mars_txt = loadImage("Diffuse_2K.png");
sun_txt = loadImage("Bump_2K.png");
ven_txt = loadImage("venus.png");
earth_txt = loadImage("earth.png");
moon1_txt = loadImage("moon.png");
mars = loadShape("Mars_2K.obj");
sun = loadShape("Mars_2K.obj");
venus = loadShape("Mars_2K.obj");
earth = loadShape("Mars_2K.obj");
moon1 = loadShape("Mars_2K.obj");
ship = loadShape("Spaceship.obj");
mars.setTexture(mars_txt);
sun.setTexture(sun_txt);
venus.setTexture(ven_txt);
earth.setTexture(earth_txt);
moon1.setTexture(moon1_txt);
}
void draw() {
background(0);
float cameraZ = (height / 2.0) / tan(fov / 2.0);
perspective(fov, float(width) / float(height), cameraZ / 10.0, cameraZ * 10.0);
// Aktualizacja pozycji statku
if (movingForward) {
shipPosition.add(PVector.mult(shipDirection, shipSpeed));
}
if (movingBackward) {
shipPosition.sub(PVector.mult(shipDirection, shipSpeed));
}
// Ustawienie kamery za statkiem
PVector cameraPosition = PVector.add(shipPosition, PVector.mult(shipDirection, -200)); // Kamera za statkiem
camera(cameraPosition.x, cameraPosition.y, cameraPosition.z,
shipPosition.x, shipPosition.y, shipPosition.z,
0, 1, 0);
print("\n" + shipPosition);
// Transformacje świata
pushMatrix();
translate(shipPosition.x, shipPosition.y, shipPosition.z);
rotateY(rotX);
rotateX(-rotY);
// Renderowanie statku kosmicznego
pushMatrix(); // SPACESHIP
rotateY(rotX);
rotateX(rotY);
scale(0.1);
shape(ship);
popMatrix();
popMatrix();
// Renderowanie innych obiektów
pushMatrix(); // SUN
scale(30);
shape(sun);
popMatrix();
pushMatrix(); // MARS
rotate(theta / 4);
translate(400, 0);
rotateY(theta / 2);
scale(6);
shape(venus);
popMatrix();
pushMatrix(); // VENUS
rotate(theta / 6);
translate(600, 0);
rotateY(theta / 1.3);
scale(10);
shape(earth);
popMatrix();
pushMatrix(); // EARTH
rotate(theta / 5);
translate(800, 0);
rotateY(theta / 1.3);
scale(10);
shape(mars);
pushMatrix(); // MOON1
rotate(theta / 1.4);
translate(8, 0, 0);
rotateY(theta / 1.3);
scale(0.3);
shape(moon1);
popMatrix();
pushMatrix(); // MOON1
rotate(theta * 1.5);
translate(0, -11, 0);
rotateY(theta / 1.3);
scale(0.3);
shape(moon1);
popMatrix();
pushMatrix(); // MOON1
rotate(theta);
translate(0, 0, -13);
rotateY(theta / 1.3);
scale(0.3);
shape(moon1);
popMatrix();
popMatrix();
theta += 0.03;
if (mouseX != width / 2 || mouseY != height / 2) {
float deltaX = mouseX - width / 2;
float deltaY = mouseY - height / 2;
rotX -= radians(deltaX) * 0.01; // Odwrócenie ruchu lewo-prawo
rotY -= radians(deltaY) * 0.01; // Poprawiono ruch góra-dół
// Aktualizacja kierunku statku na podstawie obrotów
shipDirection = new PVector(cos(rotY) * sin(rotX), sin(-rotY), cos(rotY) * cos(rotX)); // Poprawiono kierunek lotu
shipDirection.normalize();
// Reset pozycji kursora
robot.mouseMove(width / 2, height / 2);
}
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
fov -= e * -0.05; // Regulacja prędkości przybliżania i oddalania
fov = constrain(fov, 0.1, PI); // Ograniczenie pola widzenia
}
void keyPressed() {
if (key == 'W' || key == 'w') {
movingForward = true;
} else if (key == 'S' || key == 's') {
movingBackward = true;
}
}
void keyReleased() {
if (key == 'W' || key == 'w') {
movingForward = false;
} else if (key == 'S' || key == 's') {
movingBackward = false;
}
}
Preformatted text