在一个圆圈移动物体 - 机器人 [英] moving an object in a circle - android
问题描述
昨天我这个问题,我得到了一些有用的帮助,但未能解决问题。只是想我会继续努力。
I posted this question yesterday, I got some useful help but was unable to solve the problem. Just figured I'd keep trying.
确定。我试图在Android的画布上转了一圈一球的举动。做一些研究和阅读一些类似的问题后 - 我想我得到了逻辑下来,但形状保持静止。基本上我做X = A + RCOS(THETA),Y =雨(THETA)。我真的不知道是什么问题。我的code如下。有谁知道我在做什么错?我读过的其他问题,不知道为什么,我不能使它工作。
Ok. I'm trying to make a ball move in a circle on a canvas in Android. After doing some research and reading a few similar questions - I think I got the logic down but the shape remains stationary. Basically I'm doing x = a +rcos(theta), y = rain(theta). I'm not really sure what the problem is. My code is below. Does anyone know what I'm doing wrong? I've read the other questions and don't know why I can't make it work.
public class DrawingTheBall extends View {
Bitmap bball;
int x,y, theta;
public DrawingTheBall(Context context) {
super(context);
// TODO Auto-generated constructor stub
bball = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
x = 0;
y = 0;
theta = 45;
}
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;
theta = (int) Math.toRadians(10);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if(x < canvas.getWidth()){
x = (int) (a +r*Math.cos(theta));
}else{
x = 0;
}
if(y < canvas.getHeight()){
y = (int) (b +r*Math.sin(theta));
}else{
y = 0;
}
Paint p = new Paint();
canvas.drawBitmap(bball, x, y, p);
invalidate();
}
}
推荐答案
您必须增加 THETA
就行了:
theta = (int) Math.toRadians(10);
如果你总是在相同的角度画,你会总是吸引到同一个位置。
If you always draw at the same angle, you'll always draw to the same location.
编辑:
您可以把上面的线在构造函数,然后在的onDraw
,你可以这样做:
You could place the above line in your constructor, and then in onDraw
, you could do:
theta = (theta + 0.1) % (2 * Math.PI)
这篇关于在一个圆圈移动物体 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!