如何在循环路径中的物体移动? [英] how to make an object move in circular path?

查看:124
本文介绍了如何在循环路径中的物体移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑两个圆圈(0,0)为中心,以110和210分别半径...

即我有中心为(0,0)和1圈半径110和2圈半径210。
现在我需要的在移动这两个圆之间的物体tball

下面是我的code -

 公共无效的run(){        而(isitok ==真){
            //进行绘图
            如果(!holder.getSurface()。的isValid()){
                继续;
            }
            帆布帆布= holder.lockCanvas();
            canvas.drawARGB(255,150,150,10);            //的System.out.println(画布上的矩阵 - + canvas.getm));
            涂料P =新的油漆();            // canvas.drawBitmap(tball,(X - tball.getWidth())/ 2,
            //(γ - 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)的;            浮X =(canvas.getWidth()/ 2) - (tball.getWidth()/ 2);
            浮Y =(canvas.getHeight()/ 2) - 110 +(110 - 60)/ 2
                     - (tball.getHeight()/ 2);            canvas.drawBitmap(tball,X,Y,P);            浮movingpts [];            holder.unlockCanvasAndPost(画布);
        }
    }


解决方案

圈子坐标

  X = MX + R * COS(角度)
Y = MY + R *罪(角)

,其中(MX,MY)是中心或圆的中点,R是半径。对于屏幕坐标,有时最好使用

  Y = MY  -  R *罪(角)

要获得与盘的方向数学约定相一致的角度。

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);
        }
    }

解决方案

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.

这篇关于如何在循环路径中的物体移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆