我使用processing java庫來模擬使用牛頓物理學在兩個牆之間彈跳的球;水平速度是一個常數,因爲沒有加速度。我得到了模擬工作,但我想指定地板的高度。然而,在我的方程式中,當球從屏幕底部起10個像素時嘗試改變方向,每次反彈後,球在「地板」下方越來越低。如果我將場地提升到> 20像素,那麼球不會停下來,而是無限期地反彈。以下是相關的代碼: 請注意,處理的座標系從頂部開始,向下和向右延伸。我在這裏先向您的幫助表示感謝。處理中的重力(樓層)
public class Testing extends PApplet {
boolean inFloor=false;
float xPosition=500;
float yPosition=200;
float xVelocity=25;
float yVelocity=-80.0f;
float yAccelleration=+10.0f;
float elasticity=0.80f;
public void setup(){
size(displayWidth,displayHeight);
noStroke();
ellipseMode(RADIUS);
frameRate(35);
}
public boolean sketchFullScreen() {
return true;
}
public void draw(){
background(0);
//Changes direction of motion when hitting a wall
if(xPosition>=displayWidth||xPosition<0){
xVelocity=-xVelocity;
}
//supposed to change direction of motion when the ball hits the floor
if(yPosition>=displayHeight-20){
yPosition=(displayHeight-20);
yVelocity=-(yVelocity)*elasticity;
if(yVelocity>=-1 && yVelocity<=0){
xVelocity=xVelocity*elasticity;
yVelocity=0;
yAccelleration=0;
}
}
else{
yVelocity=yVelocity+yAccelleration;
}
yPosition=yVelocity+yPosition;
xPosition=xPosition+xVelocity;
ellipse(xPosition,yPosition,10,10);
}
編輯:難道這可能是一個時機的問題?
編輯:謝謝你所有的迴應。不幸的是,我不能對他們中的任何一個都滿意,(只有6個代表)。我結合了@ tobius_k的回答,@ Roberto_Mereghetti的回答以及OpenProcessing.org的一些示例代碼,並解決了這個問題。在下面提供的解決方案中,由於畫布以像素(整數值)爲單位進行度量,因此使用浮點數來指定座標會導致處理中出現圖形故障。所以我實現了一個系統,其中float值被四捨五入,並且將十進制值添加到累加器(「xRounder」和「yRounder」),當-1和1大於-1時,它們被舍入並添加到Ball的當前位置。這給了我一個地板!
終極密碼:
import processing.core.*;
//import processing.xml.*;
import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;
public class Testing extends PApplet {
int xPosition=500;
int yPosition=200;
float xRounder=0;
float yRounder=0;
float xVelocity=25;
float yVelocity=-80.0f;
float yAccelleration=+10.0f;
float elasticity=0.80f;
public void setup(){
size(displayWidth,displayHeight);
noStroke();
ellipseMode(RADIUS);
frameRate(15);
}
public boolean sketchFullScreen() {
return true;
}
/* (non-Javadoc)
* @see processing.core.PApplet#draw()
*/
public void draw(){
background(0);
yPosition=round(yVelocity)+yPosition;
yRounder+=(yVelocity-round(yVelocity));
xPosition=round(xVelocity)+xPosition;
xRounder+=(xVelocity-round(xVelocity));
if(xRounder>=1||xRounder<=-1){
xPosition=xPosition+round(xRounder);
xRounder=xRounder-round(xRounder);
}
if(yRounder>=1||yRounder<=-1){
yPosition+=round(yRounder);
yRounder=yRounder-round(yRounder);
}
if(yPosition>displayHeight-50 && yVelocity>0){
yPosition=displayHeight-50;
yVelocity=-(yVelocity)*elasticity;
xVelocity=xVelocity*elasticity;
}
if(xPosition>=displayWidth||xPosition<0){
xVelocity=-xVelocity;
}
yVelocity=yVelocity+yAccelleration;
ellipse(xPosition,yPosition,10,10);
}
static public void main(String args[]) {
PApplet.main(new String[] { "--bgcolor=#ECE9D8", "Testing" });
// new Testing().setVisible(true);
}
}
'yVelocity> = - 1 && yVelocity <= 0'可能不會發生,具體取決於加速度......它實際上可能只是變成了'yVelocity <-1' – oldrinb