Camera movement
in
Programming Questions
•
1 year ago
Hi processing Gurus,
I have a universitiy assignment, with the following things to achieve:
- draw circles and lines into texture
- split the texture into 10*10 parts
- translate and move them
- move the camera
I managed to draw into texture, split them into different parts but I could not make the camera move. If I assing any other value than the standard camera, the whole bunch of quads dissapear. I checked many examples, none of them worked...
Any help is greatly appreciated, and please note that this is my first processing project, though I have some experience in java.
Here is the code:
final int windowSize = 600;
final int boardSize = 200;
int boardStart = (windowSize/2) - (boardSize/2);
int boardEnd = (windowSize/2) + (boardSize/2);
int ellipseWidth = 20;
int ellipseHeight = 20;
Ellipse tempEllipse;
Line tempLine;
int startX;
int startY;
boolean startedEllipse = false;
boolean startedLine = false;
boolean justDraw = true;
CircleButton btn1;
LineButton btn2;
color currentcolor;
color pressedColor;
color baseColor;
float meret = 10;
float moveX = 0;
float moveY = 0;
float Xrotate = 0;
PImage img = createImage(boardSize, boardSize, RGB);
void setup()
{
size(windowSize, windowSize, P3D);
baseColor = color(102);
pressedColor = color(0, 15, 170);
currentcolor = baseColor;
textureMode( NORMALIZED );
color buttoncolor = color(102);
color highlight = color(51);
btn1 = new CircleButton(0, 0, 20, buttoncolor, highlight);
btn2 = new LineButton(22, 0, 20, buttoncolor, highlight);
}
void upix()
{
img.loadPixels();
for( int i=0; i<img.width*img.height-1; ++i)
{
int x = i % img.width;
int y = i / img.width;
if(btn1.isActive()){
if (tempEllipse.isInside(x,y)) {
img.pixels[i] = color(255);
}
} else if(btn2.isActive()){
float x0 = tempLine.getX0();
float x1 = tempLine.getX1();
float y0 = tempLine.getY0();
float y1 = tempLine.getY1();
float dx = abs(x1-x0);
float dy = abs(y1-y0);
float sx = 0;
float sy = 0;
float err = 0;
if (x0 < x1) {sx = 1;} else {sx = -1;}
if (y0 < y1) {sy = 1;} else {sy = -1;}
err = dx-dy;
while (x0!=x1 && y0!=y1){
img.pixels[(int)x0 + (int)y0 * img.width] = color(100,255,100);
float e2 = 2*err;
if (e2> -dy){
err = err -dy;
x0= x0 + sx;
}
if (e2 < dx) {
err = err + dx;
y0 = y0 + sy;
}
}
}
}
img.updatePixels();
}
void draw(){
fill(70);
rect(0,0,600,600);
if(justDraw){// this is the drawing part
btn1.update();
btn2.update();
btn1.display();
btn2.display();
beginShape(QUADS);
texture(img);
noStroke();
vertex(boardStart, boardEnd, 0, 0, 1);
vertex(boardStart, boardStart, 0, 0, 0);
vertex(boardEnd, boardStart, 0, 1, 0);
vertex(boardEnd, boardEnd, 0, 1, 1);
endShape();
if(startedEllipse){
ellipse(startX, startY, (mouseX-startX)*2, (mouseX-startX)*2);
}
if(startedLine){
stroke(155, 70, 25);
line(startX, startY, mouseX, mouseY);
}
} else {// this is where I want the movement and the camera to take place
camera(width/2+moveX, height/2, (height/2) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 0, 0, 1, 0);
float meret =10;
for (int i=0; i<meret; i++){
for (int j=0; j<meret; j++){
pushMatrix();
beginShape(QUADS);
texture(img);
vertex(boardStart+i*boardSize/meret, boardStart+(j+1)*boardSize/meret, 0, i*1/meret, (j+1)*1/meret);
vertex(boardStart+i*boardSize/meret, boardStart+j*boardSize/meret, 0, i*1/meret, j*1/meret);
vertex(boardStart+(i+1)*boardSize/meret, boardStart+j*boardSize/meret, 0, (i+1)*1/meret, j*1/meret);
vertex(boardStart+(i+1)*boardSize/meret, boardStart+(j+1)*boardSize/meret, 0, (i+1)*1/meret, (j+1)*1/meret);
endShape();
popMatrix();
}
}
}
}
void keyPressed()
{
if (key == 'o'){
moveX = 0;
justDraw = false;
}
if (key == 'a') moveX--;
if (key == 'd') moveX++;
}
boolean onBorad(){
if(mouseX>=boardStart && mouseX<=boardEnd && mouseY>=boardStart && mouseY<=boardEnd){
return true;
} else {return false;}
}
void mouseReleased() {
if(btn1.isOver()) {
if(btn1.isActive())btn1.setActive(false);
else{
btn1.setActive(true);
btn2.setActive(false);
}
} else if(btn2.isOver()) {
if(btn2.isActive())btn2.setActive(false);
else{
btn2.setActive(true);
btn1.setActive(false);
}
} else if(onBorad()){
if(btn1.isActive()){
if(!startedEllipse){
startX = mouseX;
startY = mouseY;
startedEllipse = true;
}else{
if(mouseButton == LEFT){
tempEllipse = new Ellipse(startX-boardStart , startY-boardStart, (mouseX-startX)*2, (mouseX-startX)*2);
startedEllipse = false;
upix();
}else{
startedEllipse = false;
}
}
}
if(btn2.isActive()){
if(!startedLine){
startX = mouseX;
startY = mouseY;
startedLine = true;
}else{
if(mouseButton == LEFT){
tempLine = new Line(startX-boardStart, startY-boardStart, mouseX-boardStart, mouseY-boardStart);
startedLine = false;
upix();
} else {
startedLine = false;
}
}
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean active = false;
boolean isOver(){if(mouseX>=x && mouseX<=x+size && mouseY>=y && mouseY<=y+size) return true; else return false;}
void setActive(boolean pressed){active = pressed;}
boolean isActive(){return active;}
void update()
{
if(active) currentcolor = pressedColor;
else if(isOver()) currentcolor = highlightcolor;
else currentcolor = basecolor;
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
ellipse(size/2+x, size/2+x, size-4,size-4);
}
}
class LineButton extends Button
{
LineButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
line(x+2, y+2, x+size-2, y+size-2);
}
}
class Ellipse{
int x;
int y;
float a;
float b;
public Ellipse(int x, int y, int width, int height) {
this.x = x;
this.y = y;
a = width/2.0;
b = height/2.0;
}
float getX(){
return this.x;
}
float getY(){
return this.y;
}
boolean isInside(int x, int y) {
float x1 = (x-this.x)/a;
float y1 = (y-this.y)/b;
return x1*x1 + y1*y1 < 1;
}
}
class Line{
float x1;
float y1;
float x2;
float y2;
float a;
float b;
public Line(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
void setX1(float a){
this.x1=a;
}
void setX2(float a) {
this.x2=a;
}
void setY1(float a){
this.y1=a;
}
void setY2(float a){
this.y2=a;
}
float getX0(){
return this.x1;
}
float getX1(){
return this.x2;
}
float getY0(){
return this.y1;
}
float getY1(){
return this.y2;
}
}
I have a universitiy assignment, with the following things to achieve:
- draw circles and lines into texture
- split the texture into 10*10 parts
- translate and move them
- move the camera
I managed to draw into texture, split them into different parts but I could not make the camera move. If I assing any other value than the standard camera, the whole bunch of quads dissapear. I checked many examples, none of them worked...
Any help is greatly appreciated, and please note that this is my first processing project, though I have some experience in java.
Here is the code:
final int windowSize = 600;
final int boardSize = 200;
int boardStart = (windowSize/2) - (boardSize/2);
int boardEnd = (windowSize/2) + (boardSize/2);
int ellipseWidth = 20;
int ellipseHeight = 20;
Ellipse tempEllipse;
Line tempLine;
int startX;
int startY;
boolean startedEllipse = false;
boolean startedLine = false;
boolean justDraw = true;
CircleButton btn1;
LineButton btn2;
color currentcolor;
color pressedColor;
color baseColor;
float meret = 10;
float moveX = 0;
float moveY = 0;
float Xrotate = 0;
PImage img = createImage(boardSize, boardSize, RGB);
void setup()
{
size(windowSize, windowSize, P3D);
baseColor = color(102);
pressedColor = color(0, 15, 170);
currentcolor = baseColor;
textureMode( NORMALIZED );
color buttoncolor = color(102);
color highlight = color(51);
btn1 = new CircleButton(0, 0, 20, buttoncolor, highlight);
btn2 = new LineButton(22, 0, 20, buttoncolor, highlight);
}
void upix()
{
img.loadPixels();
for( int i=0; i<img.width*img.height-1; ++i)
{
int x = i % img.width;
int y = i / img.width;
if(btn1.isActive()){
if (tempEllipse.isInside(x,y)) {
img.pixels[i] = color(255);
}
} else if(btn2.isActive()){
float x0 = tempLine.getX0();
float x1 = tempLine.getX1();
float y0 = tempLine.getY0();
float y1 = tempLine.getY1();
float dx = abs(x1-x0);
float dy = abs(y1-y0);
float sx = 0;
float sy = 0;
float err = 0;
if (x0 < x1) {sx = 1;} else {sx = -1;}
if (y0 < y1) {sy = 1;} else {sy = -1;}
err = dx-dy;
while (x0!=x1 && y0!=y1){
img.pixels[(int)x0 + (int)y0 * img.width] = color(100,255,100);
float e2 = 2*err;
if (e2> -dy){
err = err -dy;
x0= x0 + sx;
}
if (e2 < dx) {
err = err + dx;
y0 = y0 + sy;
}
}
}
}
img.updatePixels();
}
void draw(){
fill(70);
rect(0,0,600,600);
if(justDraw){// this is the drawing part
btn1.update();
btn2.update();
btn1.display();
btn2.display();
beginShape(QUADS);
texture(img);
noStroke();
vertex(boardStart, boardEnd, 0, 0, 1);
vertex(boardStart, boardStart, 0, 0, 0);
vertex(boardEnd, boardStart, 0, 1, 0);
vertex(boardEnd, boardEnd, 0, 1, 1);
endShape();
if(startedEllipse){
ellipse(startX, startY, (mouseX-startX)*2, (mouseX-startX)*2);
}
if(startedLine){
stroke(155, 70, 25);
line(startX, startY, mouseX, mouseY);
}
} else {// this is where I want the movement and the camera to take place
camera(width/2+moveX, height/2, (height/2) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 0, 0, 1, 0);
float meret =10;
for (int i=0; i<meret; i++){
for (int j=0; j<meret; j++){
pushMatrix();
beginShape(QUADS);
texture(img);
vertex(boardStart+i*boardSize/meret, boardStart+(j+1)*boardSize/meret, 0, i*1/meret, (j+1)*1/meret);
vertex(boardStart+i*boardSize/meret, boardStart+j*boardSize/meret, 0, i*1/meret, j*1/meret);
vertex(boardStart+(i+1)*boardSize/meret, boardStart+j*boardSize/meret, 0, (i+1)*1/meret, j*1/meret);
vertex(boardStart+(i+1)*boardSize/meret, boardStart+(j+1)*boardSize/meret, 0, (i+1)*1/meret, (j+1)*1/meret);
endShape();
popMatrix();
}
}
}
}
void keyPressed()
{
if (key == 'o'){
moveX = 0;
justDraw = false;
}
if (key == 'a') moveX--;
if (key == 'd') moveX++;
}
boolean onBorad(){
if(mouseX>=boardStart && mouseX<=boardEnd && mouseY>=boardStart && mouseY<=boardEnd){
return true;
} else {return false;}
}
void mouseReleased() {
if(btn1.isOver()) {
if(btn1.isActive())btn1.setActive(false);
else{
btn1.setActive(true);
btn2.setActive(false);
}
} else if(btn2.isOver()) {
if(btn2.isActive())btn2.setActive(false);
else{
btn2.setActive(true);
btn1.setActive(false);
}
} else if(onBorad()){
if(btn1.isActive()){
if(!startedEllipse){
startX = mouseX;
startY = mouseY;
startedEllipse = true;
}else{
if(mouseButton == LEFT){
tempEllipse = new Ellipse(startX-boardStart , startY-boardStart, (mouseX-startX)*2, (mouseX-startX)*2);
startedEllipse = false;
upix();
}else{
startedEllipse = false;
}
}
}
if(btn2.isActive()){
if(!startedLine){
startX = mouseX;
startY = mouseY;
startedLine = true;
}else{
if(mouseButton == LEFT){
tempLine = new Line(startX-boardStart, startY-boardStart, mouseX-boardStart, mouseY-boardStart);
startedLine = false;
upix();
} else {
startedLine = false;
}
}
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean active = false;
boolean isOver(){if(mouseX>=x && mouseX<=x+size && mouseY>=y && mouseY<=y+size) return true; else return false;}
void setActive(boolean pressed){active = pressed;}
boolean isActive(){return active;}
void update()
{
if(active) currentcolor = pressedColor;
else if(isOver()) currentcolor = highlightcolor;
else currentcolor = basecolor;
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
ellipse(size/2+x, size/2+x, size-4,size-4);
}
}
class LineButton extends Button
{
LineButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
line(x+2, y+2, x+size-2, y+size-2);
}
}
class Ellipse{
int x;
int y;
float a;
float b;
public Ellipse(int x, int y, int width, int height) {
this.x = x;
this.y = y;
a = width/2.0;
b = height/2.0;
}
float getX(){
return this.x;
}
float getY(){
return this.y;
}
boolean isInside(int x, int y) {
float x1 = (x-this.x)/a;
float y1 = (y-this.y)/b;
return x1*x1 + y1*y1 < 1;
}
}
class Line{
float x1;
float y1;
float x2;
float y2;
float a;
float b;
public Line(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
void setX1(float a){
this.x1=a;
}
void setX2(float a) {
this.x2=a;
}
void setY1(float a){
this.y1=a;
}
void setY2(float a){
this.y2=a;
}
float getX0(){
return this.x1;
}
float getX1(){
return this.x2;
}
float getY0(){
return this.y1;
}
float getY1(){
return this.y2;
}
}
1