how to make an object move in circular path?

六月ゝ 毕业季﹏ 提交于 2019-12-10 12:11:10

问题


consider two circles with (0,0) as center and 110 and 210 as radius respectively...

i.e i have CENTER as (0,0) and CIRCLE 1 radius as 110 and CIRCLE 2 radius as 210. Now i need to move an object tball in between these two circles.

Here is my code--

public void run() {

        while (isitok == true) {
            // perform drawing
            if (!holder.getSurface().isValid()) {
                continue;
            }
            Canvas canvas = holder.lockCanvas();
            canvas.drawARGB(255, 150, 150, 10);

            // System.out.println("Canvas matrix  -" + canvas.getm));
            Paint p = new Paint();

            // canvas.drawBitmap(tball, (x - tball.getWidth()) / 2,
            // (y - tball.getHeight()) / 2, p);

            p.setStyle(Paint.Style.STROKE);
            p.setColor(Color.WHITE);
            p.setColor(Color.parseColor("#0101DF"));

            canvas.drawCircle(canvas.getWidth() / 2,
                    canvas.getHeight() / 2, 60, p);
            canvas.drawCircle(canvas.getWidth() / 2,
                    canvas.getHeight() / 2, 110, p);

            float x = (canvas.getWidth() / 2) - (tball.getWidth() / 2);
            float y = (canvas.getHeight() / 2) - 110 + (110 - 60) / 2
                    - (tball.getHeight() / 2);

            canvas.drawBitmap(tball, x, y, p);

            float movingpts[];

            holder.unlockCanvasAndPost(canvas);
        }
    }

回答1:


Circle coordinates are

X = MX + R * cos( angle )
Y = MY + R * sin( angle )

where (MX,MY) is the center or midpoint of the circle and R the radius. For screen coordinates it is sometimes better to use

Y = MY - R * sin( angle )

to get the angle consistent with mathematical conventions on circle orientation.



来源:https://stackoverflow.com/questions/24242243/how-to-make-an-object-move-in-circular-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!