Javascript 加速计-球中滚动球

Javascript 加速计-球中滚动球,javascript,jquery,math,vector,2d,Javascript,Jquery,Math,Vector,2d,我想用手机加速计在球里滚一个球。这项运动奏效了 没错,问题是当球撞到墙上时。我怎样才能顺利地滚动 球沿较大球的内侧滑动的动画 这是我移动球并检查交叉点的当前代码: onSuccess: function(acceleration) { var xPos = this.xPos + (-1 * (acceleration.x * 0.5)); var yPos = this.yPos + (acceleration.y * 0.5); v

我想用手机加速计在球里滚一个球。这项运动奏效了 没错,问题是当球撞到墙上时。我怎样才能顺利地滚动 球沿较大球的内侧滑动的动画

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

    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];
    }
};
on成功:功能(加速){
var xPos=this.xPos+(-1*(加速度.x*0.5));
var yPos=这个.yPos+(加速度.y*0.5);
var intersect=此.intersect(xPos+32,
yPos+32,
32, 
self.canvas.width*0.5,
self.canvas.height*0.5,
self.canvas.width*0.5);
如果(!相交){
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);
},
交点:函数(x0,y0,r0,x1,y1,r1){
变量a、dx、dy、d、h、rx、ry;
变量x2,y2;
/*dx和dy是两个方向之间的垂直和水平距离
*圆圈的中心。
*/
dx=x1-x0;
dy=y1-y0;
/*确定中心之间的直线距离*/
d=数学sqrt((dy*dy)+(dx*dx));
/*检查可解性*/
如果(d>(r0+r1)){
/*没有解决方案。圆不相交*/
返回false;
}
if(d

感谢您的帮助:)

仅在滑动情况下使用参数圆方程

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

  • x0,y0
    是大圆心
  • r=R0-R1
  • R0
    是大圆半径
  • R1
    是小圆半径
现在角度
a

最简单的方法是将
a=重力方向
放置在以下位置:

a=atanxy(acceleration.x,acceleration.y)
atanxy
atan2
,它是四象限的tangens弧。如果你没有,用我的

并将角度校正到坐标系(可能为负数或增加90度倍数)

[notes]

如果屏幕和设备加速计之间有兼容的坐标系,则只需将加速度矢量缩放为大小
|r |
,并向其添加
(x0,y0)
,即可获得相同的结果,而无需任何测角功能

为了正确模拟,请使用达朗贝尔方程+圆边界

因此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;
现在
如果(| position-big_circle_center |>big_circle_radius)
发生碰撞,因此当您不想反弹时(所有能量都被吸收),则:

现在您需要删除径向速度和左切线速度:

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


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

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

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

  • x0,y0
    是大圆心
  • r=R0-R1
  • R0
    是大圆半径
  • R1
    是小圆半径
现在角度
a

最简单的方法是将
a=重力方向
放置在以下位置:

a=atanxy(acceleration.x,acceleration.y)
atanxy
atan2
,它是四象限的tangens弧。如果你没有,用我的

并将角度校正到坐标系(可能为负数或增加90度倍数)

[notes]

如果屏幕和设备加速计之间有兼容的坐标系,则只需将加速度矢量缩放为大小
|r |
,并向其添加
(x0,y0)
,即可获得相同的结果,而无需任何测角功能

为了正确模拟,请使用达朗贝尔方程+圆边界

因此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;
现在
如果(| position-big_circle_center |>big_circle_radius)
发生碰撞,因此当您不想反弹时(所有能量都被吸收),则:

现在您需要删除径向速度和左切线速度:

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


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

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

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

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