2011-06-25 56 views
0

我想在Java2ME中移動一個池球。速度穩定時很容易。我根據x和y速度更改球的x和y座標。他們都是整數。沒問題。然而,一個普通的球池必須先快速放慢速度然後停下來。因爲球的x和y座標是整數,所以我不能按百分比減小x和y的速度。我的意思是如果速度是9,我想減少10%,我不能做「9 * 0.1」,因爲它必須是一個整數。我知道座標不能是雙倍的。我能做什麼? 代碼:池球移動算法

public void move() { 
    //... Move the ball at the given velocity. 
    m_x += m_velocityX; // m_x: x coordinate of the ball 
    m_y += m_velocityY; // m_y: y coordinate of the ball 


    //... ball hits the borders and change the way 
    if (m_x < 0) {     // If at or beyond left side 
     m_x   = 0;   // Place against edge and 
     m_velocityX = -m_velocityX; // reverse direction. 

    } else if (m_x > m_rightBound) { // If at or beyond right side 
     m_x   = m_rightBound; // Place against right edge. 
     m_velocityX = -m_velocityX; // Reverse direction. 
    } 

    if (m_y < 0) {     // if we're at top 
     m_y  = 0; 
     m_velocityY = -m_velocityY; 

    } else if (m_y > m_bottomBound) { // if we're at bottom 
     m_y  = m_bottomBound; 
     m_velocityY = -m_velocityY; 
    } 
} 

回答

0

如果速度必須是整體的,只是更新值浮點計算的地板上。所以把速度降低10%:

m_velocityX = floor(m_velocityX * 0.9); 

你可能想在某一天做些更有趣的事情,但是這看起來很簡單和可行。

0

您應該將球的速度存儲爲velocityangle
此外,提到的每個變量應該是floatdouble

然後,一切都會更容易,更準確。

算法將爲:

float x,y,velocity,angle 
int ix,iy; 
... 
{ 
    if(y<0) angle=-angle; 
    ... etc, etc. 

    velocity*=0.95; 
    x+=velocity*cos(angle); 
    y+=velocity*sin(angle); 

    // And you get your precious integers ... 
    ix=floor(x); 
    iy=floor(y); 
} 
+0

非常感謝您對您的貢獻。 ix和iy必須是整數,因爲ix和iy是座標,座標不能是雙精度或浮點數。因爲我將它們轉換爲整數,不需要調用floor函數。如果(y <0)angle = -angle;這一個工作,但我不能做的部分,如果(X <0)。通常角度= 180°角度;但它沒有奏效。我該怎麼辦 – osman

0

如果速度是9和欲由10%減少它我不能做「9 * 0.1」,因爲它必須是一個整數

  1. 規模向上速度和座標(乘以256,例如或移位通過例如8左)
  2. 計算減少用於放大速度「9 *一十分之二百五十六」
  3. 計算新的(擴大)位置和速度
  4. 比例放倒

關於像下面...

Ball move(Ball ball, Border border, Tracer tracer) { 
    tracer.trace("scale stuff up to handle acceleration = velocity/8"); 
    Scale scale = new Scale(256); 
    Int2D position = scale.up(ball.position); 
    Velocity velocity = new Velocity(scale.up(ball.velocity)); 

    tracer.trace("calculate acceleration as scaled up velocity/8"; 
    Int2D acceleration = new Scale(8).down(velocity); 

    tracer.trace("Move the ball at the given velocity"); 
    position = position.plus(velocity); 

    tracer.trace("slow down velocity"); 
    velocity = velocity.slowDown(acceleration); 

    tracer.trace("scale back down to original"); 
    ball = new Ball(scale.down(position), new Velocity(scale.down(velocity))); 

    tracer.trace("adjust if ball hits the borders and change the way"); 
    return border.reflectIfNeeded(ball); 
} 

interface Tracer { void trace(String msg); } 

class Scale { 
    final int scale; // better be power of 2 
    Scale(int scale) { this.scale = scale; } 
    Int2D up(Int2D src) { return new Int2D(src.x * scale, src.y * scale); } 
    Int2D down(Int2D src) { return new Int2D(src.x/scale, src.y/scale); } 
} // Scale 

class Border { 
    final Int2D topLeft, bottomRight; 
    Border(Int2D topLeft, Int2D bottomRight) { 
     this.topLeft = topLeft; 
     this.bottomRight = bottomRight; 
    } 
    Ball reflectIfNeeded(Ball ball) { 
     if (within(ball)) { return ball; } 
     throw new UnsupportedOperationException("not yet implemented"); 
    } 
    private boolean within(Ball ball) { 
     return within(ball.position.x, topLeft.x, bottomRight.x) 
       && within(ball.position.y, topLeft.y, bottomRight.y); 
    } 
    private boolean within(int value, int min, int max) { 
     return value > min && value < max; 
    } 
} // Border 

class Ball { 
    final Int2D position; 
    final Velocity velocity; 
    Ball(Int2D position, Velocity velocity) { 
     this.position = position; 
     this.velocity = velocity; 
    } 
} // Ball 

class Velocity extends Int2D { 
    Velocity(Int2D src) { super(src.x, src.y); } 
    Velocity slowDown(Int2D acceleration) { 
     return new Velocity(this.minus(acceleration)); 
    } 
    Velocity reflectX() { return new Velocity(new Int2D(-x, y)); } 
    Velocity reflectY() { return new Velocity(new Int2D(x, -y)); } 
} // Velocity 

class Int2D { 
    final int x, y; 
    Int2D(int x, int y) { this.x = x; this.y = y; } 
    Int2D plus(Int2D other) { return new Int2D(x + other.x, y + other.y); } 
    Int2D minus(Int2D other) { return new Int2D(x - other.x, y - other.y); } 
} // Int2D