繁体   English   中英

如何在 Android Studio 中获得已旋转的 object 的 (x, y)?

[英]How can I get (x, y) of an object in Android Studio which has been rotated?

我正在 Android Studio 中开发一款游戏,它的 object 围绕屏幕中心旋转。 我的问题是,由于某种原因,在旋转 object 后,x 和 y position 保持不变(我猜是因为 canvas 正在旋转,它在画布上仍然具有相同的点)。 我需要 x 和 y 坐标来计算碰撞检测。 在意识到 x 和 y 没有改变后,我尝试编写一个 function ,它可以使用基本三角函数计算 x 和 y,但由于某种原因它会吐出奇怪的值(也附在下面)。 我的问题是如何在主 canvas 中找到这个 object 的 x 和 y position? 代码如下。

private int x;
    private int y;

    private int rotate;
    private int width;
    private int height;

    private int color = Color.rgb(226, 255, 243);

    private Paint paint = new Paint();

    public Player(){
        rotate = 0;
        width = Constants.width / 10;
        height = Constants.height / 50;
        x = Constants.width / 2 - width / 2;
        y = Constants.height / 2 + Constants.height / 8;
    }

    public void setRotate(int rotate){
        this.rotate = rotate;
    }
    public int getRotate(){return rotate;}

    public int getX(){return x;}
    public int getY(){return y;}
    public int getWidth(){return width;}
    public int getHeight(){return height;}

    public void update(){
        if(rotate >= 360){
            rotate = 0;
        }
    }

    public void draw(Canvas canvas){
        paint.setColor(color);

        canvas.save();
        canvas.rotate(rotate, Constants.width / 2, Constants.height / 2);
        canvas.drawRect(x, y, x + width, y + height, paint);
        canvas.restore();
    }

下面的代码是我尝试查找 x 和 y 坐标,但它只吐出奇怪的值,我找不到问题所在。 此代码是我的主要 class。

        private int getPlayerX(int rotation){
        double angleA;
        double angleC;
        double sideC;
        double sideA;

        angleA = 180 - ((180 - rotation) + 90);
        sideC = Constants.width / 2 + Constants.height / 8;
        angleC = 180 - rotation;

        sideA = ((Math.sin(angleA) * sideC)) / (Math.sin(angleC));

        playerx = Constants.width / 2 + (int) sideA;

        return playerx;
    }
    private int getPlayerY(int rotation){
        double sideC;
        double angleC;
        double angleB;
        double sideB;

        sideC = Constants.width / 2 + Constants.height / 8;
        angleC = 180 - rotation;
        angleB = 90;

        sideB = sideC * (Math.sin(angleB)) / (Math.sin(angleC));

        playery = Constants.height + (int) sideB;

        return playery;
    }

尝试查看矩阵。 这是定义 object 转换的更好做法。 看这里https://en.m.wikipedia.org/wiki/Transformation_matrix示例:

public Player(){
   //...
   //matrix 3x3 with all transformations you need
   Matrix3f m;

   void rotate(float angle) {
       Matrix3f rot = new Matrix3f();
       rot.data = {cos(angle), -sin(angle), 0.0,
                   sin(angle), cos(angle), 0.0,
                   0.0,        0.0,        1.0 }
       m.Multiply(rot);  
   }

   //get the world position of a local point
   vec2f getWorld(vec2f inPt) {
       vec3f p = vec3f(inPt.x, inPt.y, 1.0);
       p = m.Transform(p);
       return vec2f(p.x, p.y);
   }

   //get the local position of a world point
   vec2f getLocal(vec2f inPt) {
       Matrix3f invM = m.Invert();
       vec3f p = vec3f(inPt.x, inPt.y, 1.0);
       p = invM.Transform(p);
       return vec2f(p.x, p.y);
   }

Matrix3f, vec3f, vec2f 类,Transform, Invert, Multiply 等你应该自己提供(你可以在网络中找到很多代码示例)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM