获取 SCNPhysicsVehicle 的位置 [英] Getting an SCNPhysicsVehicle's Position

查看:27
本文介绍了获取 SCNPhysicsVehicle 的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apple 的 SceneKit 并且我遇到了模拟汽车的问题.使用 SCNPhysicsVehicle 行为,我能够正确设置汽车并使用记录的方法驾驶它.

I am working on a basic racing game using Apple's SceneKit and am running into issues simulating a car. Using the SCNPhysicsVehicle behavior, I am able to properly set up the car and drive it using the documented methods.

但是,我无法获得汽车的位置.SCNPhysicsVehicle 会在汽车移动时移动包含底盘和车轮的 SCNNode,但 SCNNode 保持其原始位置和方向,这似乎是合乎逻辑的.奇怪的是,底盘的 SCNPhysicsBody 速度在整个模拟过程中保持准确,因此我可以假设汽车的位置基于 SCNPhysicsBody 而不是 SCNNode.不幸的是,我没有找到获得 SCNPhysicsBody 位置的记录方法.

However, I am unable to get the car's position. It seems logical that the SCNPhysicsVehicle would move the SCNNodes that contain the chassis and the wheels as the car moves but the SCNNodes remain at their original position and orientation. Strangely enough, the chassis' SCNPhysicsBody's velocity remains accurate throughout the simulation so I can assume that the car's position is based off of the SCNPhysicsBody and not the SCNNode. Unfortunately, there is no documented method that I have found to get an SCNPhysicsBody's position.

获取汽车的位置应该是微不足道的,对于创建赛车游戏至关重要,但我似乎找不到任何获取它的方法.任何想法或建议将不胜感激.

Getting a car's position should be trivial and is essential to create a racing game but I can't seem to find any way of getting it. Any thoughts or suggestions would be appreciated.

推荐答案

Scene Kit 根据物理模拟自动更新拥有 SCNPhysicsBody 的节点的位置,所以 SCNNode.position 是要查找的正确属性.

Scene Kit automatically updates the position of the node that owns an SCNPhysicsBody based on the physics simulation, so SCNNode.position is the right property to look for.

问题在于该节点实际上有两个版本在起作用.您通常访问的节点称为模型"节点.它反映了您设置的属性的目标值,即使您通过动画设置了这些属性.presentationNode 反映了当前正在渲染的节点的状态——如果动画正在进行中,则节点的属性具有中间值,而不是动画的目标值.

The catch is that there are actually two versions of that node in play. The one you typically access is called the "model" node. It reflects the target values for properties you set, even if you set those properties through an animation. The presentationNode reflects the state of the node currently being rendered — if an animation is in progress, the node's properties have intermediate values, not the target values of the animation.

操作,物理,约束,以及您在 更新/渲染循环方法 直接针对场景图的演示"版本.因此,要读取物理模拟设置的节点属性,请获取 presentationNode 用于您感兴趣的节点(拥有车辆chassisBody 物理主体的节点),然后读取表示节点的position(或其他属性).

Actions, physics, constraints, and any scene graph changes you make inside update/render loop methods directly target the "presentation" version of your scene graph. So, to read node properties that have been set by the physics simulation, get the presentationNode for the node you're interested in (the node that owns the vehicle's chassisBody physics body), then read the presentation node's position (or other properties).

这篇关于获取 SCNPhysicsVehicle 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆