Spritekit PhysicsBody applyTorque

时间:2015-06-02 06:30:22

标签: ios objective-c sprite-kit

我正在开发一款通用应用。

我希望applyTorque参加划桨

但问题是在ipad上的桨尺寸比在iphone上大。

如何计算扭矩以对Paddle的physicalBody施加相同的效果?

SKSpriteNode *bar = [SKSpriteNode spriteNodeWithImageNamed:nameImage];
bar.name = @"bar";
bar.size = CGSizeMake(PaddleWidth, PaddleHeight);
bar.physicsBody =[SKPhysicsBody bodyWithTexture:bar.texture size:bar.size];
bar.physicsBody.restitution = 0.2;
bar.physicsBody.angularDamping = 0;
bar.physicsBody.friction = 0.02;
bar.physicsBody.mass=.2;
[paddle addChild:bar];
SKSpriteNode *anchor = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(PaddleWidth, PaddleHeight)];
anchor.name = @"anchor";
anchor.size = CGSizeMake(PaddleHeight, PaddleHeight);
anchor.position = CGPointMake(bar.position.x + bar.size.width/2, 0);
[paddle addChild:anchor];

CGFloat anchorRadius = anchor.size.width/20;

anchor.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:anchorRadius];
anchor.physicsBody.dynamic = NO;
CGPoint positionInScene = [self convertPoint:anchor.position toNode:self.scene];

pin = [SKPhysicsJointPin jointWithBodyA:bar.physicsBody
                                                     bodyB:anchor.physicsBody
                                                    anchor:positionInScene];
pin.shouldEnableLimits = YES;
pin.lowerAngleLimit = -0.5;
pin.upperAngleLimit = 0.5;
[self.scene.physicsWorld addJoint:pin];

- (无效)倒装{

SKNode *bar=[self childNodeWithName:@"bar"];
CGFloat torque;
torque=10;
[bar.physicsBody applyTorque:torque];

}

2 个答案:

答案 0 :(得分:1)

您可以使用桨的大小作为扭矩值的一个因素:

CGSize paddleSize = <Initialize with paddle size>
CGFloat torque=paddleSize.width * factor; // factor will be the value you need to multiply in order to reach your desired value

因此,如果例如桨宽为100,则使用常数因子0.05 在iPad上,不同的桨宽将使扭矩值相应地使用与上述相同的因子值来计算。

答案 1 :(得分:0)

您可以设置physicsBody的恒定质量,以便无论节点的大小如何,所施加的扭矩都会以相同的方式影响桨叶。

//While instantiating the physicsBody for the "bar" node
bar.physicsBody.mass = 0.2; //This will need to be adjusted according to your needs.

来自documentation

  

身体的质量会影响它的动量以及力量   应用于对象。