java 无法使物体做圆周运动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16802431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trouble making object move in a circle
提问by David
I'm new to animation in android. I'm working with a tutorial I have found on youtube. The app draws a picture of a ball on a canvas and then moves diagonally. I'd like to make the ball move in a circle. I've found some information about the basic math of circular motion but I'm having trouble implementing it. Could someone look at my code and tell me what I'm doing wrong. Thanks.
我是 android 动画的新手。我正在使用我在 youtube 上找到的教程。该应用程序在画布上绘制一个球的图片,然后沿对角线移动。我想让球转一圈。我找到了一些关于圆周运动基本数学的信息,但我在实现它时遇到了麻烦。有人可以看看我的代码并告诉我我做错了什么。谢谢。
Here is my code:
这是我的代码:
public class DrawingTheBall extends View {
Bitmap bball;
int x,y;
public DrawingTheBall(Context context) {
super(context);
// TODO Auto-generated constructor stub
bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
x = 0;
y = 0;
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
float a = 10;
float b = 10;
float r = 20;
double theta = 0;
theta = Math.toRadians(45);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if(x < canvas.getWidth()){
//x += 10;
x = (int) (a +r*Math.cos(theta));
}else{
x = 0;
}
if(y < canvas.getHeight()){
//y += 10;
y = (int) (b +r*Math.sin(theta));
}else{
y = 0;
}
Paint p = new Paint();
canvas.drawBitmap(bball, x, y, p);
invalidate();
}
}
}
回答by oo_miguel
Basically you need to use the Sine and Cosine trigonometric functions, which given the angle will give you the ratios of the corresponding x, and y coordinates on your screen.
基本上你需要使用正弦和余弦三角函数,给定角度会给你屏幕上相应的 x 和 y 坐标的比率。
something along:
一些东西:
double x = a + r * sin(angle);
double y = b + r * cos(angle);
should work.
应该管用。
where:
在哪里:
r - is the radius of the circle
(a,b) - is the center of the circle
angle - is the desired angle
Of course you need to increment the angle, in order for your object to move.
当然,您需要增加角度,以便您的对象移动。
回答by arynaq
Mathematically a point on the circle is defined by an angle theta
and a distance from the center radius
. In your code the angle is a constant 100
so it never moves on the circle. What you want to do is increase the angle in your update.
在数学上,圆上的点由角度theta
和距中心的距离定义radius
。在您的代码中,角度是一个常数,100
因此它永远不会在圆上移动。您想要做的是增加更新的角度。
theta = theta + Math.toRadians(10);
x = a + r*Math.cos(theta);
y = b + r*Math.sin(theta);
This will let you move on a circle that centers on (a,b)
with radius r
, 10 degrees
at a time.
这将让你移动一个圆上中心(a,b)
半径r
,10 degrees
在同一时间。
To your comment, add theta
as a field and don't set it to 45
inside onDraw
, if you want to start at 45 degrees you can initialize it to 45 inside your constructor.
在您的评论中,添加theta
为一个字段并且不要将其设置为45
inside onDraw
,如果您想从 45 度开始,您可以在构造函数中将其初始化为 45 。
int x,y;
to
int x,y, theta;
To your comment
给你的评论
int x,y, theta;
public DrawingTheBall(Context context) {
super(context);
bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
x = 0;
y = 0;
theta = 45;
}
And
和
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
float a = 10;
float b = 10;
float r = 20;
// double theta = 0; //You are using a local variable that shadows the field, it starts at 0 everytime
// theta = Math.toRadians(45); //You are setting it to 45 degrees everytime, instead:
theta = theta + Math.toRadians(10); //Increase of 10 degrees
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if(x < canvas.getWidth()){
//x += 10;
x = (int) (a +r*Math.cos(theta));
}else{
x = 0;
}
if(y < canvas.getHeight()){
//y += 10;
y = (int) (b +r*Math.sin(theta));
}else{
y = 0;
}
Paint p = new Paint();
canvas.drawBitmap(bball, x, y, p);
invalidate();
}
回答by Alec.
Take a look at this post from another SO ticket, seems very similar.
从另一个 SO 票中看一下这篇文章,看起来非常相似。