Java me 台球移动算法
我正在用Java2ME移动一个台球。当速度稳定时,这很容易。我根据x和y的速度改变球的x和y坐标。它们都是整数。没问题。然而,一个普通的台球必须先快速移动,然后减速并停止。因为球的x和y坐标是整数,我不能按百分比减小x和y的速度。我的意思是,如果速度是9,我想减少10%,我不能做“9*0.1”,因为它必须是一个整数。我知道坐标不能是两倍。我能做什么? 守则:Java me 台球移动算法,java-me,midp,Java Me,Midp,我正在用Java2ME移动一个台球。当速度稳定时,这很容易。我根据x和y的速度改变球的x和y坐标。它们都是整数。没问题。然而,一个普通的台球必须先快速移动,然后减速并停止。因为球的x和y坐标是整数,我不能按百分比减小x和y的速度。我的意思是,如果速度是9,我想减少10%,我不能做“9*0.1”,因为它必须是一个整数。我知道坐标不能是两倍。我能做什么? 守则: public void move() { //... Move the ball at the given velocity.
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;
}
}
公共作废移动(){
//…以给定的速度移动球。
m_x+=m_velocityX;//m_x:x球的坐标
m_y+=m_velocityY;//m_y:y球的坐标
//…球击中边界并改变方向
如果(m_x<0){//如果在左侧或左侧以外
m_x=0;//靠边放置,并
m_velocityX=-m_velocityX;//反向。
}else如果(m_x>m_rightbund){//如果在右侧或右侧之外
m_x=m_rightbund;//靠右边缘放置。
m_velocityX=-m_velocityX;//反向。
}
如果(m_y<0){//如果我们在顶部
m_y=0;
m_-velocityY=-m_-velocityY;
}如果(m_y>m_bottomBound){//如果我们在底部
m_y=m_底界;
m_-velocityY=-m_-velocityY;
}
}
如果速度必须是整数,只需将值更新到浮点计算的下限即可。因此,要将速度降低10%:
m_velocityX = floor(m_velocityX * 0.9);
你可能想有一天做一些更为有趣的事情,但这看起来简单可行。你应该将球速存储为
velocity
和angle
此外,提到的每个变量都应该是
float
或double
然后一切都会变得更容易,更准确
然后,算法将是:
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);
}
浮动x,y,速度,角度
int ix,iy;
...
{
如果(y)
如果速度是9,我想把它减小10%,我不能做“9*0.1”,因为它必须是一个整数
放大速度和坐标(将eg乘以256或向左移动eg 8)
计算按比例增加的速度“9*256/10”的减少量
计算新的(放大的)位置和速度
缩小
大概如下
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
球移动(球、边界、跟踪器){
tracer.trace(“按比例放大以处理加速度=速度/8”);
刻度=新刻度(256);
Int2D位置=向上缩放(球位置);
速度=新速度(放大(球速度));
tracer.trace(“将加速度计算为按比例增加的速度/8”);
Int2D加速度=新刻度(8)。向下(速度);
tracer.trace(“以给定速度移动球”);
位置=位置加(速度);
tracer.trace(“减慢速度”);
速度=速度。减速(加速度);
tracer.trace(“缩小到原始尺寸”);
球=新球(缩放向下(位置)、新速度(缩放向下(速度));
tracer.trace(“如果球击中边界并改变方向,则进行调整”);
返回边框。需要反射(球);
}
接口跟踪程序{void trace(字符串msg);}
班级规模{
最终整数刻度;//最好是2的幂
Scale(int Scale){this.Scale=Scale;}
Int2D-up(Int2D-src){返回新的Int2D(src.x*scale,src.y*scale);}
int2ddown(int2dsrc){返回新的Int2D(src.x/scale,src.y/scale);}
}//比例
阶级边界{
最终Int2D左上角、右下角;
边框(Int2D左上角,Int2D右下角){
this.topLeft=topLeft;
this.bottomRight=bottomRight;
}
需要的球反射率(球){
如果(在(球内){返回球;}
抛出新的UnsupportedOperationException(“尚未实现”);
}
内部私有布尔值(Ball-Ball){
返回(球位置.x,左上角.x,右下角.x)
&&内(球位置y,左上角y,右下角y);
}
内部私有布尔值(int值、int最小值、int最大值){
返回值>最小值&值<最大值;
}
}//边界
班级舞会{
最终Int2D位置;
最终速度;
球(Int2D位置、速度){
这个位置=位置;
速度=速度;
}
}//球
类速度扩展为Int2D{
速度(int2dsrc){super(src.x,src.y);}
速度减速(Int2D加速){
返回新的速度(这个.减去(加速度));
}
Velocity reflectX(){返回新的速度(新的Int2D(-x,y));}
速度反射(){return new Velocity(new Int2D(x,-y));}
}//速度
类Int2D{
最终整数x,y;
Int2D(intx,inty){this.x=x;this.y=y;}
int2dplus(int2dother){返回新的Int2D(x+other.x,y+other.y);}
Int2D减号(int2dother){返回新的Int2D(x-other.x,y-other.y);}
}//Int2D
非常感谢您的贡献。ix和iy必须是整数,因为ix和iy是坐标,坐标不能是双精度或浮点数。因为我会将它们转换为整数,无需调用floor函数。如果(y