We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, my question is very close of the previous post about camera position, but the result is not what I think about the result wish. I make a code to move the eye camera around the the scene, but I have a problem with size of the scene. Because the position between the eye and the scene is the same, but the sphere change size...is weird !
color rouge, noir, blanc ;
void setup()
{
size( 800,800, P3D) ;
colorMode (HSB, 360,100,100) ;
blanc = color(0,0,100);
rouge = color(10,100,100);
noir = color(10,100,0);
background(noir) ;
int numPeople = (int)random(10,40) ;
amiSetup(numPeople) ;
//initialization camera
cameraP3Dsetup() ;
}
void draw() {
background(noir) ;
PVector mousePos = new PVector (mouseX, mouseY, wheel) ;
PVector absolutePos = new PVector ( mousePos.x -width/2, mousePos.y -height/2, mousePos.y) ;
//zoom
//wheel = 0 ;
//eye camera position
PVector posEye = new PVector (eye(mousePos, radiusCamera).x, eye(mousePos, radiusCamera).y, eye(mousePos, radiusCamera).z ) ;
// the position of the scene is by default (width /2, height /2, 0 ) ;
// so if you wan move to the left, you must minus x, and go to the right add sommesthing to the "x",
// same idea for the top and the bottom, and the depth
PVector scene = new PVector (0,0,0) ;
// the direction of the cam is between (-1 to 1 )
PVector dirCam = new PVector (0.0, 1.0, 0.0) ;
println(posEye) ;
cameraP3Ddraw(posEye, scene, dirCam) ;
println("distance between the scene and the eye camera " + posEye.dist(scene)) ;
amiDraw() ;
//sphere
stroke(rouge) ;
fill(rouge) ;
pushMatrix() ;
translate(width/2, height / 2, 0);
sphere(150) ;
popMatrix() ;
}
float radiusCamera ;
PVector centerScene;
// float altCamFactor ;
void cameraP3Dsetup()
{
// give the start distance between the camera and the scene
if ( width > height ) radiusCamera = width *3 ; else radiusCamera = height *3 ;
//give the starting position of the scene in the display
centerScene = new PVector (width/2.0, height/2.0, 0) ;
//camera direction
//eye setup solution 1
eyeSetup() ;
}
void cameraP3Ddraw(PVector eye, PVector posScene, PVector dir ) {
camera(eye.x, eye.y, eye.z,
centerScene.x +posScene.x , centerScene.y +posScene.y, centerScene.z +posScene.z,
dir.x, dir.y, dir.z);
}
// mouse wheel event
float wheel ;
float checkTheWheel ;
int countWheelAction ;
void mouseWheel(MouseEvent event) {
countWheelAction += 1 ;
checkTheWheel = wheel ;
wheel = event.getCount();
}
//ALGORITHM
//Step 0 :
//global
PVector startPos, posV, posH, posBisector ;
void eyeSetup() {
posV = new PVector (0,0,0) ;
posH = new PVector (0,0,0) ;
posBisector = new PVector (0,0,0) ;
startPos = new PVector(width/2, height/2, 0 ) ;
}
PVector eye(PVector pos, float sizeField)
{
PVector posFinal = new PVector(0,0,0) ;
//STEP 1
//sphere witness vertical
posV.x = 0 ;
posV.y = AxisRotationPos(pos.y, height, sizeField *.5).y ;
posV.z = AxisRotationPos(pos.y, height, sizeField *.5).z ;
//sphere witness horizontal;
posH.x = AxisRotationPos(pos.x, width, sizeField *.5).x ;
posH.y = 0 ;
posH.z = AxisRotationPos(pos.x, width, sizeField *.5).z ;
//STEP 2
//bisector witness sphere
posBisector.x = bisector(posH,posV).x ;
posBisector.y = bisector(posH,posV).y ;
posBisector.z = bisector(posH,posV).z ;
//STEP 3
//point final bisector projection
posFinal = bisectorProjection(bisector(posH,posV), sizeField *.5 ) ;
return posFinal ;
}
//Step 1 : translate the mouse position x and y on the sphere, we must do that separately
float rotationPlan ;
PVector AxisRotationPos(float posMouse, int dist, float distanceToFocus)
{
PVector pos = new PVector (0,0,0) ;
rotationPlan = map(posMouse, 0, dist, 0, TAU) ;
PVector marge = new PVector((width -distanceToFocus *2.0) *.5,(height -distanceToFocus *2.0) *.5) ;
float dataPosXY = cos(rotationPlan) *distanceToFocus ;
float dataPosZ = sin(rotationPlan) *distanceToFocus ;
pos = new PVector ( dataPosXY, dataPosXY, dataPosZ ) ;
return pos ;
}
//STEP 2 : caculate the bisetor of the horizontal and verticale sphere
PVector bisector(PVector p1, PVector p2)
{
PVector b ;
b = PVector.add(p1, p2);
b.div(2) ;
return b ;
}
//STEP 3 : calculte the position of bisector point projection on the sphere, we supose the center of the sphere is 0,0,0
PVector bisectorProjection(PVector bisector, float radius )
{
PVector bisectorProjection = new PVector (0,0) ;
PVector center = new PVector (0,0) ;
float distanceBetweenCenterAndBisector = bisector.dist(center) ;
float rapport = radius /distanceBetweenCenterAndBisector ;
bisector.mult(rapport) ;
bisectorProjection = bisector ;
return bisectorProjection ;
}
IntList IDpeople ;
ArrayList<Ami> listPeople ;
void amiSetup(int num) {
listPeople = new ArrayList<Ami>() ;
//ID for each people
IDpeople = new IntList() ;
for ( int i = 0 ; i < num ; i++ ) {
int ID = i ;
//position of people
PVector p = new PVector ( random(10, width -10), random(10, height -10), random(-height/2, height/2)) ;
//friend of this people
for ( int f = 0 ; f < num ; f++) IDpeople.append(f) ;
//int numFriend = (int)random(num -1) ;
int numFriend = (int)random(num / 2 ) ;
int [] IDfriend = new int [numFriend] ;
for ( int j = 0 ; j < numFriend ; j++) {
int whichPeople = (int)random(IDpeople.size() ) ;
int IDofFriend = IDpeople.get(whichPeople) ;
IDfriend[j] = IDofFriend ;
if(IDpeople.size() > 0 ) IDpeople.remove(whichPeople) ;
}
//add information to the arraylist
Ami people = new Ami(p, ID, IDfriend ) ;
listPeople.add(people) ;
//clear the list for a new start friend round
IDpeople.clear() ;
}
}
void amiDraw()
{
for(int i = 0 ; i < listPeople.size() ; i++) {
// int from = (int)random(listPeople.size() ) ;
Ami peopleOrigin = listPeople.get(i) ;
if (peopleOrigin.friendList.length > 0 ) {
PVector origin = peopleOrigin.pos ;
for ( int f = 0 ; f < peopleOrigin.friendList.length ; f++) {
Ami peopleDestination = listPeople.get( peopleOrigin.friendList[f]) ;
PVector destination = peopleDestination.pos ;
//display
strokeWeight(1) ;
stroke(255) ;
line(origin.x, origin.y, origin.z, destination.x, destination.y, destination.z) ;
}
}
}
}
class Ami
{
PVector pos = new PVector(0,0,0) ;
PVector size ;
int ID ;
int [] friendList ;
Ami ( PVector pos, int ID, int [] IDfriend ) {
this.pos = pos ;
this.ID = ID ;
friendList = new int [IDfriend.length] ;
for ( int i = 0 ; i < IDfriend.length ; i ++ ) {
friendList[i] = IDfriend[i] ;
}
}
Ami ( PVector pos) {
this.pos = pos ;
}
}
Answers
where is amisetup()?
Sorry I'de forgot a part of code ! Now work fine excepte the strange thing zoom to the corners of the scene.