How can you create your own 3D camera navigations ?
in
Programming Questions
•
2 years ago
Currently I'm working on a 3D project. This project consists of having sphere attracted to boxes in the x,y and z dimensions. Till now I've managed to create the spheres and boxes and the relevant force. Now I need to create the 3D dimension which must be accessed with the mouse or keyboard. e.g if the up arrow button is pressed you can zoom in or rotate left.
I know that Peasy cam can do the trick quite easily but for this project I'm not allowed to used Peasy cam. I must create my own camera fuctions. I'm supplying the code I'm working on so you can get an idea of what I'm after.
Thanks all
I know that Peasy cam can do the trick quite easily but for this project I'm not allowed to used Peasy cam. I must create my own camera fuctions. I'm supplying the code I'm working on so you can get an idea of what I'm after.
Thanks all
-
import processing.opengl.*;
int width,height;
float xCube, yCube, zCube; // x,y and z coordiante of Cube
float xSph, ySph, zSph; // x,y and z coordinate of sphere
float dX,dY,dZ; // Variables for sphere velocity
Cube c;
Sphere s;
void setup()
{
width=height=800;
size(width,height,OPENGL);
c = new Cube();
s = new Sphere();
xCube=yCube=zCube=0; // Initial variables for cube
ySph=zSph=0; // Initial y and z coordinates for sphere
xSph =40; // Initial x coordinates for sphere
dY=4; // Inital velocity of sphere
}
void draw()
{
background(150);
translate(width/2,height/2); //Translate coordinate to centre of sketch
c.drawCube(xCube,yCube,zCube); // Draw Cube
s.drawSphere(xSph,ySph,zSph); // Draw Sphere
force();
movement();
}
//CALCULATE FOREC OF ATTRACTION
void force()
{
dX+=(xCube-xSph)/100.0;
dY+=(yCube-ySph)/100.0;
dZ+=(zCube-zSph)/100.0;
}
//CALCULATE MOVEMENT OF SPHERE
void movement()
{
xSph+=dX;
ySph+=dY;
zSph+=dZ;
} - class Cube
{
private float x;
private float y;
private float z;
//CONSTRUCTOR
public Cube()
{
this.x =x;
this.y =y;
this.z =y;
}
//DRAW CUBE
public void drawCube(float x, float y, float z)
{
pushMatrix();
translate(x,y,z);
fill(255,0,0);
box(15);
popMatrix();
}
} - class Sphere
{
private float x;
private float y;
private float z;
//CONSTRUCTOR
public Sphere()
{
this.x =x;
this.y =y;
this.z =z;
}
//DRAW SPHERES
public void drawSphere(float x, float y, float z)
{
pushMatrix();
translate(x,y,z);
fill(0,255,0);
sphere(15);
popMatrix();
}
}
1