javascript - 加速度计 - 球中滚动

标签 javascript jquery math vector 2d

我想使用手机加速计来滚动球中的球。运动有效 正确的是,问题在于球撞到墙时。我怎样才能顺利滚动 球沿着大球内侧滑动的动画?

Ball in Ball

这是我当前移动球并检查交点的代码:

    onSuccess: function(acceleration) {
        var xPos = this.xPos + (-1 * (acceleration.x * 0.5));
        var yPos = this.yPos + (acceleration.y * 0.5);

        var intersect = this.intersection(xPos + 32, 
                                          yPos + 32, 
                                          32, 
                                          self.canvas.width * 0.5, 
                                          self.canvas.height * 0.5, 
                                          self.canvas.width * 0.5);
        if (!intersect) {
            this.yPos = yPos;
            this.xPos = xPos;
        }

        this.cnv.clearRect(0.0, 0.0, this.canvas.width, this.canvas.height);    
        this.cnv.drawImage(this.target, this.xPos, this.yPos);
    },

    intersection: function(x0, y0, r0, x1, y1, r1) {

        var a, dx, dy, d, h, rx, ry;
        var x2, y2;

        /* dx and dy are the vertical and horizontal distances between
         * the circle centers.
         */
        dx = x1 - x0;
        dy = y1 - y0;

        /* Determine the straight-line distance between the centers. */
        d = Math.sqrt((dy*dy) + (dx*dx));

        /* Check for solvability. */
        if (d > (r0 + r1)) {
            /* no solution. circles do not intersect. */
            return false;
        }
        if (d < Math.abs(r0 - r1)) {
            /* no solution. one circle is contained in the other */
            return false;
        }

        /* 'point 2' is the point where the line through the circle
         * intersection points crosses the line between the circle
         * centers.  
         */

        /* Determine the distance from point 0 to point 2. */
        a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;

        /* Determine the coordinates of point 2. */
        x2 = x0 + (dx * a/d);
        y2 = y0 + (dy * a/d);

        /* Determine the distance from point 2 to either of the
         * intersection points.
         */
        h = Math.sqrt((r0*r0) - (a*a));

        /* Now determine the offsets of the intersection points from
         * point 2.
         */
        rx = -dy * (h/d);
        ry = dx * (h/d);

        /* Determine the absolute intersection points. */
        var xi = x2 + rx;
        var xi_prime = x2 - rx;
        var yi = y2 + ry;
        var yi_prime = y2 - ry;

        return [xi, xi_prime, yi, yi_prime];
    }
};

感谢您的帮助:)

最佳答案

在只是滑动的情况下使用参数圆方程

x=x0+r*cos(a)
y=y0+r*sin(a)

地点:

  • x0,y0为大圆圆心
  • r = R0-R1
  • R0 是大圆半径
  • R1 是小圆半径

现在 Angular a

最简单的方法是放置a=重力方向:

a=atanxy(acceleration.x,acceleration.y)

atanxyatan2,它是 4 象限切线弧。如果您没有,请使用我的

并修正与坐标系的 Angular (可能取反或添加一些 90 度倍数)

[注释]

如果屏幕和设备加速度计之间具有兼容的坐标系,则只需将加速度矢量缩放到大小 |r| 并向其添加 (x0,y0) 即可没有任何测 Angular 函数的相同结果...

为了正确进行模拟,请使用 D'ALember 方程 + 圆边界

所以2D运动非常简单:

// in some timer with interval dt [sec]
velocity.x+=acceleration.x*dt;
velocity.y+=acceleration.y*dt;
position.x+=velocity.x*dt;
position.y+=velocity.y*dt;

现在 if (|position-big_circle_center|>big_circle_radius) 发生碰撞,因此当您不希望任何反弹(所有能量都被吸收)时,则:

position-=big_circle_center;
position*=big_circle_radius/|position|;
position+=big_circle_center;

现在您需要删除径向速度并仅保留切向速度:

normal=position-big_circle_center; // normal vector to surface
normal*=dot(velocity,normal);      // this is the normal speed part
velocity-=normal;                  // now just tangential speed should be left

enter image description here

所以在这之后,速度的切线(黄色)部分仍然存在......希望我没有忘记一些东西(比如制作单位向量或+/-某处......)

关于javascript - 加速度计 - 球中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216173/

相关文章:

javascript - 使用下拉列表显示/隐藏内容

javascript - 如何编写检查数组数组的过滤器

javascript - 如何访问在 Ajax 回调函数中初始化的变量?

javascript - jquery after 方法不返回生成的元素

math - 解释 Vinay Deolalikar 的证明 P != NP

java - 如何将数学符号(例如 R^n)放入 Java 字符串?

javascript - Codeigniter 2.1,Jquery - 500 内部服务器错误

javascript - reducer 中未定义初始变量

javascript - 为什么我不能使用 JavaScript 迭代保存在 html 数据选择器上的数组?

c# - 使用 Math.NET 进行约束的线性回归