简体   繁体   English

CANNON.js:检查身体是否受到约束

[英]CANNON.js: check if a body is being constrained

I've been trying to make a multiplayer game using javascript (most of which is on the server, using Node.js); 我一直在尝试使用javascript(其中大部分在服务器上,使用Node.js)制作多人游戏; one of the core mechanics I want to make is that players will be able to design their own fighting style (right down to how they swing their sword etc). 我要制定的核心机制之一是,玩家将能够设计自己的战斗方式(直接到挥剑的方式等)。 Problem is that I can't find any simple way of constraining players' movements. 问题是我找不到限制玩家动作的任何简单方法。

I first tried to write a method that checks then clamps the player's style so it doesn't look like they're breaking every limb simultaneously, but that didn't really work. 我首先尝试编写一种方法,然后进行检查,然后确定球员的风格,以免看起来他们同时折断了所有肢体,但这并没有真正起作用。 I then found the wonderful CANNON.ConeTwistConstraint , but after looking through the documentation I've found that CANNON.js's constraints don't seem to have any sort of built-in function for just testing whether two bodies are exceeding the constraint's limits. 然后,我找到了很棒的CANNON.ConeTwistConstraint ,但是在浏览了文档之后,我发现CANNON.js的约束似乎没有任何内置函数可用于仅测试两个物体是否超出约束的限制。 I've thought about having my game just create objects in a separate simulation and check whether a force is being applied to either object, but I'm not sure about how to go about this, or if there's a better way. 我曾考虑过让我的游戏仅在单独的模拟中创建对象,然后检查是否对任一对象施加了力,但是我不确定如何进行此操作,或者是否有更好的方法。

Is there a simple/easier solution to my problem? 有没有简单/轻松的解决方案来解决我的问题? If not, what would be the least CPU-intensive way of implementing the above? 如果没有,那么实现上述最少的CPU密集型方法是什么?

You can manually check if the ConeTwistConstraint is hitting its limit. 您可以手动检查ConeTwistConstraint是否达到极限。 If you have a look at the method CANNON.ConeEquation.prototype.computeB , you can see that it computes the constraint violation, "g", using cos() and a dot product. 如果看一下CANNON.ConeEquation.prototype.computeB方法,您会发现它使用cos()和点积来计算约束违规“ g”。 You can simply do the same with the following code. 您可以使用以下代码简单地执行相同操作。

var eq = coneTwistConstraint.coneEquation;
var g = Math.cos(eq.angle) - eq.axisA.dot(eq.axisB);
if(g > 0) {
    // Constraint limit exceeded
} else {
    // Constraint is within limits
}

Using the same strategy, you can check if the twist limit is exceeded. 使用相同的策略,您可以检查是否超过了扭曲限制。 Since the twist equation is a CANNON.RotationalEquation , the code becomes: 由于扭曲方程是CANNON.RotationalEquation ,因此代码变为:

var eq2 = coneTwistConstraint.twistEquation;
var g2 = Math.cos(eq2.maxAngle) - eq2.axisA.dot(eq2.axisB);

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

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