2017年11月21日 星期二

week08

期中作業製作

加入3D sphere  texture
還有球彈跳 跟反彈限制



背景貼圖 營造出空間感




PImage earth;
PShape go;

float ballvx = 0;
float ballvy = 0;
float ballvz= 0;
float ballx = 0;
float bally = 0;
float ballz = 1;

PImage floor;
PImage front;
PImage ceil;
PImage left;
PImage right;
PImage stop;
boolean fly=true;

int z=1;
int angle=0;

void setup()
{
  size(500,500,P3D);
  earth = loadImage("earth.png");
  floor = loadImage("floor.jpg");
  front = loadImage("front.jpg");
  left = loadImage("left.jpg");
  right = loadImage("right.jpg");
  ceil = loadImage("ceil4.jpg");
  stop = loadImage("us2.png");
  noStroke();
  go = createShape(SPHERE,50);
  go.setTexture(earth);
  background(255);
  smooth();
}

void draw()
{
   background(255,255,255);
   background();

   pushMatrix();
  lights();
  shapeMode(CENTER);
  translate(ballx,bally,-ballz);
  shape(go);
  popMatrix();

  if(fly)
  {
      ballvy +=1;
      ballvx *=0.9975;
      ballx += ballvx;
      bally += ballvy;
      ballz += (ballvz*z);

  }

  if (ballx > width-10)
  {
    ballx=width-10;
    ballvx=-ballvx;
  }

// changing directions after hitting the left wall
  if (ballx<80)
  {
    ballx=80; 
    ballvx=-ballvx;
  }

// loosing height after each bounce
  if (bally > height-20)
  {
    bally=height-5;
    ballvy = -ballvy*0.98;
  }

// slow the ball down faster when it is rolling.
  if (bally > height-19)
  {
    ballvx *=0.5;
  }

  if(ballz>300 )
  {
    z*=-1;
    ballvz-=0.5;
 
  }
  if(ballz<0)
  {
    ballz=0;
  }

   // zPosition=300;
   //
   // print(zPosition+"\n");



}

// re-throw the ball
void mouseDragged()
{
 // xPosition = mouseX;
 //yPosition = mouseY;
  ballvx += (mouseX - pmouseX)/8;
  ballvy += (mouseY - pmouseY)/8;

}

void mouseReleased()
{
 ballvz+=1;
}

背景部分程式碼:


void background()
{

 pushMatrix();
  beginShape();
      texture(front);
      vertex(0, 0, -300,0, 0);
      vertex(500, 0, -300,618, 0);
      vertex(500, 500,-300, 618, 258);
      vertex(0, 500, -300,0, 258);
  endShape();
 popMatrix();

 pushMatrix();
      beginShape();
      texture(stop);
      vertex(140, 5, -295,0, 0);
      vertex(370, 5, -295,800, 0);
      vertex(370, 155,-295, 800, 800);
      vertex(140, 155, -295,0, 800);
      endShape();
 popMatrix();


 pushMatrix();
  beginShape();
   texture(left);//left
  vertex(0, 0, 0,0, 0);
  vertex(0, 0, -300,618, 0);
  vertex(0, 500, -300,618, 258);
  vertex(0, 500,0, 0, 258);
  endShape();
 popMatrix();

 pushMatrix();
  beginShape();
    texture(right); //right
    vertex(500, 0,   0, 0, 0);
    vertex(500, 0, -300,618, 0);
    vertex(500, 500, -300,618, 258);
    vertex(500, 500,0, 0, 258);
  endShape();
 popMatrix();

 pushMatrix();
  beginShape(); //ceil
    texture(ceil);
    vertex(0, 0, 0,532, 0);
    vertex(500, 0, 0,0, 0);
    vertex(500, 0, -300,0, 214);
    vertex(0, 0,-300, 532, 214);
 
 
 
  endShape();
 popMatrix();

 pushMatrix();
  beginShape();
    texture(floor);//floor
    vertex(500, 500,0, 618, 0);
    vertex(500, 500, -300,618, 258);
    vertex(0, 500, -300,0, 258);
    vertex(0, 500, 0,0,0);
  endShape();
 popMatrix();

 pushMatrix();
    beginShape();
    translate(328, 115,-270);
    scale(1.85,1.8,2);
    endShape();
 popMatrix();


}



沒有留言:

張貼留言