简体   繁体   English

绳子模拟html5

[英]rope simulation html5

I'm trying to simulate a rope physically correct on a website. 我正试图在网站上模拟物理上正确的绳索。

Having searched the web, my conclusion is that I need a physics engine like box2d? 搜索过网络后,我的结论是我需要一个像box2d这样的物理引擎?

If I have understood it right the solution is to split up an object into small segments and then to join and animate these all together. 如果我理解正确,解决方案是将一个对象分成小段,然后加入并将这些对象组合在一起。 In addition, I want to combine the physics with the browser. 另外,我想将物理学与浏览器结合起来。 What that means is: when I'm scrolling the website fast down, the rope has to react in a physically correct manner. 这意味着:当我快速向下滚动网站时,绳索必须以物理上正确的方式作出反应。

is this possible? 这可能吗?

You don't need a library at all. 你根本不需要图书馆。 You can happily do this with object, component or procedural code, and an understanding of basic mechanical physics and some 2D geometry (basic calculus might help). 您可以愉快地使用对象,组件或程序代码,以及对基本机械物理和一些2D几何(基本微积分可能有帮助)的理解。

Doing it in an optimized fashion would require more work. 以优化的方式进行这项工作需要更多的工作。 Doing it in a modular and reusable fashion would require some upfront engineering. 以模块化和可重复使用的方式进行这项工作需要一些前期工程。

But making a rope, and making the rope behave normally is quite straightforward. 但制作一根绳子,并使绳子表现正常是非常简单的。

A 2D rope would be a series of line-segments or "bones". 2D绳索将是一系列线段或“骨头”。 Each bone can bend around its joints. 每根骨头都可以绕着关节弯曲。

You can apply gravity or whatever other forces you would like to each bone. 您可以对每个骨骼施加重力或任何其他力量。
Each bone has constraints (the other bones it's attached to, above and below). 每个骨骼都有约束(它附着的其他骨骼,上方和下方)。
So as you update the "rope", you update each bone, based on the constraints which came before and after. 因此,当您更新“绳索”时,您将根据之前和之后的约束更新每个骨骼。
You can create an anchor point -- pinning the top of the rope to the wall, so that regardless of how the other bones move, the top-most bone will pivot around its top joint, but will not move anywhere. 您可以创建一个锚点 - 将绳索的顶部固定到墙上,这样无论其他骨骼如何移动,最顶部的骨骼将围绕其顶部关节旋转,但不会移动到任何位置。

To apply physics to the middle of the hanging rope (like pulling it by the middle, and then letting go), you would use Inverse Kinematics to solve how that force against one or two joints affected each of the other attached (and constrained ) joints. 要将物理应用于悬挂绳索的中间(如将其拉向中间,然后放开),您将使用反向运动学来解决对一个或两个关节的力如何影响其他连接(和约束 )关节的每一个。

Think of it less like a snake, and more like a bike chain. 想想它不像蛇,更像是自行车链。
The shorter each "link" in the chain, the more fluid the rope will seem, but the longer it will take to process. 链条中的每个“链节”越短,绳索看起来就越流畅,但加工所需的时间越长。
The longer each "link" in the chain, the faster you can compute the changes, but the more rigid and blocky it seems as it bends. 链中的每个“链接”越长,计算变化的速度就越快,但它看起来变得越僵硬和越块。

Look up "Verlet Integration", "Kinematics" ("Forward"/"Inverse") and have a half-decent grasp of the math behind rotation and moving an object along 2D vectors... 查找“Verlet集成”,“运动学”(“前进”/“反向”),并对旋转后的数学和沿2D矢量移动对象有一个相当不错的把握......
With a little bit of mass in there, if you want to get fancy with the whip-like stuff, rather than just making waves. 如果你想要用鞭子般的东西,而不仅仅是制造波浪,那里有一点点的质量。

The rest is just loops. 其余的只是循环。

See the following for a basic example: 有关基本示例,请参阅以下内容:
http://gamedev.tutsplus.com/tutorials/implementation/simulate-fabric-and-ragdolls-with-simple-verlet-integration/ http://gamedev.tutsplus.com/tutorials/implementation/simulate-fabric-and-ragdolls-with-simple-verlet-integration/

And have fun. 玩得开心。

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

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