如何在SceneKit中切换物理体类型

huangapple go评论51阅读模式
英文:

How to switch physics body type in SceneKit

问题

我在游戏中有一个运动学刚体,当需要跳跃时,我将其切换为动态刚体,以便物理模拟可以处理重力的所有复杂性。我是通过替换节点的整个物理刚体来实现的。然而,这样做也会重置所有可以自定义的物理刚体属性。是否有一种方法可以更改节点的物理刚体而不创建新的物体?或者至少有一种方法可以“复制”所有值,比如恢复、角阻尼等。

我迄今为止尝试过的方法:

  1. 更改物理刚体的type属性,但文档说它应该是一个常量(尽管它是一个可获取-设置的属性???)。我不想违反文档,所以我不认为这是正确的方法。
  2. 尝试使用指针和反射来以某种方式复制这些值。我昨天开始阅读关于Mirror的内容,所以我不确定是否可以实现这样的功能,但我正在考虑它。
  3. 使用copy()函数,但是我仍然需要手动复制所有小的设置。

请告诉我是否有一种方法可以在保留其属性(如位掩码、物理设置等)的情况下切换物理刚体类型。

谢谢

英文:

I have a kinematic body in my game that I switch to a dynamic body when it needs to jump so the physics simulation can handle all the complexities of gravity. I do this by replacing the entire physics body of the node. However, doing this also resets all the customisation you can do to physics bodies. Is there any way to change the physics body of a node without creating a new one? Or at least a way that somehow “copies” all the values like restitution, angular dampening, etc.

What I’ve tried so far:

  1. Changing the type property on a physics body but the documentation says it’s supposed to be a constant (even though it’s a get-set property???). I don’t want to go against the documentation so I don’t think this should be the right way
  2. Experimenting with pointers and reflections to copy the values somehow. I started reading about Mirror yesterday so I’m not sure if functionality like this even would be possible, but I am considering it.
  3. Using the copy() function, however, I’d still have to manually copy all the small settings

Please let me know if there’s a way to switch the physics body type while preserving its properties such as bitmasks, physics settings, etc.

Thanks

答案1

得分: 1

SCNPhysicsBody 由其自己的 "构建" 几何形状组成,因此每当您想要更改它时,您需要重新创建它。但是,您可以例如预先定义所有所需的 physicsBodies(动态、静态、运动学,包括所有属性如位掩码等)并将其分配给您需要的那个。

英文:

As a SCNPhysicsBody consists of its own "constructed" geometry, you will need to recreate it each time you want to change it. But you could i.Ex. predefine all physicsBodies you need (dynamic, static, kinematic, including all properties like bitmasks, etc...) in a set of variables or constants and then you assign the one you want at the moment you need it.

huangapple
  • 本文由 发表于 2023年2月19日 01:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定