[英]Android Math does not work
private void gotoPos()
{
spaceX = x2 - x;
spaceY = y2 - y;
if (Math.abs(spaceX) >= Math.abs(spaceY)) {
xSpeed = Math.round(spaceX * (3/Math.abs(spaceX)));
ySpeed = Math.round(spaceY * (3/Math.abs(spaceX)));
}
With this code I want to move an object to the position x2 and y2. 使用此代码,我想将对象移动到位置x2和y2。 x and y is the current position of the object.
x和y是对象的当前位置。 spaceX is the space that is between the object and the x position it should go to.
spaceX是对象与应该到达的x位置之间的空间。 The same for spaceY.
对于spaceY也是一样。
But I don't want the object to move more than 3 Pixels per draw. 但是我不希望对象每次平移超过3个像素。
Example: object position: x = 35, y = 22 示例:对象位置:x = 35,y = 22
Point it should go to: x2 = 79, y2 = 46 将其指向:x2 = 79,y2 = 46
space between them: spaceX = 79-35 = 44, spaceY = 46-22 = 24 它们之间的空间:spaceX = 79-35 = 44,spaceY = 46-22 = 24
spaceX is bigger then spaceY so: spaceX大于spaceY,因此:
xSpeed = 44 * (3/44) = 3, ySpeed = 24 * (3/44) = 1.63 = 2 xSpeed = 44 *(3/44)= 3,ySpeed = 24 *(3/44)= 1.63 = 2
But it does not work like this. 但这不是这样的。 When I start the app the object does not go to x2 and y2.
当我启动应用程序时,对象不会转到x2和y2。 If I change xSpeed = spaceX;
如果我改变xSpeed = spaceX; ySpeed = spaceY;
ySpeed = spaceY;
The object moves to the position but I do not want it to go there instantly 物体移动到该位置,但我不希望它立即移动到该位置
Complete Code: 完整的代码:
public class Sprite
{
private boolean walking = true;
private int actionWalk = 0;
private Random rnd;
private int checkIfAction;
private int nextAction = 0;
static final private int BMP_COLUMNS = 4;
static final private int BMP_ROWS = 4;
private int[] DIRECTION_TO_SPRITE_SHEET = { 1, 0, 3, 2 };
public int x=-1;
private int y=-1;
public int xSpeed;
private int ySpeed;
private int width;
private int height;
private int bottomSpace;
private Bitmap bmp;
private GameView theGameView;
private int currentFrame=0;
private int x2, y2;
private boolean isTouched;
private int spaceX, spaceY;
D d
public Sprite(GameView theGameView, Bitmap bmp)
{
this.theGameView = theGameView;
this.bmp = bmp;
this.width = bmp.getWidth() / BMP_COLUMNS;
this.height = bmp.getHeight() / BMP_ROWS;
rnd = new Random();
xSpeed = 0;
ySpeed = 0;
}
public void shareTouch(float xTouch, float yTouch)
{
x2 = (int) xTouch;
y2 = (int) yTouch;
isTouched = true;
}
private void gotoPos()
{
spaceX = x2 - x;
spaceY = y2 - y;
if (Math.abs(spaceX) >= Math.abs(spaceY)) {
xSpeed = Math.round(spaceX * (3/Math.abs(spaceX)));
ySpeed = Math.round(spaceY * (3/Math.abs(spaceX)));
}
else {
xSpeed = spaceX;
ySpeed = spaceY;
}
}
D d
private void bounceOff()
{
bottomSpace = theGameView.getHeight() - y;
if (x > theGameView.getWidth() - (width * theGameView.getDensity()) - xSpeed - bottomSpace / 2 || x + xSpeed < bottomSpace / 2)
{
xSpeed = -xSpeed;
}
x = x + xSpeed;
if (y > theGameView.getHeight() - (height * theGameView.getDensity()) - ySpeed || y + ySpeed < theGameView.getHeight() / 2)
{
ySpeed = -ySpeed;
}
y = y + ySpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
d d
public void onDraw(Canvas canvas)
{
if (x == -1)
{
x = (theGameView.getWidth() / 2);
y = (theGameView.getHeight() / 2 + theGameView.getHeight() / 4);
}
if (isTouched == true)
{
gotoPos();
}
/* if (nextAction == 100)
{
action();
nextAction = 0;
}
nextAction += 1;*/
bounceOff();
int sourceX, sourceY;
if (walking == true)
{
sourceX = currentFrame * width;
}
else
{
sourceX = 0;
}
sourceY = getAnimationRow() * height;
Rect source = new Rect(sourceX, sourceY, sourceX + width, sourceY + height);
Rect destine = new Rect(x, y, (int) (x + (width * theGameView.getDensity())), (int) (y + (height * theGameView.getDensity())));
canvas.drawBitmap(bmp, source, destine, null);
}
d d
private int getAnimationRow()
{
double directionDouble = (Math.atan2(xSpeed, ySpeed) / (Math.PI / 2) + 2);
int spriteDir = (int) Math.round(directionDouble) % BMP_ROWS;
return DIRECTION_TO_SPRITE_SHEET[spriteDir];
}
} }
Simple problem: you use integer arithmetic and don't understand this: 一个简单的问题:您使用整数算术,但对此不理解:
spaceX * (3/Math.abs(spaceX))
The result will be 0
in nearly all cases as 3/x
with x > 3
is 0
all the time. 其结果将是
0
在几乎所有情况下为3/x
与x > 3
是0
的所有时间。
To make your program working use either floating point arithmetic or rewrite your formulas to work as expected. 要使程序正常运行,请使用浮点算术或重写公式以使其按预期工作。
To use floating point arithetic you have to change to 要使用浮点运算,您必须更改为
spaceX * (3.0/Math.abs(spaceX))
assuming that you variable spaceX
is also floating point. 假设您的变量
spaceX
也是浮点数。
Also you can use 你也可以用
(spaceX * 3) / Math.abs(spaceX)
if you want to stay with integers (what I suppose). 如果您想保留整数(我想)。
Given points a Vector2d(x, y) and b Vector2d(x2, y2)- 给定点的Vector2D(X,Y)和B的Vector2D(X2,Y2) -
Create a vector V from a to b by subtracting b from a as you did. 通过从减去B中你没有创建从一个矢量V到B。 Normalize vector V into a unit vector and multiply it with the distance you want.
将向量V归一化为单位向量,然后将其乘以所需的距离。 Then add the resulting vector to point a .
然后将结果向量相加指向a 。
On update: 更新时:
a.add(b.subtract(a).norm().multiply(d));
Depending on your vector implementation, modify properly the above pseudo code. 根据您的矢量实现,正确修改上面的伪代码。
There is a logical fallacy in your code: what if Math.abs(spaceX) < Math.abs(spaceY))
? 您的代码中存在逻辑上的谬误:如果
Math.abs(spaceX) < Math.abs(spaceY))
怎么办? Then your object would not move at all. 这样,您的对象将根本不会移动。
What you calculate, 'x-distance / y-distance', is usually considered angle, not speed. 您所计算的“ x距离/ y距离”通常被认为是角度,而不是速度。 Speed is 'distance / time'.
速度是“距离/时间”。 You can calculate the distance, and you should decide on a reasonable speed for your object.
您可以计算距离,并且应该确定对象的合理速度。 Since you know your fps -- 20 --, you can then calculate how many pixels your object needs to move in each frame.
由于您知道fps-20-,因此可以计算出对象在每一帧中需要移动多少像素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.