2017-04-10 24 views
-1
//declaringglobalvaribales 

float dia1=50; 

//diameteroftheface 

float x=400; 

float y=400; 

float speedX=4; 

float speedY=0; 

//setup 

void setup() { 

size(810, 810); 
} //draw 

void draw() { 

background(225); 

fill(225, 0, 0); 

stroke(0); 

ellipse(x, y, dia1, dia1); 

fill(0, 225, 0); 

//a nose 

triangle(x, y, x+10, y+10, x-10, y+10); 

//movingtheballXway 

x=x+speedX; 

//movingtheballYway 

y=y+speedY; 

//if it hits the left or right corners it will turn around 

if (x>width-dia1/2 || x<2+dia1/2) { 

    speedX=speedX*-1; 
} 
// it its hits the top or bottom it will turn around 

if (y>height-dia1/2 || y<2+dia1/2) { 

    speedY=speedY*-1; 
} 

// this code to move it according to the keys W S D A 

void keyPressed() { 

if (keyCode=='W') { 

    speedX=0; 

    speedY=-4; 

} 
if (keyCode=='S') { 

    speedX=0; 

    speedY=4; 

} 
if (keyCode=='A') { 

    speedX=-4; 

    speedY=0; 

} 
if (keyCode=='D') { 

    speedX=4; 

    speedY=0; 

} 

} 

我用鼻子做了這個球,用鑰匙W S D A在屏幕上移動。如果碰到邊緣,它會彈回來。如何將球向其移動的方向旋轉?我嘗試使用旋轉功能,但會拋出所有東西?

我試圖改變球面臨使其面對,因爲它的移動以同樣的方式的方向。我想爲此使用旋轉,但是一旦我使用旋轉,它會拋出所有的座標。旋轉它也沒有幫助。

我註釋掉我試圖做的東西。例如,我試圖將它翻譯成250,250,然後再旋轉它,但是然後切換X和Y座標。此外,球不會一直走到角落,它會移出(因爲它被翻譯下來)。

我需要什麼樣的旋轉/轉換邏輯的改變?

回答

0

很可能使用你的三角形旋轉功能的肆虐,你在你的平局循環中執行若干變量的旋轉,因爲你不告訴確切的處理要變換哪個對象。一種方法是查找pushMatrix和popMatrix函數(谷歌「處理pushMatrix」以查看如何使用類型和相關函數的有用信息)。因爲三角形是在每一幀的繪製循環中創建的,所以將其實現到代碼中會很麻煩。一個簡單的方法來轉換爲特定的形狀(在您的實例中爲三角形),將其存儲爲PShape,然後根據需要進行轉換。由於PShape的可以很容易地使用PShape職能轉變,你不必擔心你的轉換影響其他變量(所以沒有必要使用推/ popmatrix,這裏是實現你的三角作爲PShape你的代碼中的註釋版本。

//declaringglobalvaribales 

float dia1=50; 

//diameteroftheface 

float x=400; 

float y=400; 

float speedX=4; 

float speedY=0; 

//Initialize PShape which we can later store a triangle in 
PShape tri; 

void setup() { 
    size(810, 810); 

    //Initialize triangle - this triangle faces right 
    tri = createShape(TRIANGLE, 0, 10, 0, -10, 10, 0); 
} 


void draw() { 

    background(225); 

    fill(225, 0, 0); 

    stroke(0); 

    ellipse(x, y, dia1, dia1); 

    fill(0, 225, 0); 

    //Draw the stored PShape at x and y coordinate 
    shape(tri,x,y); 

    //movingtheballXway 

    x=x+speedX; 

    //movingtheballYway 

    y=y+speedY; 

    //if it hits the left or right corners it will turn around 

    if (x>width-dia1/2 || x<2+dia1/2) { 
    speedX=speedX*-1; 
    //Flip PShape rotation 
    tri.rotate(PI); 
    } // it its hits the top or bottom it will turn around 

    if (y>height-dia1/2 || y<2+dia1/2) { 
    speedY=speedY*-1; 
    //Flip PShape rotation 
    tri.rotate(PI); 
    } 
} 

    // this code to move it according to the keys W S D A 

    void keyPressed() { 

    if (keyCode=='W') { 
     speedX=0; 
     speedY=-4; 
     //reset triangle orientation (transformation matrix) to original then rotate to face UP 
     tri.resetMatrix(); 
     tri.rotate(-PI/2); 
    } 
    if (keyCode=='S') { 
     //reset triangle orientation (transformation matrix) to original then rotate to face DOWN 
     speedX=0; 
     speedY=4; 
     tri.resetMatrix(); 
     tri.rotate(PI/2); 
    } 
    if (keyCode=='A') { 
     //reset triangle orientation (transformation matrix) to original then rotate to face LEFT 
     tri.resetMatrix(); 
     tri.rotate(PI); 
     speedX=-4; 
     speedY=0; 
    } 
    if (keyCode=='D') { 
     //reset triangle orientation (transformation matrix) to original - it is already pointing right 
     tri.resetMatrix(); 
     speedX=4; 
     speedY=0; 
    } 
    } 

我懷疑你的下一步,或寫這篇文章的代碼更有效的方式可能是開始實施PVectors(谷歌處理PVectors看看如何使用類型和相關功能有用的信息)的位置和你的「球」的方向。下面是一些註釋代碼,開始向你展示瞭如何在當前的代碼來實現這一點。雖然有可以在此進行。欲瞭解更多有關如何矢量w許多改進ORK在處理本教程 - http://natureofcode.com/book/chapter-1-vectors/

//declaringglobalvaribales 

//diameteroftheface 
float dia1=50; 

//initialize position PVector and tell it where you want it to be - in this case 400,400 
PVector position = new PVector(400, 400); 

//how many steps you want your position to move per frame 
float speed=4; 

//initialize direction vector as 0,0 - the ellipse will not move until you give it a 
//direction as it is initialized with no direction 
PVector direction = new PVector(0, 0); 

void setup() { 
    size(810, 810); 
} 


void draw() { 

    background(225); 

    fill(225, 0, 0); 

    stroke(0); 

    //draw ellipse at your position PVector using the PVectors x and y values 
    ellipse(position.x, position.y, dia1, dia1); 

    fill(0, 225, 0); 

    //drawing a line to indicate what direction the ellipse is heading in using the position coordinates and the position plus direction 
    line(position.x, position.y, position.x+direction.x*4, position.y+direction.y*4); 

    // add the direction to the position to make it move 
    position =position.add(direction); 

    //if the position PVector is close to sketch edges invert its direction by multiplying direction PVector by -1 
    if (position.x>width-dia1/2 || position.x<2+dia1/2) { 
    direction.mult(-1); 
    } 

    if (position.y>height-dia1/2 || position.y<2+dia1/2) { 
    direction.mult(-1); 
    } 
} 

// this code to move it according to the keys W S D A 

void keyPressed() { 
    //set the direction coordinates based on keypresses 
    //also multiply the direction by speed variable so it moves at a speed set at top of script 
    if (keyCode=='W') { 
    direction.y = -1*speed; 
    direction.x = 0; 
    } 
    if (keyCode=='S') { 
    direction.y = 1*speed; 
    direction.x = 0; 
    } 
    if (keyCode=='A') { 
    direction.x = -1*speed; 
    direction.y = 0; 
    } 
    if (keyCode=='D') { 
    direction.x = 1*speed; 
    direction.y = 0; 
    } 
} 
0

如果你有一箇中心點,要面對的角度,並從該中心的距離,你可以使用cos()sin()計算終點。這裏有一個簡單的例子:

float angle = 0; 
float distance = 25; 

void draw(){ 
    angle += .01; 

    float startX = width/2; 
    float startY = height/2; 

    float endX = startX + cos(angle)*distance; 
    float endY = startY + sin(angle)*distance; 

    background(255); 
    line(startX, startY, endX, endY); 
} 

在未來,請儘量在發佈前下降到MCVE這樣縮小了你的問題。