Resat
Resat

Reputation: 31

Spritekit PhysicsBody applyTorque

I'm working on a universal app.

I want to applyTorque to a Paddle

But the problem is the paddle sizes are bigger on a ipad than the one one iphone.

How can I calculate the torque to apply the same affect to the Paddle's 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];

-(void)flip{

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

}

Upvotes: 3

Views: 514

Answers (2)

ZeMoon
ZeMoon

Reputation: 20274

You can set a constant mass of the physicsBody so that the torque applied affects the paddle the same way regardless of the node's 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 the body affects its momentum as well as how forces are applied to the object.

Upvotes: 1

giorashc
giorashc

Reputation: 13713

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

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 is 100 use a constant factor of 0.05 On the iPad the different paddle width will make the torque value to be calculated accordingly using the same factor value as above.

Upvotes: 2

Related Questions