Spritekit PhysicsBody applyTorque

I am working on a universal app.

I want applyTorque

to Paddle

But the problem is that the paddle is larger on an ipad than on one iphone.

How can I calculate the torque to apply the same effect to the physical Paddle logic?

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];

      

- (void) {flip

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

      

}

+3


source to share


2 answers


You can use the paddle size as a factor in the amount of torque:

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

      



So if for example paddle width a 100

constant factor is used 0.05

On an iPad different paddle widths will calculate the torque value using the same factor as above.

+1


source


You can set a constant physical body mass so that the applied torque acts on the paddle in the same way, regardless of the node size.

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

      



From the documentation :

The mass of a body affects its momentum as well as how forces are applied to an object.

0


source







All Articles