为什么 SCNPhysicsBody 在设置 eulerAngles 时会重置位置?
Why SCNPhysicsBody resets position when set eulerAngles?
我正在尝试使用 SceneKit 为 tvOS 开发游戏,但我遇到了问题。
当我在向 physicsBody 施加脉冲之前设置节点的 eulerAngle 时,节点将重置为其原始位置。
我期待看到节点在地板平面上四处移动,但在每次点击时,节点都会在施加脉冲之前移动到原点位置。
刚开始使用这个框架,不知道哪里出错了。
我正在使用带有 tvOS 9.0 和 XCode 7.1.1
的新 AppleTV
要重现它,您可以创建一个新的 xcode 项目(tvOS 游戏)并将 GameViewController.m 替换为以下代码:
#import "GameViewController.h"
SCNNode *ship;
SCNNode *node;
SCNNode *ground;
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
SCNScene *scene = [[SCNScene alloc] init];
scene.physicsWorld.gravity = SCNVector3Make(0, -800, 0);
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 10000;
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 64, 64);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient;
ambientLightNode.light.color = [UIColor darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
SCNGeometry *geometry;
SCNMaterial *material;
SCNNode *tempNode;
SCNPhysicsShape* shape;
SCNPhysicsBody* body;
//--
SCNScene *loaded = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
tempNode = [loaded.rootNode childNodeWithName:@"ship" recursively:YES];
geometry = [SCNCylinder cylinderWithRadius:16 height:8];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
tempNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:shape];
tempNode.physicsBody.restitution = 1;
tempNode.physicsBody.friction = 0.25;
tempNode.physicsBody.categoryBitMask = 2;
tempNode.physicsBody.collisionBitMask = 1;
tempNode.position = SCNVector3Make(32, 32, 0);
[scene.rootNode addChildNode:tempNode];
ship = tempNode;
//--
geometry = [SCNCylinder cylinderWithRadius:16 height:8];
material = [[SCNMaterial alloc] init];
material.diffuse.contents = UIColor.yellowColor;
geometry.materials = @[material];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
body = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:shape];
tempNode = [SCNNode nodeWithGeometry: geometry];
tempNode.physicsBody = body;
tempNode.physicsBody.restitution = 1;
tempNode.physicsBody.friction = 0.25;
tempNode.physicsBody.categoryBitMask = 2;
tempNode.physicsBody.collisionBitMask = 1;
tempNode.position = SCNVector3Make(0, 32, 0);
[scene.rootNode addChildNode:tempNode];
node = tempNode;
//--
geometry = [[SCNFloor alloc] init];
material = [[SCNMaterial alloc] init];
material.diffuse.contents = UIColor.blueColor;
geometry.materials = @[material];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
body = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeKinematic shape:shape];
tempNode = [SCNNode nodeWithGeometry: geometry];
tempNode.physicsBody = body;
tempNode.physicsBody.categoryBitMask = 1;
[scene.rootNode addChildNode:tempNode];
ground = tempNode;
//--
SCNLookAtConstraint * constraint = [SCNLookAtConstraint lookAtConstraintWithTarget: ground];
constraint.gimbalLockEnabled = YES;
cameraNode.constraints = @[constraint];
// configure the SCNView
SCNView *scnView = (SCNView *)self.view;
scnView.scene = scene;
scnView.allowsCameraControl = NO;
//scnView.antialiasingMode = SCNAntialiasingModeMultisampling2X;
scnView.debugOptions = SCNDebugOptionShowPhysicsShapes;
scnView.showsStatistics = YES;
scnView.backgroundColor = [UIColor blackColor];
// add a tap gesture recognizer
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
NSMutableArray *gestureRecognizers = [NSMutableArray array];
[gestureRecognizers addObject:tapGesture];
[gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
scnView.gestureRecognizers = gestureRecognizers;
}
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize
{
float x = (rand() / (float)RAND_MAX) - 0.5f;
float y = (rand() / (float)RAND_MAX) - 0.5f;
float speed = (rand() / (float)RAND_MAX) * 300;
CGPoint velocity = CGPointMake(x, y);
float angle = [self AngleBetween:velocity And:CGPointMake(1, 0)] + M_PI_2;
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
// if comment these lines the problem doesn't appears
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (float) AngleBetween:(CGPoint)_origin And:(CGPoint)_destination
{
float dotProduct = (_origin.x * _destination.x) + (_origin.y * _destination.y);
float perpDotProduct = (_origin.x * _destination.y) - (_origin.y * _destination.x);
return -atan2f(-perpDotProduct, dotProduct);
}
@end
如果您注释设置欧拉角的行(在 handleTap 方法中),则问题不会出现。
我找到了问题的答案。
基于这个答案:
我意识到我必须在应用变换之前从场景树中删除节点,然后重新插入它们。
为了跟随我的例子,这里是代码:
SCNNode *root = node.parentNode;
[node removeFromParentNode];
[ship removeFromParentNode];
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
[root addChildNode:node];
[root addChildNode:ship];
来自 Apple 的 SCNPhysicsBody 文档:
If you change the transform value—or any of the other properties that are components of the transform, such as position and rotation—of a node affected by physics, SceneKit resets the physics simulation for that node.
物理模拟值在presentationNode属性中。因此,在您的情况下,要走的路是在应用转换之前复制表示节点的位置,然后将其写回:
SCNVector3 nodePosition = [[node presentationNode] position];
SCNVector3 shipPosition = [[ship presentationNode] position];
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
// if comment these lines the problem doesn't appears
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
[node setPosition: nodePosition];
[ship setPosition: shipPosition];
Bon,6 年过去了... iOS 15, Swift 5.x 在同一个节点上做动画和物理看起来仍然很痛苦。
这是上一个答案的 swift 版本。请注意,我并没有尝试在渲染的每一帧中制作动画,因为从性能的角度来看,它 [2020 M1 MBP] 如果我这样做的话,它会杀死我的机器。 [当然是寻找 child 的循环问题。
“节点”是“parentNode”的child。物理学正在改变“parentNode”。与此同时,我正在旋转 child“节点”。
@MainActor class SceneDelegate: NSObject, SCNSceneRendererDelegate {
var share = Common.shared
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if time > spawnTime2 && !share.landed {
renderer.scene?.rootNode.childNodes.filter({ [=10=].name == "coreNode" }).forEach({ parentNode in
share.updateVelocity(figure: (parentNode.physicsBody?.velocity.y)!)
})
}
if time > spawnTime && !share.landed {
print("spawn")
renderer.scene?.rootNode.childNodes.filter({ [=10=].name == "coreNode" }).forEach({ parentNode in
let savePos = parentNode.presentation.position
parentNode.childNodes.filter({ [=10=].name == "shipNode" }).forEach({ node in
let quaternion = simd_quatf(angle: GLKMathDegreesToRadians(1), axis: simd_float3(0,1,0))
node.simdOrientation = quaternion * node.simdOrientation
// node.eulerAngles = SCNVector3(x: 0, y:GLKMathDegreesToRadians(旋转器), z:0)
旋转器 += 1
})
parentNode.position = savePos
})
}
}
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
spawnTime = time + TimeInterval(0.015)
spawnTime2 = time + TimeInterval(0.018)
}
}
四元数和欧拉旋转都有效,这就是为什么我保留代码的原因。
spawnTime 速度是一个对我有用的捏造数字。太快了会跳,太慢了根本没用
我正在尝试使用 SceneKit 为 tvOS 开发游戏,但我遇到了问题。 当我在向 physicsBody 施加脉冲之前设置节点的 eulerAngle 时,节点将重置为其原始位置。
我期待看到节点在地板平面上四处移动,但在每次点击时,节点都会在施加脉冲之前移动到原点位置。
刚开始使用这个框架,不知道哪里出错了。 我正在使用带有 tvOS 9.0 和 XCode 7.1.1
的新 AppleTV要重现它,您可以创建一个新的 xcode 项目(tvOS 游戏)并将 GameViewController.m 替换为以下代码:
#import "GameViewController.h"
SCNNode *ship;
SCNNode *node;
SCNNode *ground;
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
SCNScene *scene = [[SCNScene alloc] init];
scene.physicsWorld.gravity = SCNVector3Make(0, -800, 0);
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 10000;
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 64, 64);
// create and add a light to the scene
SCNNode *lightNode = [SCNNode node];
lightNode.light = [SCNLight light];
lightNode.light.type = SCNLightTypeOmni;
lightNode.position = SCNVector3Make(0, 10, 10);
[scene.rootNode addChildNode:lightNode];
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient;
ambientLightNode.light.color = [UIColor darkGrayColor];
[scene.rootNode addChildNode:ambientLightNode];
SCNGeometry *geometry;
SCNMaterial *material;
SCNNode *tempNode;
SCNPhysicsShape* shape;
SCNPhysicsBody* body;
//--
SCNScene *loaded = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
tempNode = [loaded.rootNode childNodeWithName:@"ship" recursively:YES];
geometry = [SCNCylinder cylinderWithRadius:16 height:8];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
tempNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:shape];
tempNode.physicsBody.restitution = 1;
tempNode.physicsBody.friction = 0.25;
tempNode.physicsBody.categoryBitMask = 2;
tempNode.physicsBody.collisionBitMask = 1;
tempNode.position = SCNVector3Make(32, 32, 0);
[scene.rootNode addChildNode:tempNode];
ship = tempNode;
//--
geometry = [SCNCylinder cylinderWithRadius:16 height:8];
material = [[SCNMaterial alloc] init];
material.diffuse.contents = UIColor.yellowColor;
geometry.materials = @[material];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
body = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:shape];
tempNode = [SCNNode nodeWithGeometry: geometry];
tempNode.physicsBody = body;
tempNode.physicsBody.restitution = 1;
tempNode.physicsBody.friction = 0.25;
tempNode.physicsBody.categoryBitMask = 2;
tempNode.physicsBody.collisionBitMask = 1;
tempNode.position = SCNVector3Make(0, 32, 0);
[scene.rootNode addChildNode:tempNode];
node = tempNode;
//--
geometry = [[SCNFloor alloc] init];
material = [[SCNMaterial alloc] init];
material.diffuse.contents = UIColor.blueColor;
geometry.materials = @[material];
shape = [SCNPhysicsShape shapeWithGeometry:geometry options:nil];
body = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeKinematic shape:shape];
tempNode = [SCNNode nodeWithGeometry: geometry];
tempNode.physicsBody = body;
tempNode.physicsBody.categoryBitMask = 1;
[scene.rootNode addChildNode:tempNode];
ground = tempNode;
//--
SCNLookAtConstraint * constraint = [SCNLookAtConstraint lookAtConstraintWithTarget: ground];
constraint.gimbalLockEnabled = YES;
cameraNode.constraints = @[constraint];
// configure the SCNView
SCNView *scnView = (SCNView *)self.view;
scnView.scene = scene;
scnView.allowsCameraControl = NO;
//scnView.antialiasingMode = SCNAntialiasingModeMultisampling2X;
scnView.debugOptions = SCNDebugOptionShowPhysicsShapes;
scnView.showsStatistics = YES;
scnView.backgroundColor = [UIColor blackColor];
// add a tap gesture recognizer
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
NSMutableArray *gestureRecognizers = [NSMutableArray array];
[gestureRecognizers addObject:tapGesture];
[gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
scnView.gestureRecognizers = gestureRecognizers;
}
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize
{
float x = (rand() / (float)RAND_MAX) - 0.5f;
float y = (rand() / (float)RAND_MAX) - 0.5f;
float speed = (rand() / (float)RAND_MAX) * 300;
CGPoint velocity = CGPointMake(x, y);
float angle = [self AngleBetween:velocity And:CGPointMake(1, 0)] + M_PI_2;
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
// if comment these lines the problem doesn't appears
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (float) AngleBetween:(CGPoint)_origin And:(CGPoint)_destination
{
float dotProduct = (_origin.x * _destination.x) + (_origin.y * _destination.y);
float perpDotProduct = (_origin.x * _destination.y) - (_origin.y * _destination.x);
return -atan2f(-perpDotProduct, dotProduct);
}
@end
如果您注释设置欧拉角的行(在 handleTap 方法中),则问题不会出现。
我找到了问题的答案。
基于这个答案:
我意识到我必须在应用变换之前从场景树中删除节点,然后重新插入它们。
为了跟随我的例子,这里是代码:
SCNNode *root = node.parentNode;
[node removeFromParentNode];
[ship removeFromParentNode];
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
[root addChildNode:node];
[root addChildNode:ship];
来自 Apple 的 SCNPhysicsBody 文档:
If you change the transform value—or any of the other properties that are components of the transform, such as position and rotation—of a node affected by physics, SceneKit resets the physics simulation for that node.
物理模拟值在presentationNode属性中。因此,在您的情况下,要走的路是在应用转换之前复制表示节点的位置,然后将其写回:
SCNVector3 nodePosition = [[node presentationNode] position];
SCNVector3 shipPosition = [[ship presentationNode] position];
[node.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
[ship.physicsBody applyForce:SCNVector3Make(velocity.x*speed, 0, velocity.y*speed) impulse:YES];
// if comment these lines the problem doesn't appears
node.eulerAngles = SCNVector3Make(0, angle, 0);
ship.eulerAngles = SCNVector3Make(0, angle, 0);
[node setPosition: nodePosition];
[ship setPosition: shipPosition];
Bon,6 年过去了... iOS 15, Swift 5.x 在同一个节点上做动画和物理看起来仍然很痛苦。
这是上一个答案的 swift 版本。请注意,我并没有尝试在渲染的每一帧中制作动画,因为从性能的角度来看,它 [2020 M1 MBP] 如果我这样做的话,它会杀死我的机器。 [当然是寻找 child 的循环问题。
“节点”是“parentNode”的child。物理学正在改变“parentNode”。与此同时,我正在旋转 child“节点”。
@MainActor class SceneDelegate: NSObject, SCNSceneRendererDelegate {
var share = Common.shared
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if time > spawnTime2 && !share.landed {
renderer.scene?.rootNode.childNodes.filter({ [=10=].name == "coreNode" }).forEach({ parentNode in
share.updateVelocity(figure: (parentNode.physicsBody?.velocity.y)!)
})
}
if time > spawnTime && !share.landed {
print("spawn")
renderer.scene?.rootNode.childNodes.filter({ [=10=].name == "coreNode" }).forEach({ parentNode in
let savePos = parentNode.presentation.position
parentNode.childNodes.filter({ [=10=].name == "shipNode" }).forEach({ node in
let quaternion = simd_quatf(angle: GLKMathDegreesToRadians(1), axis: simd_float3(0,1,0))
node.simdOrientation = quaternion * node.simdOrientation
// node.eulerAngles = SCNVector3(x: 0, y:GLKMathDegreesToRadians(旋转器), z:0) 旋转器 += 1 })
parentNode.position = savePos
})
}
}
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
spawnTime = time + TimeInterval(0.015)
spawnTime2 = time + TimeInterval(0.018)
}
}
四元数和欧拉旋转都有效,这就是为什么我保留代码的原因。
spawnTime 速度是一个对我有用的捏造数字。太快了会跳,太慢了根本没用