简体   繁体   English

如何给移动的球带来旋转的错觉

[英]How to give the illusion of rotation to a moving ball

 //draw method
 draw() {
     //draw the ball
     ctx.beginPath();
     ctx.arc(this.xPos, this.yPos, this.radius, 0, 2 * Math.PI), false;
     ctx.moveTo(this.xPos, this.yPos);
     var i;
     //use a for loop 
     for (i = 1; i < 8; i++) {
         var x = this.radius * Math.cos(i * 2 / 7 * Math.PI) + this.xPos;
         var y = this.radius * Math.sin(i * 2 / 7 * Math.PI) + this.yPos;
         ctx.lineTo(x, y);
         ctx.moveTo(this.xPos, this.yPos);
     }

     ctx.lineTo(this.xPos, this.yPos);
     ctx.fillStyle = "rgba(125, 125, 125, 1)";
     ctx.strokeStyle = "red";
     ctx.fill();
     ctx.stroke();
 }

I'm completely new to graphics programming, and I am wondering how I could use a rotate method to shift all the lines in the circle (created by the draw method) by a fixed amount of radians as it moves我是图形编程的新手,我想知道如何使用旋转方法将圆中的所有线(由绘制方法创建)移动固定数量的弧度

here is an image of the output:这是输出的图像:

输出

There are more efficient ways to go about this, but they would require things like webgl or webgpu so just to get what you want going:有更有效的方法来解决这个问题,但它们需要像 webgl 或 webgpu 这样的东西,所以只是为了得到你想要的:

This is the 2D rotation matrix:这是二维旋转矩阵: 在此处输入图片说明

Add a theta parameter to your draw method and outside of the draw parameter increment it by a small amount like 0.001 on each iteration of a loop (remember to clear your canvas before drawing).theta参数添加到您的 draw 方法中,并且在 draw 参数之外,在循环的每次迭代中将其增加少量,例如 0.001(记住在绘制之前清除画布)。

Then inside of your method, calculate the center C of your circle.然后在您的方法内部,计算圆的中心C The formula to rotate ANY shape round its centroid C 'is:围绕其质心C '旋转任何形状的公式是:

R(theta) * (S - C) + C

Where S is the set of all points in your shape and R(theta) is the rotation matrix evaluated at theta.其中 S 是形状中所有点的集合,R(theta) 是在 theta 处评估的旋转矩阵。

In other words, for every pair of (x, y) coordinates in the cartwheel, you first subtract the center, then you multiply the resulting vector <x, y> by the rotation matrix FROM THE LEFT (R * V), the final vector <x', y'> represents the rotated shape.换句话说,对于侧手翻中的每一对 (x, y) 坐标,您首先减去中心,然后将结果向量 <x, y> 乘以从左(R * V)的旋转矩阵,最后矢量 <x', y'> 表示旋转后的形状。

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

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